From 75d668e152122f0039c7573c7c52ee6fa95371ce Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 13 Nov 2015 15:32:29 +0100 Subject: [PATCH] audio-converter: add resampler Add a resampler to the processing chain when needed. port the audio resampler to the new audioconverter library --- gst-libs/gst/audio/Makefile.am | 3 + gst-libs/gst/audio/audio-converter.c | 82 +- gst-libs/gst/audio/audio-converter.h | 9 + gst-libs/gst/audio/audio-resampler-core.h | 240 +++ gst-libs/gst/audio/audio-resampler.c | 1121 ++++++++++++++ gst-libs/gst/audio/audio-resampler.h | 188 +++ gst-libs/gst/audio/audio.h | 1 + gst-libs/gst/audio/dbesi0.c | 147 ++ gst/audioresample/Makefile.am | 20 +- gst/audioresample/arch.h | 266 ---- gst/audioresample/fixed_arm4.h | 148 -- gst/audioresample/fixed_arm5e.h | 178 --- gst/audioresample/fixed_bfin.h | 176 --- gst/audioresample/fixed_debug.h | 487 ------ gst/audioresample/fixed_generic.h | 110 -- gst/audioresample/gstaudioresample.c | 710 +++------ gst/audioresample/gstaudioresample.h | 27 +- gst/audioresample/resample.c | 1516 ------------------- gst/audioresample/resample_neon.h | 202 --- gst/audioresample/resample_sse.h | 229 --- gst/audioresample/speex_resampler.h | 403 ----- gst/audioresample/speex_resampler_double.c | 26 - gst/audioresample/speex_resampler_float.c | 27 - gst/audioresample/speex_resampler_int.c | 26 - gst/audioresample/speex_resampler_wrapper.h | 192 --- 25 files changed, 1991 insertions(+), 4543 deletions(-) create mode 100644 gst-libs/gst/audio/audio-resampler-core.h create mode 100644 gst-libs/gst/audio/audio-resampler.c create mode 100644 gst-libs/gst/audio/audio-resampler.h create mode 100644 gst-libs/gst/audio/dbesi0.c delete mode 100644 gst/audioresample/arch.h delete mode 100644 gst/audioresample/fixed_arm4.h delete mode 100644 gst/audioresample/fixed_arm5e.h delete mode 100644 gst/audioresample/fixed_bfin.h delete mode 100644 gst/audioresample/fixed_debug.h delete mode 100644 gst/audioresample/fixed_generic.h delete mode 100644 gst/audioresample/resample.c delete mode 100644 gst/audioresample/resample_neon.h delete mode 100644 gst/audioresample/resample_sse.h delete mode 100644 gst/audioresample/speex_resampler.h delete mode 100644 gst/audioresample/speex_resampler_double.c delete mode 100644 gst/audioresample/speex_resampler_float.c delete mode 100644 gst/audioresample/speex_resampler_int.c delete mode 100644 gst/audioresample/speex_resampler_wrapper.h diff --git a/gst-libs/gst/audio/Makefile.am b/gst-libs/gst/audio/Makefile.am index e707d1d5c2..6b5e881446 100644 --- a/gst-libs/gst/audio/Makefile.am +++ b/gst-libs/gst/audio/Makefile.am @@ -10,6 +10,7 @@ glib_enum_headers= \ audio-converter.h \ audio-info.h \ audio-quantize.h \ + audio-resampler.h \ gstaudioringbuffer.h glib_enum_define = GST_AUDIO @@ -33,6 +34,7 @@ libgstaudio_@GST_API_VERSION@_la_SOURCES = \ audio-converter.c \ audio-info.c \ audio-quantize.c \ + audio-resampler.c \ gstaudioringbuffer.c \ gstaudioclock.c \ gstaudiocdsrc.c \ @@ -59,6 +61,7 @@ libgstaudio_@GST_API_VERSION@include_HEADERS = \ audio-converter.h \ audio-info.h \ audio-quantize.h \ + audio-resampler.h \ gstaudioringbuffer.h \ gstaudioclock.h \ gstaudiofilter.h \ diff --git a/gst-libs/gst/audio/audio-converter.c b/gst-libs/gst/audio/audio-converter.c index 31f2a0eea0..d7b831a192 100644 --- a/gst-libs/gst/audio/audio-converter.c +++ b/gst-libs/gst/audio/audio-converter.c @@ -127,6 +127,10 @@ struct _GstAudioConverter GstAudioChannelMixer *mix; AudioChain *mix_chain; + /* resample */ + GstAudioResampler *resampler; + AudioChain *resample_chain; + /* convert out */ AudioConvertFunc convert_out; AudioChain *convert_out_chain; @@ -213,16 +217,19 @@ audio_chain_free (AudioChain * chain) } static gpointer * -audio_chain_alloc_samples (AudioChain * chain, gsize num_samples) +audio_chain_alloc_samples (AudioChain * chain, gsize num_samples, gsize * avail) { - return chain->alloc_func (chain, num_samples, chain->alloc_data); + return chain->alloc_func (chain, num_samples, avail, chain->alloc_data); } static void audio_chain_set_samples (AudioChain * chain, gpointer * samples, gsize num_samples) { - GST_LOG ("set samples %p %" G_GSIZE_FORMAT, samples, num_samples); + if (num_samples == 0) + return; + + GST_LOG ("set samples %" G_GSIZE_FORMAT, num_samples); chain->samples = samples; chain->num_samples = num_samples; @@ -264,10 +271,14 @@ get_opt_enum (GstAudioConverter * convert, const gchar * opt, GType type, return res; } +#define DEFAULT_OPT_RESAMPLER_METHOD GST_AUDIO_RESAMPLER_METHOD_BLACKMAN_NUTTALL #define DEFAULT_OPT_DITHER_METHOD GST_AUDIO_DITHER_NONE #define DEFAULT_OPT_NOISE_SHAPING_METHOD GST_AUDIO_NOISE_SHAPING_NONE #define DEFAULT_OPT_QUANTIZATION 1 +#define GET_OPT_RESAMPLER_METHOD(c) get_opt_enum(c, \ + GST_AUDIO_CONVERTER_OPT_RESAMPLER_METHOD, GST_TYPE_AUDIO_RESAMPLER_METHOD, \ + DEFAULT_OPT_RESAMPLER_METHOD) #define GET_OPT_DITHER_METHOD(c) get_opt_enum(c, \ GST_AUDIO_CONVERTER_OPT_DITHER_METHOD, GST_TYPE_AUDIO_DITHER_METHOD, \ DEFAULT_OPT_DITHER_METHOD) @@ -448,6 +459,7 @@ do_unpack (AudioChain * chain, gpointer user_data) } } else { tmp = convert->in_data; + num_samples = convert->in_samples; GST_LOG ("get in samples %p", tmp); } audio_chain_set_samples (chain, tmp, num_samples); @@ -493,6 +505,32 @@ do_mix (AudioChain * chain, gpointer user_data) return TRUE; } +static gboolean +do_resample (AudioChain * chain, gpointer user_data) +{ + GstAudioConverter *convert = user_data; + gpointer *in, *out; + gsize in_frames, out_frames, produced, consumed; + + in = audio_chain_get_samples (chain->prev, &in_frames); + + out_frames = + gst_audio_resampler_get_out_frames (convert->resampler, in_frames); + out = + (chain->allow_ip ? in : audio_chain_alloc_samples (chain, out_frames, + &out_frames)); + + GST_LOG ("resample %p %p,%" G_GSIZE_FORMAT " %" G_GSIZE_FORMAT, in, out, + in_frames, out_frames); + + gst_audio_resampler_resample (convert->resampler, in, in_frames, out, + out_frames, &produced, &consumed); + + audio_chain_set_samples (chain, out, produced); + + return TRUE; +} + static gboolean do_convert_out (AudioChain * chain, gpointer user_data) { @@ -631,6 +669,35 @@ chain_mix (GstAudioConverter * convert, AudioChain * prev) return prev; } +static AudioChain * +chain_resample (GstAudioConverter * convert, AudioChain * prev) +{ + GstAudioInfo *in = &convert->in; + GstAudioInfo *out = &convert->out; + GstAudioResamplerMethod method; + GstAudioResamplerFlags flags; + GstAudioFormat format = convert->current_format; + gint channels = convert->current_channels; + + if (in->rate != out->rate) { + method = GET_OPT_RESAMPLER_METHOD (convert); + + flags = 0; + if (convert->current_layout == GST_AUDIO_LAYOUT_NON_INTERLEAVED) + flags |= GST_AUDIO_RESAMPLER_FLAG_NON_INTERLEAVED; + + convert->resampler = + gst_audio_resampler_new (method, flags, format, channels, in->rate, + out->rate, convert->config); + + prev = convert->resample_chain = audio_chain_new (prev, convert); + prev->allow_ip = FALSE; + prev->pass_alloc = FALSE; + audio_chain_set_make_func (prev, do_resample, convert, NULL); + } + return prev; +} + static AudioChain * chain_convert_out (GstAudioConverter * convert, AudioChain * prev) { @@ -840,7 +907,6 @@ gst_audio_converter_new (GstAudioConverterFlags flags, GstAudioInfo * in_info, g_return_val_if_fail (in_info != NULL, FALSE); g_return_val_if_fail (out_info != NULL, FALSE); - g_return_val_if_fail (in_info->rate == out_info->rate, FALSE); g_return_val_if_fail (in_info->layout == GST_AUDIO_LAYOUT_INTERLEAVED, FALSE); g_return_val_if_fail (in_info->layout == out_info->layout, FALSE); @@ -868,11 +934,13 @@ gst_audio_converter_new (GstAudioConverterFlags flags, GstAudioInfo * in_info, prev = chain_convert_in (convert, prev); /* step 3, channel mix */ prev = chain_mix (convert, prev); - /* step 4, optional convert for quantize */ + /* step 4, resample */ + prev = chain_resample (convert, prev); + /* step 5, optional convert for quantize */ prev = chain_convert_out (convert, prev); - /* step 5, optional quantize */ + /* step 6, optional quantize */ prev = chain_quantize (convert, prev); - /* step 6, pack */ + /* step 7, pack */ convert->pack_chain = chain_pack (convert, prev); /* optimize */ diff --git a/gst-libs/gst/audio/audio-converter.h b/gst-libs/gst/audio/audio-converter.h index 6d037db0dd..14417d6db4 100644 --- a/gst-libs/gst/audio/audio-converter.h +++ b/gst-libs/gst/audio/audio-converter.h @@ -28,6 +28,15 @@ typedef struct _GstAudioConverter GstAudioConverter; +/** + * GST_AUDIO_CONVERTER_OPT_RESAMPLER_METHOD: + * + * #GST_TYPE_AUDIO_RESAMPLER_METHOD, The resampler method to use when + * changing sample rates. + * Default is #GST_AUDIO_RESAMPLER_METHOD_BLACKMAN_NUTTALL. + */ +#define GST_AUDIO_CONVERTER_OPT_RESAMPLER_METHOD "GstAudioConverter.resampler-method" + /** * GST_AUDIO_CONVERTER_OPT_DITHER_METHOD: * diff --git a/gst-libs/gst/audio/audio-resampler-core.h b/gst-libs/gst/audio/audio-resampler-core.h new file mode 100644 index 0000000000..2a2fd1291e --- /dev/null +++ b/gst-libs/gst/audio/audio-resampler-core.h @@ -0,0 +1,240 @@ +/* GStreamer + * Copyright (C) <2015> Wim Taymans + * + * 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 library 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + + +#define PRECISION_S16 15 +#define PRECISION_S32 30 + +#ifdef HAVE_EMMINTRIN_H +#include +#endif + +static inline void +inner_product_gdouble (gdouble * o, const gdouble * a, const gdouble * b, + gint len) +{ + gint i = 0; + gdouble res; +#ifdef HAVE_EMMINTRIN_H + __m128d sum = _mm_setzero_pd (); + + for (; i < len - 7; i += 8) { + sum = + _mm_add_pd (sum, _mm_mul_pd (_mm_loadu_pd (a + i + 0), + _mm_loadu_pd (b + i + 0))); + sum = + _mm_add_pd (sum, _mm_mul_pd (_mm_loadu_pd (a + i + 2), + _mm_loadu_pd (b + i + 2))); + sum = + _mm_add_pd (sum, _mm_mul_pd (_mm_loadu_pd (a + i + 4), + _mm_loadu_pd (b + i + 4))); + sum = + _mm_add_pd (sum, _mm_mul_pd (_mm_loadu_pd (a + i + 6), + _mm_loadu_pd (b + i + 6))); + } + sum = _mm_add_sd (sum, _mm_unpackhi_pd (sum, sum)); + _mm_store_sd (&res, sum); +#else + res = 0.0; +#endif + + for (; i < len; i++) + res += a[i] * b[i]; + + *o = res; +} + +static inline void +inner_product_gfloat (gfloat * o, const gfloat * a, const gfloat * b, gint len) +{ + gint i = 0; + gfloat res; +#ifdef HAVE_EMMINTRIN_H + __m128 sum = _mm_setzero_ps (); + + for (; i < len - 7; i += 8) { + sum = + _mm_add_ps (sum, _mm_mul_ps (_mm_loadu_ps (a + i + 0), + _mm_loadu_ps (b + i + 0))); + sum = + _mm_add_ps (sum, _mm_mul_ps (_mm_loadu_ps (a + i + 4), + _mm_loadu_ps (b + i + 4))); + } + sum = _mm_add_ps (sum, _mm_movehl_ps (sum, sum)); + sum = _mm_add_ss (sum, _mm_shuffle_ps (sum, sum, 0x55)); + _mm_store_ss (&res, sum); +#else + res = 0.0; +#endif + + for (; i < len; i++) + res += a[i] * b[i]; + + *o = res; +} + +static inline void +inner_product_gint32 (gint32 * o, const gint32 * a, const gint32 * b, gint len) +{ + gint i = 0; + gint64 res = 0; + + for (; i < len; i++) + res += (gint64) a[i] * (gint64) b[i]; + + res = (res + (1 << (PRECISION_S32 - 1))) >> PRECISION_S32; + *o = CLAMP (res, -(1L << 31), (1L << 31) - 1); +} + +static inline void +inner_product_gint16 (gint16 * o, const gint16 * a, const gint16 * b, gint len) +{ + gint i = 0; + gint32 res = 0; +#ifdef HAVE_EMMINTRIN_H + __m128i sum[2], ta, tb; + __m128i t1[2]; + + sum[0] = _mm_setzero_si128 (); + sum[1] = _mm_setzero_si128 (); + + for (; i < len - 7; i += 8) { + ta = _mm_loadu_si128 ((__m128i *) (a + i)); + tb = _mm_loadu_si128 ((__m128i *) (b + i)); + + t1[0] = _mm_mullo_epi16 (ta, tb); + t1[1] = _mm_mulhi_epi16 (ta, tb); + + sum[0] = _mm_add_epi32 (sum[0], _mm_unpacklo_epi16 (t1[0], t1[1])); + sum[1] = _mm_add_epi32 (sum[1], _mm_unpackhi_epi16 (t1[0], t1[1])); + } + sum[0] = _mm_add_epi32 (sum[0], sum[1]); + sum[0] = + _mm_add_epi32 (sum[0], _mm_shuffle_epi32 (sum[0], _MM_SHUFFLE (2, 3, 2, + 3))); + sum[0] = + _mm_add_epi32 (sum[0], _mm_shuffle_epi32 (sum[0], _MM_SHUFFLE (1, 1, 1, + 1))); + res = _mm_cvtsi128_si32 (sum[0]); +#else + res = 0; +#endif + + for (; i < len; i++) + res += (gint32) a[i] * (gint32) b[i]; + + res = (res + (1 << (PRECISION_S16 - 1))) >> PRECISION_S16; + *o = CLAMP (res, -(1L << 15), (1L << 15) - 1); +} + +static inline void +inner_product_gdouble_2 (gdouble * o, const gdouble * a, const gdouble * b, + gint len) +{ + gint i = 0; + gdouble r[2]; +#ifdef HAVE_EMMINTRIN_H + __m128d sum = _mm_setzero_pd (), t; + + for (; i < len - 3; i += 4) { + t = _mm_loadu_pd (b + i); + sum = + _mm_add_pd (sum, _mm_mul_pd (_mm_loadu_pd (a + 2 * i), + _mm_unpacklo_pd (t, t))); + sum = + _mm_add_pd (sum, _mm_mul_pd (_mm_loadu_pd (a + 2 * i + 2), + _mm_unpackhi_pd (t, t))); + + t = _mm_loadu_pd (b + i + 2); + sum = + _mm_add_pd (sum, _mm_mul_pd (_mm_loadu_pd (a + 2 * i + 4), + _mm_unpacklo_pd (t, t))); + sum = + _mm_add_pd (sum, _mm_mul_pd (_mm_loadu_pd (a + 2 * i + 6), + _mm_unpackhi_pd (t, t))); + } + _mm_store_pd (r, sum); +#else + r[0] = 0.0; + r[1] = 0.0; +#endif + + for (; i < len; i++) { + r[0] += a[2 * i] * b[i]; + r[1] += a[2 * i + 1] * b[i]; + } + o[0] = r[0]; + o[1] = r[1]; +} + +static inline void +inner_product_gint16_2 (gint16 * o, const gint16 * a, const gint16 * b, gint len) +{ + gint i = 0; + gint32 r[2]; +#ifdef HAVE_EMMINTRIN_H + guint64 r64; + __m128i sum[2], ta, tb; + __m128i t1[2]; + + sum[0] = _mm_setzero_si128 (); + sum[1] = _mm_setzero_si128 (); + + for (; i < len - 7; i += 8) { + tb = _mm_loadu_si128 ((__m128i *) (b + i)); + + t1[1] = _mm_unpacklo_epi16 (tb, tb); + + ta = _mm_loadu_si128 ((__m128i *) (a + 2 * i)); + t1[0] = _mm_mullo_epi16 (ta, t1[1]); + t1[1] = _mm_mulhi_epi16 (ta, t1[1]); + + sum[0] = _mm_add_epi32 (sum[0], _mm_unpacklo_epi16 (t1[0], t1[1])); + sum[1] = _mm_add_epi32 (sum[1], _mm_unpackhi_epi16 (t1[0], t1[1])); + + t1[1] = _mm_unpackhi_epi16 (tb, tb); + + ta = _mm_loadu_si128 ((__m128i *) (a + 2 * i + 8)); + t1[0] = _mm_mullo_epi16 (ta, t1[1]); + t1[1] = _mm_mulhi_epi16 (ta, t1[1]); + + sum[0] = _mm_add_epi32 (sum[0], _mm_unpacklo_epi16 (t1[0], t1[1])); + sum[1] = _mm_add_epi32 (sum[1], _mm_unpackhi_epi16 (t1[0], t1[1])); + } + sum[0] = _mm_add_epi32 (sum[0], sum[1]); + sum[0] = + _mm_add_epi32 (sum[0], _mm_shuffle_epi32 (sum[0], _MM_SHUFFLE (2, 3, 2, + 3))); + r64 = _mm_cvtsi128_si64 (sum[0]); + r[0] = r64 >> 32; + r[1] = r64 & 0xffffffff; +#else + r[0] = 0; + r[1] = 0; +#endif + + for (; i < len; i++) { + r[0] += (gint32) a[2 * i] * (gint32) b[i]; + r[1] += (gint32) a[2 * i + 1] * (gint32) b[i]; + } + r[0] = (r[0] + (1 << (PRECISION_S16 - 1))) >> PRECISION_S16; + r[1] = (r[1] + (1 << (PRECISION_S16 - 1))) >> PRECISION_S16; + o[0] = CLAMP (r[0], -(1L << 15), (1L << 15) - 1); + o[1] = CLAMP (r[1], -(1L << 15), (1L << 15) - 1); +} diff --git a/gst-libs/gst/audio/audio-resampler.c b/gst-libs/gst/audio/audio-resampler.c new file mode 100644 index 0000000000..75a3b424f9 --- /dev/null +++ b/gst-libs/gst/audio/audio-resampler.c @@ -0,0 +1,1121 @@ +/* GStreamer + * Copyright (C) <2015> Wim Taymans + * + * 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 library 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include + +#include "audio-resampler.h" + +typedef struct _Tap +{ + gpointer taps; + + gint sample_inc; + gint next_phase; + gint size; +} Tap; + +typedef void (*MakeTapsFunc) (GstAudioResampler * resampler, Tap * t, gint j); +typedef void (*ResampleFunc) (GstAudioResampler * resampler, gpointer in[], + gsize in_len, gpointer out[], gsize out_len, gsize * consumed, + gsize * produced, gboolean move); +typedef void (*DeinterleaveFunc) (GstAudioResampler * resampler, + gpointer * sbuf, gpointer in[], gsize in_frames); +typedef void (*MirrorFunc) (GstAudioResampler * resampler, gpointer * sbuf); + +struct _GstAudioResampler +{ + GstAudioResamplerMethod method; + GstAudioResamplerFlags flags; + GstAudioFormat format; + GstStructure *options; + guint channels; + gint in_rate; + gint out_rate; + gint bps, bpf; + gint ostride; + + gdouble cutoff; + gdouble kaiser_beta; + /* for cubic */ + gdouble b, c; + + guint n_taps; + Tap *taps; + gpointer coeff; + gpointer tmpcoeff; + + DeinterleaveFunc deinterleave; + MirrorFunc mirror; + ResampleFunc resample; + + gboolean filling; + gint samp_inc; + gint samp_frac; + gint samp_index; + gint samp_phase; + gint skip; + + gpointer samples; + gsize samples_len; + gsize samples_avail; + gpointer *sbuf; +}; + +#ifndef GST_DISABLE_GST_DEBUG +#define GST_CAT_DEFAULT ensure_debug_category() +static GstDebugCategory * +ensure_debug_category (void) +{ + static gsize cat_gonce = 0; + + if (g_once_init_enter (&cat_gonce)) { + gsize cat_done; + + cat_done = (gsize) _gst_debug_category_new ("audio-resampler", 0, + "audio-resampler object"); + + g_once_init_leave (&cat_gonce, cat_done); + } + + return (GstDebugCategory *) cat_gonce; +} +#else +#define ensure_debug_category() /* NOOP */ +#endif /* GST_DISABLE_GST_DEBUG */ + +/** + * SECTION:gstaudioresampler + * @short_description: Utility structure for resampler information + * + * #GstAudioResampler is a structure which holds the information + * required to perform various kinds of resampling filtering. + * + */ + +typedef struct +{ + gdouble cutoff; + gdouble downsample_cutoff_factor; + gdouble stopband_attenuation; + gdouble transition_bandwidth; +} KaiserQualityMap; + +static const KaiserQualityMap kaiser_qualities[] = { + {0.860, 0.96511, 60, 0.7}, /* 8 taps */ + {0.880, 0.96591, 65, 0.29}, /* 16 taps */ + {0.910, 0.96923, 70, 0.145}, /* 32 taps */ + {0.920, 0.97600, 80, 0.105}, /* 48 taps */ + {0.940, 0.97979, 85, 0.087}, /* 64 taps default quality */ + {0.940, 0.98085, 95, 0.077}, /* 80 taps */ + {0.945, 0.99471, 100, 0.068}, /* 96 taps */ + {0.950, 1.0, 105, 0.055}, /* 128 taps */ + {0.960, 1.0, 110, 0.045}, /* 160 taps */ + {0.968, 1.0, 115, 0.039}, /* 192 taps */ + {0.975, 1.0, 120, 0.0305} /* 256 taps */ +}; + +typedef struct +{ + guint n_taps; + gdouble cutoff; +} BlackmanQualityMap; + +static const BlackmanQualityMap blackman_qualities[] = { + {8, 0.5,}, + {16, 0.6,}, + {24, 0.72,}, + {32, 0.8,}, + {48, 0.85,}, /* default */ + {64, 0.90,}, + {80, 0.92,}, + {96, 0.933,}, + {128, 0.950,}, + {148, 0.955,}, + {160, 0.960,} +}; + +#define DEFAULT_QUALITY GST_AUDIO_RESAMPLER_QUALITY_DEFAULT +#define DEFAULT_OPT_CUBIC_B 1.0 +#define DEFAULT_OPT_CUBIC_C 0.0 + +static gdouble +get_opt_double (GstStructure * options, const gchar * name, gdouble def) +{ + gdouble res; + if (!options || !gst_structure_get_double (options, name, &res)) + res = def; + return res; +} + +static gint +get_opt_int (GstStructure * options, const gchar * name, gint def) +{ + gint res; + if (!options || !gst_structure_get_int (options, name, &res)) + res = def; + return res; +} + +#define GET_OPT_CUTOFF(options,def) get_opt_double(options, \ + GST_AUDIO_RESAMPLER_OPT_CUTOFF,def) +#define GET_OPT_DOWN_CUTOFF_FACTOR(options,def) get_opt_double(options, \ + GST_AUDIO_RESAMPLER_OPT_DOWN_CUTOFF_FACTOR, def) +#define GET_OPT_STOP_ATTENUATION(options,def) get_opt_double(options, \ + GST_AUDIO_RESAMPLER_OPT_STOP_ATTENUATION, def) +#define GET_OPT_TRANSITION_BANDWIDTH(options,def) get_opt_double(options, \ + GST_AUDIO_RESAMPLER_OPT_TRANSITION_BANDWIDTH, def) +#define GET_OPT_CUBIC_B(options) get_opt_double(options, \ + GST_AUDIO_RESAMPLER_OPT_CUBIC_B, DEFAULT_OPT_CUBIC_B) +#define GET_OPT_CUBIC_C(options) get_opt_double(options, \ + GST_AUDIO_RESAMPLER_OPT_CUBIC_C, DEFAULT_OPT_CUBIC_C) +#define GET_OPT_N_TAPS(options,def) get_opt_int(options, \ + GST_AUDIO_RESAMPLER_OPT_N_TAPS, def) + +#include "dbesi0.c" +#define bessel dbesi0 + +static inline gdouble +get_nearest_tap (GstAudioResampler * resampler, gdouble x) +{ + gdouble a = fabs (x); + + if (a < 0.5) + return 1.0; + else + return 0.0; +} + +static inline gdouble +get_linear_tap (GstAudioResampler * resampler, gdouble x) +{ + gdouble a; + + a = fabs (x) / resampler->n_taps; + + if (a < 1.0) + return 1.0 - a; + else + return 0.0; +} + +static inline gdouble +get_cubic_tap (GstAudioResampler * resampler, gdouble x) +{ + gdouble a, a2, a3, b, c; + + a = fabs (x * 4.0) / resampler->n_taps; + a2 = a * a; + a3 = a2 * a; + + b = resampler->b; + c = resampler->c; + + if (a <= 1.0) + return ((12.0 - 9.0 * b - 6.0 * c) * a3 + + (-18.0 + 12.0 * b + 6.0 * c) * a2 + (6.0 - 2.0 * b)) / 6.0; + else if (a <= 2.0) + return ((-b - 6.0 * c) * a3 + + (6.0 * b + 30.0 * c) * a2 + + (-12.0 * b - 48.0 * c) * a + (8.0 * b + 24.0 * c)) / 6.0; + else + return 0.0; +} + +static inline gdouble +get_blackman_nuttall_tap (GstAudioResampler * resampler, gdouble x) +{ + gdouble s, y, w, Fc = resampler->cutoff; + + y = G_PI * x; + s = (y == 0.0 ? Fc : sin (y * Fc) / y); + + w = 2.0 * y / resampler->n_taps + G_PI; + return s * (0.3635819 - 0.4891775 * cos (w) + 0.1365995 * cos (2 * w) - + 0.0106411 * cos (3 * w)); +} + +static inline gdouble +get_kaiser_tap (GstAudioResampler * resampler, gdouble x) +{ + gdouble s, y, w, Fc = resampler->cutoff; + + y = G_PI * x; + s = (y == 0.0 ? Fc : sin (y * Fc) / y); + + w = 2.0 * x / resampler->n_taps; + return s * bessel (resampler->kaiser_beta * sqrt (MAX (1 - w * w, 0))); +} + +#define CONVERT_TAPS(type, precision) \ +G_STMT_START { \ + type *taps = t->taps = (type *) resampler->coeff + j * n_taps; \ + gdouble multiplier = (1 << precision); \ + gint i, j; \ + gdouble offset, l_offset, h_offset; \ + gboolean exact = FALSE; \ + /* Round to integer, but with an adjustable bias that we use to */ \ + /* eliminate the DC error. */ \ + l_offset = 0.0; \ + h_offset = 1.0; \ + offset = 0.5; \ + for (i = 0; i < 32; i++) { \ + gint64 sum = 0; \ + for (j = 0; j < n_taps; j++) \ + sum += taps[j] = floor (offset + tmpcoeff[j] * multiplier / weight); \ + if (sum == (1 << precision)) { \ + exact = TRUE; \ + break; \ + } \ + if (l_offset == h_offset) \ + break; \ + if (sum < (1 << precision)) { \ + if (offset > l_offset) \ + l_offset = offset; \ + offset += (h_offset - l_offset) / 2; \ + } else { \ + if (offset < h_offset) \ + h_offset = offset; \ + offset -= (h_offset - l_offset) / 2; \ + } \ + } \ + if (!exact) \ + GST_WARNING ("can't find exact taps"); \ +} G_STMT_END + +#include "audio-resampler-core.h" + +static void +make_taps (GstAudioResampler * resampler, Tap * t, gint j) +{ + gint n_taps = resampler->n_taps; + gdouble x, weight = 0.0; + gdouble *tmpcoeff = resampler->tmpcoeff; + gint tap_offs = n_taps / 2; + gint out_rate = resampler->out_rate; + gint l; + + x = ((double) (1.0 - tap_offs) - (double) j / out_rate); + + switch (resampler->method) { + case GST_AUDIO_RESAMPLER_METHOD_NEAREST: + for (l = 0; l < n_taps; l++, x += 1.0) + weight += tmpcoeff[l] = get_nearest_tap (resampler, x); + break; + + case GST_AUDIO_RESAMPLER_METHOD_LINEAR: + for (l = 0; l < n_taps; l++, x += 1.0) + weight += tmpcoeff[l] = get_linear_tap (resampler, x); + break; + + case GST_AUDIO_RESAMPLER_METHOD_CUBIC: + for (l = 0; l < n_taps; l++, x += 1.0) + weight += tmpcoeff[l] = get_cubic_tap (resampler, x); + break; + + case GST_AUDIO_RESAMPLER_METHOD_BLACKMAN_NUTTALL: + for (l = 0; l < n_taps; l++, x += 1.0) + weight += tmpcoeff[l] = get_blackman_nuttall_tap (resampler, x); + break; + + case GST_AUDIO_RESAMPLER_METHOD_KAISER: + for (l = 0; l < n_taps; l++, x += 1.0) + weight += tmpcoeff[l] = get_kaiser_tap (resampler, x); + break; + + default: + break; + } + + switch (resampler->format) { + case GST_AUDIO_FORMAT_F64: + { + gdouble *taps = t->taps = (gdouble *) resampler->coeff + j * n_taps; + for (l = 0; l < n_taps; l++) + taps[l] = tmpcoeff[l] / weight; + break; + } + case GST_AUDIO_FORMAT_F32: + { + gfloat *taps = t->taps = (gfloat *) resampler->coeff + j * n_taps; + for (l = 0; l < n_taps; l++) + taps[l] = tmpcoeff[l] / weight; + break; + } + case GST_AUDIO_FORMAT_S32: + CONVERT_TAPS (gint32, PRECISION_S32); + break; + case GST_AUDIO_FORMAT_S16: + CONVERT_TAPS (gint16, PRECISION_S16); + break; + default: + break; + } +} + +#define MAKE_RESAMPLE_FUNC(type) \ +static void \ +resample_ ##type (GstAudioResampler * resampler, gpointer in[], gsize in_len, \ + gpointer out[], gsize out_len, gsize * consumed, gsize * produced, \ + gboolean move) \ +{ \ + gint c, di = 0; \ + gint n_taps = resampler->n_taps; \ + gint channels = resampler->channels; \ + gint ostride = resampler->ostride; \ + gint samp_index = 0; \ + gint samp_phase = 0; \ + \ + for (c = 0; c < channels; c++) { \ + type *ip = in[c]; \ + type *op = ostride == 1 ? out[c] : (type *)out[0] + c; \ + \ + samp_index = resampler->samp_index; \ + samp_phase = resampler->samp_phase; \ + \ + for (di = 0; di < out_len; di++) { \ + Tap *t = &resampler->taps[samp_phase]; \ + type *ipp = &ip[samp_index]; \ + \ + if (t->taps == NULL) \ + make_taps (resampler, t, samp_phase); \ + \ + inner_product_ ##type (op, ipp, t->taps, n_taps); \ + op += ostride; \ + \ + samp_phase = t->next_phase; \ + samp_index += t->sample_inc; \ + } \ + if (move) \ + memmove (ip, &ip[samp_index], (in_len - samp_index) * sizeof(type)); \ + } \ + *consumed = samp_index - resampler->samp_index; \ + *produced = di; \ + \ + resampler->samp_index = move ? 0 : samp_index; \ + resampler->samp_phase = samp_phase; \ +} + +MAKE_RESAMPLE_FUNC (gdouble); +MAKE_RESAMPLE_FUNC (gfloat); +MAKE_RESAMPLE_FUNC (gint32); +MAKE_RESAMPLE_FUNC (gint16); + +#define MAKE_RESAMPLE_INTERLEAVED_FUNC(type,channels) \ +static void \ +resample_interleaved_ ##type##_##channels (GstAudioResampler * resampler, gpointer in[],\ + gsize in_len, gpointer out[], gsize out_len, gsize * consumed, gsize * produced, \ + gboolean move) \ +{ \ + gint di = 0; \ + gint n_taps = resampler->n_taps; \ + gint ostride = resampler->ostride; \ + gint samp_index = 0; \ + gint samp_phase = 0; \ + \ + { \ + type *ip = in[0]; \ + type *op = out[0]; \ + \ + samp_index = resampler->samp_index; \ + samp_phase = resampler->samp_phase; \ + \ + for (di = 0; di < out_len; di++) { \ + Tap *t = &resampler->taps[samp_phase]; \ + type *ipp = &ip[samp_index * channels]; \ + \ + if (t->taps == NULL) \ + make_taps (resampler, t, samp_phase); \ + \ + inner_product_ ##type## _##channels (op, ipp, t->taps, n_taps); \ + \ + op += ostride; \ + samp_phase = t->next_phase; \ + samp_index += t->sample_inc; \ + } \ + if (move) \ + memmove (ip, &ip[samp_index * channels], \ + (in_len - samp_index) * sizeof(type) * channels); \ + } \ + *consumed = samp_index - resampler->samp_index; \ + *produced = di; \ + \ + resampler->samp_index = move ? 0 : samp_index; \ + resampler->samp_phase = samp_phase; \ +} + +MAKE_RESAMPLE_INTERLEAVED_FUNC (gdouble, 2); +MAKE_RESAMPLE_INTERLEAVED_FUNC (gint16, 2); + + +#define MAKE_DEINTERLEAVE_FUNC(type) \ +static void \ +deinterleave_ ##type (GstAudioResampler * resampler, gpointer sbuf[], \ + gpointer in[], gsize in_frames) \ +{ \ + guint i, c, channels = resampler->channels; \ + gsize samples_avail = resampler->samples_avail; \ + for (c = 0; c < channels; c++) { \ + type *s = (type *) sbuf[c] + samples_avail; \ + if (in == NULL) { \ + for (i = 0; i < in_frames; i++) \ + s[i] = 0; \ + } else { \ + type *ip = (type *) in[0] + c; \ + for (i = 0; i < in_frames; i++, ip += channels) \ + s[i] = *ip; \ + } \ + } \ +} + +MAKE_DEINTERLEAVE_FUNC (gdouble); +MAKE_DEINTERLEAVE_FUNC (gfloat); +MAKE_DEINTERLEAVE_FUNC (gint32); +MAKE_DEINTERLEAVE_FUNC (gint16); + +static void +deinterleave_copy (GstAudioResampler * resampler, gpointer sbuf[], + gpointer in[], gsize in_frames) +{ + gsize samples_avail = resampler->samples_avail; + gint bpf = resampler->bpf; + + if (in == NULL) + memset ((guint8 *) sbuf[0] + samples_avail * bpf, 0, in_frames * bpf); + else + memcpy ((guint8 *) sbuf[0] + samples_avail * bpf, in[0], in_frames * bpf); +} + +static void +deinterleave_copy_n (GstAudioResampler * resampler, gpointer sbuf[], + gpointer in[], gsize in_frames) +{ + guint c, channels = resampler->channels; + gsize samples_avail = resampler->samples_avail; + gint bps = resampler->bps; + + for (c = 0; c < channels; c++) { + if (in == NULL) + memset ((guint8 *) sbuf[c] + samples_avail * bps, 0, in_frames * bps); + else + memcpy ((guint8 *) sbuf[c] + samples_avail * bps, in[c], in_frames * bps); + } +} + +/* mirror input samples into the history when we have nothing else */ +#define MAKE_MIRROR_FUNC(type) \ +static void \ +mirror_ ##type (GstAudioResampler * resampler, gpointer sbuf[]) \ +{ \ + guint i, c, channels = resampler->channels; \ + gint si = resampler->n_taps / 2; \ + gint n_taps = resampler->n_taps; \ + for (c = 0; c < channels; c++) { \ + type *s = sbuf[c]; \ + for (i = 0; i < si; i++) \ + s[i] = -s[n_taps - i]; \ + } \ +} + +MAKE_MIRROR_FUNC (gdouble); +MAKE_MIRROR_FUNC (gfloat); +MAKE_MIRROR_FUNC (gint32); +MAKE_MIRROR_FUNC (gint16); + +static void +calculate_kaiser_params (GstAudioResampler * resampler) +{ + gdouble A, B, dw, tr_bw, Fc; + gint n; + const KaiserQualityMap *q = &kaiser_qualities[DEFAULT_QUALITY]; + + /* default cutoff */ + Fc = q->cutoff; + if (resampler->out_rate < resampler->in_rate) + Fc *= q->downsample_cutoff_factor; + + Fc = GET_OPT_CUTOFF (resampler->options, Fc); + A = GET_OPT_STOP_ATTENUATION (resampler->options, q->stopband_attenuation); + tr_bw = + GET_OPT_TRANSITION_BANDWIDTH (resampler->options, + q->transition_bandwidth); + + GST_LOG ("Fc %f, A %f, tr_bw %f", Fc, A, tr_bw); + + /* calculate Beta */ + if (A > 50) + B = 0.1102 * (A - 8.7); + else if (A >= 21) + B = 0.5842 * pow (A - 21, 0.4) + 0.07886 * (A - 21); + else + B = 0.0; + /* calculate transition width in radians */ + dw = 2 * G_PI * (tr_bw); + /* order of the filter */ + n = (A - 8.0) / (2.285 * dw); + + resampler->kaiser_beta = B; + resampler->n_taps = n + 1; + resampler->cutoff = Fc; + + GST_LOG ("using Beta %f n_taps %d cutoff %f", resampler->kaiser_beta, + resampler->n_taps, resampler->cutoff); +} + +static void +resampler_calculate_taps (GstAudioResampler * resampler) +{ + gint bps; + gint j; + gint n_taps; + gint out_rate; + gint in_rate; + + switch (resampler->method) { + case GST_AUDIO_RESAMPLER_METHOD_NEAREST: + resampler->n_taps = 2; + break; + case GST_AUDIO_RESAMPLER_METHOD_LINEAR: + resampler->n_taps = GET_OPT_N_TAPS (resampler->options, 2); + break; + case GST_AUDIO_RESAMPLER_METHOD_CUBIC: + resampler->n_taps = GET_OPT_N_TAPS (resampler->options, 4); + resampler->b = GET_OPT_CUBIC_B (resampler->options); + resampler->c = GET_OPT_CUBIC_C (resampler->options);; + break; + case GST_AUDIO_RESAMPLER_METHOD_BLACKMAN_NUTTALL: + { + const BlackmanQualityMap *q = &blackman_qualities[DEFAULT_QUALITY]; + resampler->n_taps = GET_OPT_N_TAPS (resampler->options, q->n_taps); + resampler->cutoff = GET_OPT_CUTOFF (resampler->options, q->cutoff); + break; + } + case GST_AUDIO_RESAMPLER_METHOD_KAISER: + calculate_kaiser_params (resampler); + break; + } + + in_rate = resampler->in_rate; + out_rate = resampler->out_rate; + + if (out_rate < in_rate) { + resampler->cutoff = resampler->cutoff * out_rate / in_rate; + resampler->n_taps = resampler->n_taps * in_rate / out_rate; + } + /* only round up for bigger taps, the small taps are used for nearest, + * linear and cubic and we want to use less taps for those. */ + if (resampler->n_taps > 4) + resampler->n_taps = GST_ROUND_UP_8 (resampler->n_taps); + + n_taps = resampler->n_taps; + bps = resampler->bps; + + GST_LOG ("using n_taps %d cutoff %f", n_taps, resampler->cutoff); + + resampler->taps = g_realloc_n (resampler->taps, out_rate, sizeof (Tap)); + resampler->coeff = g_realloc_n (resampler->coeff, out_rate, bps * n_taps); + resampler->tmpcoeff = + g_realloc_n (resampler->tmpcoeff, n_taps, sizeof (gdouble)); + + resampler->samp_inc = in_rate / out_rate; + resampler->samp_frac = in_rate % out_rate; + + for (j = 0; j < out_rate; j++) { + Tap *t = &resampler->taps[j]; + t->taps = NULL; + t->sample_inc = (j + in_rate) / out_rate; + t->next_phase = (j + in_rate) % out_rate; + } + + switch (resampler->format) { + case GST_AUDIO_FORMAT_F64: + if (resampler->channels == 2 && n_taps >= 4) { + resampler->resample = resample_interleaved_gdouble_2; + resampler->deinterleave = deinterleave_copy; + } else { + resampler->resample = resample_gdouble; + resampler->deinterleave = deinterleave_gdouble; + } + resampler->mirror = mirror_gdouble; + break; + case GST_AUDIO_FORMAT_F32: + resampler->resample = resample_gfloat; + resampler->deinterleave = deinterleave_gfloat; + resampler->mirror = mirror_gfloat; + break; + case GST_AUDIO_FORMAT_S32: + resampler->resample = resample_gint32; + resampler->deinterleave = deinterleave_gint32; + resampler->mirror = mirror_gint32; + break; + case GST_AUDIO_FORMAT_S16: + if (resampler->channels == 2 && n_taps >= 4) { + resampler->resample = resample_interleaved_gint16_2; + resampler->deinterleave = deinterleave_copy; + } else { + resampler->resample = resample_gint16; + resampler->deinterleave = deinterleave_gint16; + } + resampler->mirror = mirror_gint16; + break; + default: + break; + } + if (resampler->flags & GST_AUDIO_RESAMPLER_FLAG_NON_INTERLEAVED) { + resampler->deinterleave = deinterleave_copy_n; + resampler->ostride = 1; + } else { + resampler->ostride = resampler->channels; + } +} + +#define PRINT_TAPS(type,print) \ +G_STMT_START { \ + type sum = 0.0, *taps; \ + \ + if (t->taps == NULL) \ + make_taps (resampler, t, i); \ + \ + taps = t->taps; \ + for (j = 0; j < n_taps; j++) { \ + type tap = taps[j]; \ + fprintf (stderr, "\t%" print " ", tap); \ + sum += tap; \ + } \ + fprintf (stderr, "\t: sum %" print "\n", sum);\ +} G_STMT_END + +static void +resampler_dump (GstAudioResampler * resampler) +{ +#if 0 + gint i, n_taps, out_rate; + gint64 a; + + out_rate = resampler->out_rate; + n_taps = resampler->n_taps; + + fprintf (stderr, "out size %d, max taps %d\n", out_rate, n_taps); + + a = g_get_monotonic_time (); + + for (i = 0; i < out_rate; i++) { + gint j; + Tap *t = &resampler->taps[i]; + + fprintf (stderr, "%u: %d %d\t ", i, t->sample_inc, t->next_phase); + switch (resampler->format) { + case GST_AUDIO_FORMAT_F64: + PRINT_TAPS (gdouble, "f"); + break; + case GST_AUDIO_FORMAT_F32: + PRINT_TAPS (gfloat, "f"); + break; + case GST_AUDIO_FORMAT_S32: + PRINT_TAPS (gint32, "d"); + break; + case GST_AUDIO_FORMAT_S16: + PRINT_TAPS (gint16, "d"); + break; + default: + break; + } + } + fprintf (stderr, "time %" G_GUINT64_FORMAT "\n", g_get_monotonic_time () - a); +#endif +} + +/** + * gst_audio_resampler_options_set_quality: + * @method: a #GstAudioResamplerMethod + * @quality: the quality + * @in_rate: the input rate + * @out_rate: the output rate + * @options: a #GstStructure + * + * Set the parameters for resampling from @in_rate to @out_rate using @method + * for @quality in @options. + */ +void +gst_audio_resampler_options_set_quality (GstAudioResamplerMethod method, + guint quality, guint in_rate, guint out_rate, GstStructure * options) +{ + g_return_if_fail (options != NULL); + g_return_if_fail (quality < 11); + g_return_if_fail (in_rate != 0 && out_rate != 0); + + switch (method) { + case GST_AUDIO_RESAMPLER_METHOD_NEAREST: + break; + case GST_AUDIO_RESAMPLER_METHOD_LINEAR: + gst_structure_set (options, + GST_AUDIO_RESAMPLER_OPT_N_TAPS, G_TYPE_INT, 2, NULL); + break; + case GST_AUDIO_RESAMPLER_METHOD_CUBIC: + gst_structure_set (options, + GST_AUDIO_RESAMPLER_OPT_N_TAPS, G_TYPE_INT, 4, + GST_AUDIO_RESAMPLER_OPT_CUBIC_B, G_TYPE_DOUBLE, DEFAULT_OPT_CUBIC_B, + GST_AUDIO_RESAMPLER_OPT_CUBIC_C, G_TYPE_DOUBLE, DEFAULT_OPT_CUBIC_C, + NULL); + break; + case GST_AUDIO_RESAMPLER_METHOD_BLACKMAN_NUTTALL: + { + const BlackmanQualityMap *map = &blackman_qualities[quality]; + gst_structure_set (options, + GST_AUDIO_RESAMPLER_OPT_N_TAPS, G_TYPE_INT, map->n_taps, + GST_AUDIO_RESAMPLER_OPT_CUTOFF, G_TYPE_DOUBLE, map->cutoff, NULL); + break; + } + case GST_AUDIO_RESAMPLER_METHOD_KAISER: + { + const KaiserQualityMap *map = &kaiser_qualities[quality]; + gdouble cutoff; + + cutoff = map->cutoff; + if (out_rate < in_rate) + cutoff *= map->downsample_cutoff_factor; + + gst_structure_set (options, + GST_AUDIO_RESAMPLER_OPT_CUTOFF, G_TYPE_DOUBLE, cutoff, + GST_AUDIO_RESAMPLER_OPT_STOP_ATTENUATION, G_TYPE_DOUBLE, + map->stopband_attenuation, + GST_AUDIO_RESAMPLER_OPT_TRANSITION_BANDWIDTH, G_TYPE_DOUBLE, + map->transition_bandwidth, NULL); + break; + } + } +} + +/** + * gst_audio_resampler_new: + * @resampler: a #GstAudioResampler + * @method: a #GstAudioResamplerMethod + * @flags: #GstAudioResamplerFlags + * @in_rate: input rate + * @out_rate: output rate + * @options: extra options + * + * Make a new resampler. + * + * Returns: %TRUE on success + */ +GstAudioResampler * +gst_audio_resampler_new (GstAudioResamplerMethod method, + GstAudioResamplerFlags flags, + GstAudioFormat format, guint channels, + guint in_rate, guint out_rate, GstStructure * options) +{ + GstAudioResampler *resampler; + const GstAudioFormatInfo *info; + + g_return_val_if_fail (in_rate != 0, FALSE); + g_return_val_if_fail (out_rate != 0, FALSE); + + resampler = g_slice_new0 (GstAudioResampler); + resampler->method = method; + resampler->flags = flags; + resampler->format = format; + resampler->channels = channels; + + info = gst_audio_format_get_info (format); + resampler->bps = GST_AUDIO_FORMAT_INFO_WIDTH (info) / 8; + resampler->bpf = resampler->bps * channels; + resampler->sbuf = g_malloc0 (sizeof (gpointer) * channels); + + GST_DEBUG ("method %d, bps %d, bpf %d", method, resampler->bps, + resampler->bpf); + + gst_audio_resampler_update (resampler, in_rate, out_rate, options); + + return resampler; +} + +/** + * gst_audio_resampler_update: + * @resampler: a #GstAudioResampler + * @in_rate: new input rate + * @out_rate: new output rate + * @options: new options or %NULL + * + * Update the resampler parameters for @resampler. This function should + * not be called concurrently with any other function on @resampler. + * + * Returns: %TRUE if the new parameters could be set + */ +gboolean +gst_audio_resampler_update (GstAudioResampler * resampler, + guint in_rate, guint out_rate, GstStructure * options) +{ + gint gcd; + + g_return_val_if_fail (resampler != NULL, FALSE); + g_return_val_if_fail (in_rate != 0, FALSE); + g_return_val_if_fail (out_rate != 0, FALSE); + + gcd = gst_util_greatest_common_divisor (in_rate, out_rate); + in_rate /= gcd; + out_rate /= gcd; + + resampler->in_rate = in_rate; + resampler->out_rate = out_rate; + if (options) { + if (resampler->options) + gst_structure_free (resampler->options); + resampler->options = gst_structure_copy (options); + } + + GST_DEBUG ("%u->%u", in_rate, out_rate); + + resampler_calculate_taps (resampler); + resampler_dump (resampler); + + resampler->filling = TRUE; + resampler->samp_index = 0; + resampler->samp_phase = 0; + resampler->samples_avail = resampler->n_taps / 2 - 1; + + return TRUE; +} + +/** + * gst_audio_resampler_free: + * @resampler: a #GstAudioResampler + * + * Free a previously allocated #GstAudioResampler @resampler. + * + * Since: 1.6 + */ +void +gst_audio_resampler_free (GstAudioResampler * resampler) +{ + g_return_if_fail (resampler != NULL); + + g_free (resampler->taps); + g_free (resampler->coeff); + g_free (resampler->tmpcoeff); + g_free (resampler->samples); + g_free (resampler->sbuf); + if (resampler->options) + gst_structure_free (resampler->options); + g_slice_free (GstAudioResampler, resampler); +} + +static inline gsize +calc_out (GstAudioResampler * resampler, gsize in) +{ + return ((in * resampler->out_rate - + resampler->samp_phase) / resampler->in_rate) + 1; +} + +/** + * gst_audio_resampler_get_out_frames: + * @resampler: a #GstAudioResampler + * @in_frames: number of input frames + * + * Get the number of output frames that would be currently available when + * @in_frames are given to @resampler. + * + * Returns: The number of frames that would be availabe after giving + * @in_frames as input to @resampler. + */ +gsize +gst_audio_resampler_get_out_frames (GstAudioResampler * resampler, + gsize in_frames) +{ + gsize need, avail; + + g_return_val_if_fail (resampler != NULL, 0); + + need = resampler->n_taps + resampler->samp_index + resampler->skip; + avail = resampler->samples_avail + in_frames; + if (avail < need) + return 0; + + return calc_out (resampler, avail - need); +} + +/** + * gst_audio_resampler_get_in_frames: + * @resampler: a #GstAudioResampler + * @out_frames: number of input frames + * + * Get the number of input frames that would currently be needed + * to produce @out_frames from @resampler. + * + * Returns: The number of input frames needed for producing + * @out_frames of data from @resampler. + */ +gsize +gst_audio_resampler_get_in_frames (GstAudioResampler * resampler, + gsize out_frames) +{ + gsize in_frames; + + g_return_val_if_fail (resampler != NULL, 0); + + in_frames = + (resampler->samp_phase + + out_frames * resampler->samp_frac) / resampler->out_rate; + in_frames += out_frames * resampler->samp_inc; + + return in_frames; +} + +/** + * gst_audio_resampler_get_max_latency: + * @resampler: a #GstAudioResampler + * + * Get the maximum number of input samples that the resampler would + * need before producing output. + * + * Returns: the latency of @resampler as expressed in the number of + * frames. + */ +gsize +gst_audio_resampler_get_max_latency (GstAudioResampler * resampler) +{ + g_return_val_if_fail (resampler != NULL, 0); + + return resampler->n_taps / 2; +} + +/* make the buffers to hold the (deinterleaved) samples */ +static inline gpointer * +get_sample_bufs (GstAudioResampler * resampler, gsize need) +{ + if (resampler->samples_len < need) { + guint c, channels = resampler->channels; + GST_LOG ("realloc %d -> %d", (gint) resampler->samples_len, (gint) need); + /* FIXME, move history */ + resampler->samples = g_realloc (resampler->samples, need * resampler->bpf); + resampler->samples_len = need; + /* set up new pointers */ + for (c = 0; c < channels; c++) + resampler->sbuf[c] = + (gint8 *) resampler->samples + + (c * resampler->samples_len * resampler->bps); + } + return resampler->sbuf; +} + +/** + * gst_audio_resampler_resample: + * @resampler: a #GstAudioResampler + * @in: input samples + * @in_frames: number of input frames + * @out: output samples + * @out_frames: maximum output frames + * @consumed: number of frames consumed + * @produced: number of frames produced + * + * Perform resampling on @in_frames frames in @in and write at most + * @out_frames of frames to @out. + * + * In case the samples are interleaved, @in and @out must point to an + * array with a single element pointing to a block of interleaved samples. + * + * If non-interleaved samples are used, @in and @out must point to an + * array with pointers to memory blocks, one for each channel. + * + * @in may be %NULL, in which case @in_frames of 0 samples are pushed + * into the resampler. + * + * The number of frames consumed is returned in @consumed and can be + * less than @in_frames due to latency of the resampler or because + * the number of samples produced equals @out_frames. + * + * The number of frames produced is returned in @produced. + */ +void +gst_audio_resampler_resample (GstAudioResampler * resampler, + gpointer in[], gsize in_frames, gpointer out[], gsize out_frames, + gsize * consumed, gsize * produced) +{ + gsize samples_avail; + gsize out2, need; + gpointer *sbuf; + + /* do sample skipping */ + if (resampler->skip >= in_frames) { + /* we need tp skip all input */ + resampler->skip -= in_frames; + *consumed = in_frames; + *produced = 0; + return; + } + /* skip the last samples by advancing the sample index */ + resampler->samp_index += resampler->skip; + + samples_avail = resampler->samples_avail; + + /* make sure we have enough space to copy our samples */ + sbuf = get_sample_bufs (resampler, in_frames + samples_avail); + + /* copy/deinterleave the samples */ + resampler->deinterleave (resampler, sbuf, in, in_frames); + + /* update new amount of samples in our buffer */ + resampler->samples_avail = samples_avail += in_frames; + + need = resampler->n_taps + resampler->samp_index; + if (samples_avail < need) { + /* not enough samples to start */ + *consumed = in_frames; + *produced = 0; + return; + } + + if (resampler->filling) { + /* if we are filling up our history duplicate the samples to the left */ + resampler->mirror (resampler, sbuf); + resampler->filling = FALSE; + } + + /* calculate maximum number of available output samples */ + out2 = calc_out (resampler, samples_avail - need); + out_frames = MIN (out2, out_frames); + + /* resample all channels */ + resampler->resample (resampler, sbuf, samples_avail, out, out_frames, + consumed, produced, TRUE); + + GST_LOG ("in %" G_GSIZE_FORMAT ", used %" G_GSIZE_FORMAT ", consumed %" + G_GSIZE_FORMAT ", produced %" G_GSIZE_FORMAT, in_frames, samples_avail, + *consumed, *produced); + + /* update pointers */ + if (*consumed > 0) { + gssize left = samples_avail - *consumed; + if (left > 0) { + /* we consumed part of our samples */ + resampler->samples_avail = left; + } else { + /* we consumed all our samples, empty our buffers */ + resampler->samples_avail = 0; + resampler->skip = -left; + } + /* we always consume everything */ + *consumed = in_frames; + } +} diff --git a/gst-libs/gst/audio/audio-resampler.h b/gst-libs/gst/audio/audio-resampler.h new file mode 100644 index 0000000000..78ac999fd2 --- /dev/null +++ b/gst-libs/gst/audio/audio-resampler.h @@ -0,0 +1,188 @@ +/* GStreamer + * Copyright (C) <2015> Wim Taymans + * + * 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 library 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., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef __GST_AUDIO_RESAMPLER_H__ +#define __GST_AUDIO_RESAMPLER_H__ + +#include +#include + +G_BEGIN_DECLS + +typedef struct _GstAudioResampler GstAudioResampler; + +/** + * GST_AUDIO_RESAMPLER_OPT_CUTOFF + * + * G_TYPE_DOUBLE, Cutoff parameter for the filter. 0.940 is the default. + */ +#define GST_AUDIO_RESAMPLER_OPT_CUTOFF "GstAudioResampler.cutoff" +/** + * GST_AUDIO_RESAMPLER_OPT_STOP_ATTENUTATION + * + * G_TYPE_DOUBLE, stopband attenuation in debibels. The attenutation + * after the stopband for the kaiser window. 85 dB is the default. + */ +#define GST_AUDIO_RESAMPLER_OPT_STOP_ATTENUATION "GstAudioResampler.stop-attenutation" +/** + * GST_AUDIO_RESAMPLER_OPT_TRANSITION_BANDWIDTH + * + * G_TYPE_DOUBLE, transition bandwidth. The width of the + * transition band for the kaiser window. 0.087 is the default. + */ +#define GST_AUDIO_RESAMPLER_OPT_TRANSITION_BANDWIDTH "GstAudioResampler.transition-bandwidth" + +/** + * GST_AUDIO_RESAMPLER_OPT_CUBIC_B: + * + * G_TYPE_DOUBLE, B parameter of the cubic filter. + * Values between 0.0 and 2.0 are accepted. 1.0 is the default. + * + * Below are some values of popular filters: + * B C + * Hermite 0.0 0.0 + * Spline 1.0 0.0 + * Catmull-Rom 0.0 1/2 + */ +#define GST_AUDIO_RESAMPLER_OPT_CUBIC_B "GstAudioResampler.cubic-b" +/** + * GST_AUDIO_RESAMPLER_OPT_CUBIC_C: + * + * G_TYPE_DOUBLE, C parameter of the cubic filter. + * Values between 0.0 and 2.0 are accepted. 0.0 is the default. + * + * See #GST_AUDIO_RESAMPLER_OPT_CUBIC_B for some more common values + */ +#define GST_AUDIO_RESAMPLER_OPT_CUBIC_C "GstAudioResampler.cubic-c" + +/** + * GST_AUDIO_RESAMPLER_OPT_N_TAPS: + * + * G_TYPE_INT: the number of taps to use for the filter. + * 0 is the default and selects the taps automatically. + */ +#define GST_AUDIO_RESAMPLER_OPT_N_TAPS "GstAudioResampler.n-taps" + +/** + * GstAudioResamplerFilterMode: + * @GST_AUDIO_RESAMPLER_FILTER_MODE_INTERPOLATED: Use interpolated filter tables. This + * uses less memory but more CPU and is slightly less accurate. + * @GST_AUDIO_RESAMPLER_FILTER_MODE_FULL: Use full filter table. This uses more memory + * but less CPU. + * @GST_AUDIO_RESAMPLER_FILTER_MODE_AUTO: Automatically choose between interpolated + * and full filter tables. + * + * Select for the filter tables should be set up. + */ +typedef enum { + GST_AUDIO_RESAMPLER_FILTER_MODE_INTERPOLATED = (0), + GST_AUDIO_RESAMPLER_FILTER_MODE_FULL, + GST_AUDIO_RESAMPLER_FILTER_MODE_AUTO, +} GstAudioResamplerFilterMode; +/** + * GST_AUDIO_RESAMPLER_OPT_FILTER_MODE: + * + * GST_TYPE_AUDIO_RESAMPLER_FILTER_MODE: how the filter tables should be + * constructed. + * GST_AUDIO_RESAMPLER_FILTER_MODE_AUTO is the default. + */ +#define GST_AUDIO_RESAMPLER_OPT_FILTER_MODE "GstAudioResampler.filter-mode" +/** + * GST_AUDIO_RESAMPLER_OPT_FILTER_MODE_THRESHOLD: + * + * G_TYPE_UINT: the amount of memory to use for full filter tables before + * switching to interpolated filter tables. + * 1048576 is the default. + */ +#define GST_AUDIO_RESAMPLER_OPT_FILTER_MODE_THRESHOLD "GstAudioResampler.filter-mode-threshold" + +/** + * GstAudioResamplerMethod: + * @GST_AUDIO_RESAMPLER_METHOD_NEAREST: Duplicates the samples when + * upsampling and drops when downsampling + * @GST_AUDIO_RESAMPLER_METHOD_LINEAR: Uses linear interpolation to reconstruct + * missing samples and averaging to downsample + * @GST_AUDIO_RESAMPLER_METHOD_CUBIC: Uses cubic interpolation + * @GST_AUDIO_RESAMPLER_METHOD_BLACKMAN_NUTTALL: Uses Blackman-Nuttall windowed sinc interpolation + * @GST_AUDIO_RESAMPLER_METHOD_KAISER: Uses Kaiser windowed sinc interpolation + * + * Different subsampling and upsampling methods + * + * Since: 1.6 + */ +typedef enum { + GST_AUDIO_RESAMPLER_METHOD_NEAREST, + GST_AUDIO_RESAMPLER_METHOD_LINEAR, + GST_AUDIO_RESAMPLER_METHOD_CUBIC, + GST_AUDIO_RESAMPLER_METHOD_BLACKMAN_NUTTALL, + GST_AUDIO_RESAMPLER_METHOD_KAISER +} GstAudioResamplerMethod; + +/** + * GstAudioResamplerFlags: + * @GST_AUDIO_RESAMPLER_FLAG_NONE: no flags + * @GST_AUDIO_RESAMPLER_FLAG_NON_INTERLEAVED: samples are non-interleaved. an array + * of blocks of samples, one for each channel, should be passed to the resample + * function. + * @GST_AUDIO_RESAMPLER_FLAG_VARIABLE_RATE: allow arbitrary sample rate changes. + * + * Different resampler flags. + */ +typedef enum { + GST_AUDIO_RESAMPLER_FLAG_NONE = (0), + GST_AUDIO_RESAMPLER_FLAG_NON_INTERLEAVED = (1 << 0), + GST_AUDIO_RESAMPLER_FLAG_VARIABLE_RATE = (1 << 1), +} GstAudioResamplerFlags; + +#define GST_AUDIO_RESAMPLER_QUALITY_MIN 0 +#define GST_AUDIO_RESAMPLER_QUALITY_MAX 10 +#define GST_AUDIO_RESAMPLER_QUALITY_DEFAULT 4 + +void gst_audio_resampler_options_set_quality (GstAudioResamplerMethod method, + guint quality, + guint in_rate, guint out_rate, + GstStructure *options); + +GstAudioResampler * gst_audio_resampler_new (GstAudioResamplerMethod method, + GstAudioResamplerFlags flags, + GstAudioFormat format, guint channels, + guint in_rate, guint out_rate, + GstStructure *options); +void gst_audio_resampler_free (GstAudioResampler *resampler); + + +gboolean gst_audio_resampler_update (GstAudioResampler *resampler, + guint in_rate, guint out_rate, + GstStructure *options); + +gsize gst_audio_resampler_get_out_frames (GstAudioResampler *resampler, + gsize in_frames); +gsize gst_audio_resampler_get_in_frames (GstAudioResampler *resampler, + gsize out_frames); + +gsize gst_audio_resampler_get_max_latency (GstAudioResampler *resampler); + +void gst_audio_resampler_resample (GstAudioResampler * resampler, + gpointer in[], gsize in_frames, + gpointer out[], gsize out_frames, + gsize *produced, gsize *consumed); + +G_END_DECLS + +#endif /* __GST_AUDIO_RESAMPLER_H__ */ diff --git a/gst-libs/gst/audio/audio.h b/gst-libs/gst/audio/audio.h index 0aa83bdbeb..ae0299ab4e 100644 --- a/gst-libs/gst/audio/audio.h +++ b/gst-libs/gst/audio/audio.h @@ -30,6 +30,7 @@ #include #include #include +#include G_BEGIN_DECLS diff --git a/gst-libs/gst/audio/dbesi0.c b/gst-libs/gst/audio/dbesi0.c new file mode 100644 index 0000000000..958eba7172 --- /dev/null +++ b/gst-libs/gst/audio/dbesi0.c @@ -0,0 +1,147 @@ +/* Copyright(C) 1996 Takuya OOURA + +You may use, copy, modify this code for any purpose and +without fee. + +Package home: http://www.kurims.kyoto-u.ac.jp/~ooura/bessel.html +*/ + +/* Bessel I_0(x) function in double precision */ + +#include + +static double +dbesi0 (double x) +{ + int k; + double w, t, y; + static double a[65] = { + 8.5246820682016865877e-11, 2.5966600546497407288e-9, + 7.9689994568640180274e-8, 1.9906710409667748239e-6, + 4.0312469446528002532e-5, 6.4499871606224265421e-4, + 0.0079012345761930579108, 0.071111111109207045212, + 0.444444444444724909, 1.7777777777777532045, + 4.0000000000000011182, 3.99999999999999998, + 1.0000000000000000001, + 1.1520919130377195927e-10, 2.2287613013610985225e-9, + 8.1903951930694585113e-8, 1.9821560631611544984e-6, + 4.0335461940910133184e-5, 6.4495330974432203401e-4, + 0.0079013012611467520626, 0.071111038160875566622, + 0.44444450319062699316, 1.7777777439146450067, + 4.0000000132337935071, 3.9999999968569015366, + 1.0000000003426703174, + 1.5476870780515238488e-10, 1.2685004214732975355e-9, + 9.2776861851114223267e-8, 1.9063070109379044378e-6, + 4.0698004389917945832e-5, 6.4370447244298070713e-4, + 0.0079044749458444976958, 0.071105052411749363882, + 0.44445280640924755082, 1.7777694934432109713, + 4.0000055808824003386, 3.9999977081165740932, + 1.0000004333949319118, + 2.0675200625006793075e-10, -6.1689554705125681442e-10, + 1.2436765915401571654e-7, 1.5830429403520613423e-6, + 4.2947227560776583326e-5, 6.3249861665073441312e-4, + 0.0079454472840953930811, 0.070994327785661860575, + 0.44467219586283000332, 1.7774588182255374745, + 4.0003038986252717972, 3.9998233869142057195, + 1.0000472932961288324, + 2.7475684794982708655e-10, -3.8991472076521332023e-9, + 1.9730170483976049388e-7, 5.9651531561967674521e-7, + 5.1992971474748995357e-5, 5.7327338675433770752e-4, + 0.0082293143836530412024, 0.069990934858728039037, + 0.44726764292723985087, 1.7726685170014087784, + 4.0062907863712704432, 3.9952750700487845355, + 1.0016354346654179322 + }; + static double b[70] = { + 6.7852367144945531383e-8, 4.6266061382821826854e-7, + 6.9703135812354071774e-6, 7.6637663462953234134e-5, + 7.9113515222612691636e-4, 0.0073401204731103808981, + 0.060677114958668837046, 0.43994941411651569622, + 2.7420017097661750609, 14.289661921740860534, + 59.820609640320710779, 188.78998681199150629, + 399.8731367825601118, 427.56411572180478514, + 1.8042097874891098754e-7, 1.2277164312044637357e-6, + 1.8484393221474274861e-5, 2.0293995900091309208e-4, + 0.0020918539850246207459, 0.019375315654033949297, + 0.15985869016767185908, 1.1565260527420641724, + 7.1896341224206072113, 37.354773811947484532, + 155.80993164266268457, 489.5211371158540918, + 1030.9147225169564806, 1093.5883545113746958, + 4.8017305613187493564e-7, 3.261317843912380074e-6, + 4.9073137508166159639e-5, 5.3806506676487583755e-4, + 0.0055387918291051866561, 0.051223717488786549025, + 0.42190298621367914765, 3.0463625987357355872, + 18.895299447327733204, 97.915189029455461554, + 407.13940115493494659, 1274.3088990480582632, + 2670.9883037012547506, 2815.7166284662544712, + 1.2789926338424623394e-6, 8.6718263067604918916e-6, + 1.3041508821299929489e-4, 0.001428224737372747892, + 0.014684070635768789378, 0.13561403190404185755, + 1.1152592585977393953, 8.0387088559465389038, + 49.761318895895479206, 257.2684232313529138, + 1066.8543146269566231, 3328.3874581009636362, + 6948.8586598121634874, 7288.4893398212481055, + 3.409350368197032893e-6, 2.3079025203103376076e-5, + 3.4691373283901830239e-4, 0.003794994977222908545, + 0.038974209677945602145, 0.3594948380414878371, + 2.9522878893539528226, 21.246564609514287056, + 131.28727387146173141, 677.38107093296675421, + 2802.3724744545046518, 8718.5731420798254081, + 18141.348781638832286, 18948.925349296308859 + }; + static double c[45] = { + 2.5568678676452702768e-15, 3.0393953792305924324e-14, + 6.3343751991094840009e-13, 1.5041298011833009649e-11, + 4.4569436918556541414e-10, 1.746393051427167951e-8, + 1.0059224011079852317e-6, 1.0729838945088577089e-4, + 0.05150322693642527738, + 5.2527963991711562216e-15, 7.202118481421005641e-15, + 7.2561421229904797156e-13, 1.482312146673104251e-11, + 4.4602670450376245434e-10, 1.7463600061788679671e-8, + 1.005922609132234756e-6, 1.0729838937545111487e-4, + 0.051503226936437300716, + 1.3365917359358069908e-14, -1.2932643065888544835e-13, + 1.7450199447905602915e-12, 1.0419051209056979788e-11, + 4.58047881980598326e-10, 1.7442405450073548966e-8, + 1.0059461453281292278e-6, 1.0729837434500161228e-4, + 0.051503226940658446941, + 5.3771611477352308649e-14, -1.1396193006413731702e-12, + 1.2858641335221653409e-11, -5.9802086004570057703e-11, + 7.3666894305929510222e-10, 1.6731837150730356448e-8, + 1.0070831435812128922e-6, 1.0729733111203704813e-4, + 0.051503227360726294675, + 3.7819492084858931093e-14, -4.8600496888588034879e-13, + 1.6898350504817224909e-12, 4.5884624327524255865e-11, + 1.2521615963377513729e-10, 1.8959658437754727957e-8, + 1.0020716710561353622e-6, 1.073037119856927559e-4, + 0.05150322383300230775 + }; + + w = fabs (x); + if (w < 8.5) { + t = w * w * 0.0625; + k = 13 * ((int) t); + y = (((((((((((a[k] * t + a[k + 1]) * t + + a[k + 2]) * t + a[k + 3]) * t + + a[k + 4]) * t + a[k + 5]) * t + a[k + + 6]) * t + a[k + 7]) * t + a[k + 8]) * t + a[k + + 9]) * t + a[k + 10]) * t + a[k + 11]) * t + a[k + 12]; + } else if (w < 12.5) { + k = (int) w; + t = w - k; + k = 14 * (k - 8); + y = ((((((((((((b[k] * t + b[k + 1]) * t + b[k + 2]) * t + b[k + 3]) * t + + b[k + 4]) * t + b[k + 5]) * t + b[k + + 6]) * t + b[k + 7]) * t + b[k + 8]) * t + + b[k + 9]) * t + b[k + 10]) * t + b[k + 11]) * t + b[k + + 12]) * t + b[k + 13]; + } else { + t = 60 / w; + k = 9 * ((int) t); + y = ((((((((c[k] * t + c[k + 1]) * t + + c[k + 2]) * t + c[k + 3]) * t + c[k + 4]) * t + + c[k + 5]) * t + c[k + 6]) * t + c[k + 7]) * t + + c[k + 8]) * sqrt (t) * exp (w); + } + return y; +} diff --git a/gst/audioresample/Makefile.am b/gst/audioresample/Makefile.am index 728700d2f5..b361fd9784 100644 --- a/gst/audioresample/Makefile.am +++ b/gst/audioresample/Makefile.am @@ -8,10 +8,9 @@ ORC_TEST_LIBS = endif libgstaudioresample_la_SOURCES = \ - gstaudioresample.c \ - speex_resampler_int.c \ - speex_resampler_float.c \ - speex_resampler_double.c + gstaudioresample.c + +nodist_libgstaudioresample_la_SOURCES = $(BUILT_SOURCES) libgstaudioresample_la_CFLAGS = \ $(GST_PLUGINS_BASE_CFLAGS) \ @@ -30,15 +29,4 @@ libgstaudioresample_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS) libgstaudioresample_la_LIBTOOLFLAGS = $(GST_PLUGIN_LIBTOOLFLAGS) noinst_HEADERS = \ - arch.h \ - fixed_arm4.h \ - fixed_arm5e.h \ - fixed_bfin.h \ - fixed_debug.h \ - fixed_generic.h \ - gstaudioresample.h \ - resample.c \ - resample_sse.h \ - resample_neon.h \ - speex_resampler.h \ - speex_resampler_wrapper.h + gstaudioresample.h diff --git a/gst/audioresample/arch.h b/gst/audioresample/arch.h deleted file mode 100644 index 4e77e6e1c2..0000000000 --- a/gst/audioresample/arch.h +++ /dev/null @@ -1,266 +0,0 @@ -/* Copyright (C) 2003 Jean-Marc Valin */ -/** - @file arch.h - @brief Various architecture definitions Speex -*/ -/* - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - - Neither the name of the Xiph.org Foundation nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef ARCH_H -#define ARCH_H - -#ifndef SPEEX_VERSION -#define SPEEX_MAJOR_VERSION 1 /**< Major Speex version. */ -#define SPEEX_MINOR_VERSION 1 /**< Minor Speex version. */ -#define SPEEX_MICRO_VERSION 15 /**< Micro Speex version. */ -#define SPEEX_EXTRA_VERSION "" /**< Extra Speex version. */ -#define SPEEX_VERSION "speex-1.2beta3" /**< Speex version string. */ -#endif - -/* A couple test to catch stupid option combinations */ -#ifdef FIXED_POINT - -#ifdef FLOATING_POINT -#error You cannot compile as floating point and fixed point at the same time -#endif -#ifdef _USE_SSE -#error SSE is only for floating-point -#endif -#if ((defined (ARM4_ASM)||defined (ARM4_ASM)) && defined(BFIN_ASM)) || (defined (ARM4_ASM)&&defined(ARM5E_ASM)) -#error Make up your mind. What CPU do you have? -#endif -#ifdef VORBIS_PSYCHO -#error Vorbis-psy model currently not implemented in fixed-point -#endif - -#else - -#ifndef FLOATING_POINT -#error You now need to define either FIXED_POINT or FLOATING_POINT -#endif -#if defined (ARM4_ASM) || defined(ARM5E_ASM) || defined(BFIN_ASM) -#error I suppose you can have a [ARM4/ARM5E/Blackfin] that has float instructions? -#endif -#ifdef FIXED_POINT_DEBUG -#error "Don't you think enabling fixed-point is a good thing to do if you want to debug that?" -#endif - - -#endif - -#ifndef OUTSIDE_SPEEX -#include "../include/speex/speex_types.h" -#endif - -#ifndef ABS -#define ABS(x) ((x) < 0 ? (-(x)) : (x)) /**< Absolute integer value. */ -#endif - -#define ABS16(x) ((x) < 0 ? (-(x)) : (x)) /**< Absolute 16-bit value. */ -#define MIN16(a,b) ((a) < (b) ? (a) : (b)) /**< Maximum 16-bit value. */ -#define MAX16(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum 16-bit value. */ -#define ABS32(x) ((x) < 0 ? (-(x)) : (x)) /**< Absolute 32-bit value. */ -#define MIN32(a,b) ((a) < (b) ? (a) : (b)) /**< Maximum 32-bit value. */ -#define MAX32(a,b) ((a) > (b) ? (a) : (b)) /**< Maximum 32-bit value. */ - -#ifdef FIXED_POINT - -typedef spx_int16_t spx_word16_t; -typedef spx_int32_t spx_word32_t; -typedef spx_word32_t spx_mem_t; -typedef spx_word16_t spx_coef_t; -typedef spx_word16_t spx_lsp_t; -typedef spx_word32_t spx_sig_t; - -#define Q15ONE 32767 - -#define LPC_SCALING 8192 -#define SIG_SCALING 16384 -#define LSP_SCALING 8192. -#define GAMMA_SCALING 32768. -#define GAIN_SCALING 64 -#define GAIN_SCALING_1 0.015625 - -#define LPC_SHIFT 13 -#define LSP_SHIFT 13 -#define SIG_SHIFT 14 -#define GAIN_SHIFT 6 - -#define VERY_SMALL 0 -#define VERY_LARGE32 ((spx_word32_t)2147483647) -#define VERY_LARGE16 ((spx_word16_t)32767) -#define Q15_ONE ((spx_word16_t)32767) - - -#ifdef FIXED_DEBUG -#include "fixed_debug.h" -#else - -#include "fixed_generic.h" - -#ifdef ARM5E_ASM -#include "fixed_arm5e.h" -#elif defined (ARM4_ASM) -#include "fixed_arm4.h" -#elif defined (BFIN_ASM) -#include "fixed_bfin.h" -#endif - -#endif - - -#else - -#ifdef DOUBLE_PRECISION -typedef double spx_mem_t; -typedef double spx_coef_t; -typedef double spx_lsp_t; -typedef double spx_sig_t; -typedef double spx_word16_t; -typedef double spx_word32_t; - -#define Q15ONE 1.0 -#define LPC_SCALING 1. -#define SIG_SCALING 1. -#define LSP_SCALING 1. -#define GAMMA_SCALING 1. -#define GAIN_SCALING 1. -#define GAIN_SCALING_1 1. - - -#define VERY_SMALL 1e-20 -#define VERY_LARGE32 1e20 -#define VERY_LARGE16 1e20 -#define Q15_ONE ((spx_word16_t)1.) -#else /* !DOUBLE_PRECISION */ -typedef float spx_mem_t; -typedef float spx_coef_t; -typedef float spx_lsp_t; -typedef float spx_sig_t; -typedef float spx_word16_t; -typedef float spx_word32_t; - -#define Q15ONE 1.0f -#define LPC_SCALING 1.f -#define SIG_SCALING 1.f -#define LSP_SCALING 1.f -#define GAMMA_SCALING 1.f -#define GAIN_SCALING 1.f -#define GAIN_SCALING_1 1.f - - -#define VERY_SMALL 1e-15f -#define VERY_LARGE32 1e15f -#define VERY_LARGE16 1e15f -#define Q15_ONE ((spx_word16_t)1.f) -#endif /* DOUBLE_PRECISION */ - -#define QCONST16(x,bits) (x) -#define QCONST32(x,bits) (x) - -#define NEG16(x) (-(x)) -#define NEG32(x) (-(x)) -#define EXTRACT16(x) (x) -#define EXTEND32(x) (x) -#define SHR16(a,shift) (a) -#define SHL16(a,shift) (a) -#define SHR32(a,shift) (a) -#define SHL32(a,shift) (a) -#define PSHR16(a,shift) (a) -#define PSHR32(a,shift) (a) -#define VSHR32(a,shift) (a) -#define SATURATE16(x,a) (x) -#define SATURATE32(x,a) (x) -#define SATURATE32PSHR(x,shift,a) (x) - -#define PSHR(a,shift) (a) -#define SHR(a,shift) (a) -#define SHL(a,shift) (a) -#define SATURATE(x,a) (x) - -#define ADD16(a,b) ((a)+(b)) -#define SUB16(a,b) ((a)-(b)) -#define ADD32(a,b) ((a)+(b)) -#define SUB32(a,b) ((a)-(b)) -#define MULT16_16_16(a,b) ((a)*(b)) -#define MULT16_16(a,b) ((spx_word32_t)(a)*(spx_word32_t)(b)) -#define MAC16_16(c,a,b) ((c)+(spx_word32_t)(a)*(spx_word32_t)(b)) - -#define MULT16_32_Q11(a,b) ((a)*(b)) -#define MULT16_32_Q13(a,b) ((a)*(b)) -#define MULT16_32_Q14(a,b) ((a)*(b)) -#define MULT16_32_Q15(a,b) ((a)*(b)) -#define MULT16_32_P15(a,b) ((a)*(b)) - -#define MAC16_32_Q11(c,a,b) ((c)+(a)*(b)) -#define MAC16_32_Q15(c,a,b) ((c)+(a)*(b)) - -#define MAC16_16_Q11(c,a,b) ((c)+(a)*(b)) -#define MAC16_16_Q13(c,a,b) ((c)+(a)*(b)) -#define MAC16_16_P13(c,a,b) ((c)+(a)*(b)) -#define MULT16_16_Q11_32(a,b) ((a)*(b)) -#define MULT16_16_Q13(a,b) ((a)*(b)) -#define MULT16_16_Q14(a,b) ((a)*(b)) -#define MULT16_16_Q15(a,b) ((a)*(b)) -#define MULT16_16_P15(a,b) ((a)*(b)) -#define MULT16_16_P13(a,b) ((a)*(b)) -#define MULT16_16_P14(a,b) ((a)*(b)) - -#define DIV32_16(a,b) (((spx_word32_t)(a))/(spx_word16_t)(b)) -#define PDIV32_16(a,b) (((spx_word32_t)(a))/(spx_word16_t)(b)) -#define DIV32(a,b) (((spx_word32_t)(a))/(spx_word32_t)(b)) -#define PDIV32(a,b) (((spx_word32_t)(a))/(spx_word32_t)(b)) - - -#endif - - -#if defined (CONFIG_TI_C54X) || defined (CONFIG_TI_C55X) - -/* 2 on TI C5x DSP */ -#define BYTES_PER_CHAR 2 -#define BITS_PER_CHAR 16 -#define LOG2_BITS_PER_CHAR 4 - -#else - -#define BYTES_PER_CHAR 1 -#define BITS_PER_CHAR 8 -#define LOG2_BITS_PER_CHAR 3 - -#endif - - - -#ifdef FIXED_DEBUG -extern long long spx_mips; -#endif - - -#endif diff --git a/gst/audioresample/fixed_arm4.h b/gst/audioresample/fixed_arm4.h deleted file mode 100644 index b6981cae72..0000000000 --- a/gst/audioresample/fixed_arm4.h +++ /dev/null @@ -1,148 +0,0 @@ -/* Copyright (C) 2004 Jean-Marc Valin */ -/** - @file fixed_arm4.h - @brief ARM4 fixed-point operations -*/ -/* - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - - Neither the name of the Xiph.org Foundation nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FIXED_ARM4_H -#define FIXED_ARM4_H - -#undef MULT16_32_Q14 -static inline spx_word32_t MULT16_32_Q14(spx_word16_t x, spx_word32_t y) { - int res; - int dummy; - asm ( - "smull %0,%1,%2,%3 \n\t" - "mov %0, %0, lsr #14 \n\t" - "add %0, %0, %1, lsl #18 \n\t" - : "=&r"(res), "=&r" (dummy) - : "r"(y),"r"((int)x)); - return(res); -} - -#undef MULT16_32_Q15 -static inline spx_word32_t MULT16_32_Q15(spx_word16_t x, spx_word32_t y) { - int res; - int dummy; - asm ( - "smull %0,%1,%2,%3 \n\t" - "mov %0, %0, lsr #15 \n\t" - "add %0, %0, %1, lsl #17 \n\t" - : "=&r"(res), "=&r" (dummy) - : "r"(y),"r"((int)x)); - return(res); -} - -#undef DIV32_16 -static inline short DIV32_16(int a, int b) -{ - int res=0; - int dead1, dead2, dead3, dead4, dead5; - __asm__ __volatile__ ( - "\teor %5, %0, %1\n" - "\tmovs %4, %0\n" - "\trsbmi %0, %0, #0 \n" - "\tmovs %4, %1\n" - "\trsbmi %1, %1, #0 \n" - "\tmov %4, #1\n" - - "\tsubs %3, %0, %1, asl #14 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #14 \n" - - "\tsubs %3, %0, %1, asl #13 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #13 \n" - - "\tsubs %3, %0, %1, asl #12 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #12 \n" - - "\tsubs %3, %0, %1, asl #11 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #11 \n" - - "\tsubs %3, %0, %1, asl #10 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #10 \n" - - "\tsubs %3, %0, %1, asl #9 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #9 \n" - - "\tsubs %3, %0, %1, asl #8 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #8 \n" - - "\tsubs %3, %0, %1, asl #7 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #7 \n" - - "\tsubs %3, %0, %1, asl #6 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #6 \n" - - "\tsubs %3, %0, %1, asl #5 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #5 \n" - - "\tsubs %3, %0, %1, asl #4 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #4 \n" - - "\tsubs %3, %0, %1, asl #3 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #3 \n" - - "\tsubs %3, %0, %1, asl #2 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #2 \n" - - "\tsubs %3, %0, %1, asl #1 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4, asl #1 \n" - - "\tsubs %3, %0, %1 \n" - "\tmovpl %0, %3 \n" - "\torrpl %2, %2, %4 \n" - - "\tmovs %5, %5, lsr #31 \n" - "\trsbne %2, %2, #0 \n" - : "=r" (dead1), "=r" (dead2), "=r" (res), - "=r" (dead3), "=r" (dead4), "=r" (dead5) - : "0" (a), "1" (b), "2" (res) - : "cc" - ); - return res; -} - - -#endif diff --git a/gst/audioresample/fixed_arm5e.h b/gst/audioresample/fixed_arm5e.h deleted file mode 100644 index 9b4861c9a7..0000000000 --- a/gst/audioresample/fixed_arm5e.h +++ /dev/null @@ -1,178 +0,0 @@ -/* Copyright (C) 2003 Jean-Marc Valin */ -/** - @file fixed_arm5e.h - @brief ARM-tuned fixed-point operations -*/ -/* - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - - Neither the name of the Xiph.org Foundation nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FIXED_ARM5E_H -#define FIXED_ARM5E_H - -#undef MULT16_16 -static inline spx_word32_t MULT16_16(spx_word16_t x, spx_word16_t y) { - int res; - asm ("smulbb %0,%1,%2;\n" - : "=&r"(res) - : "%r"(x),"r"(y)); - return(res); -} - -#undef MAC16_16 -static inline spx_word32_t MAC16_16(spx_word32_t a, spx_word16_t x, spx_word32_t y) { - int res; - asm ("smlabb %0,%1,%2,%3;\n" - : "=&r"(res) - : "%r"(x),"r"(y),"r"(a)); - return(res); -} - -#undef MULT16_32_Q15 -static inline spx_word32_t MULT16_32_Q15(spx_word16_t x, spx_word32_t y) { - int res; - asm ("smulwb %0,%1,%2;\n" - : "=&r"(res) - : "%r"(y<<1),"r"(x)); - return(res); -} - -#undef MAC16_32_Q15 -static inline spx_word32_t MAC16_32_Q15(spx_word32_t a, spx_word16_t x, spx_word32_t y) { - int res; - asm ("smlawb %0,%1,%2,%3;\n" - : "=&r"(res) - : "%r"(y<<1),"r"(x),"r"(a)); - return(res); -} - -#undef MULT16_32_Q11 -static inline spx_word32_t MULT16_32_Q11(spx_word16_t x, spx_word32_t y) { - int res; - asm ("smulwb %0,%1,%2;\n" - : "=&r"(res) - : "%r"(y<<5),"r"(x)); - return(res); -} - -#undef MAC16_32_Q11 -static inline spx_word32_t MAC16_32_Q11(spx_word32_t a, spx_word16_t x, spx_word32_t y) { - int res; - asm ("smlawb %0,%1,%2,%3;\n" - : "=&r"(res) - : "%r"(y<<5),"r"(x),"r"(a)); - return(res); -} - -#undef DIV32_16 -static inline short DIV32_16(int a, int b) -{ - int res=0; - int dead1, dead2, dead3, dead4, dead5; - __asm__ __volatile__ ( - "\teor %5, %0, %1\n" - "\tmovs %4, %0\n" - "\trsbmi %0, %0, #0 \n" - "\tmovs %4, %1\n" - "\trsbmi %1, %1, #0 \n" - "\tmov %4, #1\n" - - "\tsubs %3, %0, %1, asl #14 \n" - "\torrpl %2, %2, %4, asl #14 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #13 \n" - "\torrpl %2, %2, %4, asl #13 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #12 \n" - "\torrpl %2, %2, %4, asl #12 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #11 \n" - "\torrpl %2, %2, %4, asl #11 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #10 \n" - "\torrpl %2, %2, %4, asl #10 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #9 \n" - "\torrpl %2, %2, %4, asl #9 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #8 \n" - "\torrpl %2, %2, %4, asl #8 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #7 \n" - "\torrpl %2, %2, %4, asl #7 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #6 \n" - "\torrpl %2, %2, %4, asl #6 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #5 \n" - "\torrpl %2, %2, %4, asl #5 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #4 \n" - "\torrpl %2, %2, %4, asl #4 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #3 \n" - "\torrpl %2, %2, %4, asl #3 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #2 \n" - "\torrpl %2, %2, %4, asl #2 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1, asl #1 \n" - "\torrpl %2, %2, %4, asl #1 \n" - "\tmovpl %0, %3 \n" - - "\tsubs %3, %0, %1 \n" - "\torrpl %2, %2, %4 \n" - "\tmovpl %0, %3 \n" - - "\tmovs %5, %5, lsr #31 \n" - "\trsbne %2, %2, #0 \n" - : "=r" (dead1), "=r" (dead2), "=r" (res), - "=r" (dead3), "=r" (dead4), "=r" (dead5) - : "0" (a), "1" (b), "2" (res) - : "memory", "cc" - ); - return res; -} - - - - -#endif diff --git a/gst/audioresample/fixed_bfin.h b/gst/audioresample/fixed_bfin.h deleted file mode 100644 index 9eb21e3396..0000000000 --- a/gst/audioresample/fixed_bfin.h +++ /dev/null @@ -1,176 +0,0 @@ -/* Copyright (C) 2005 Analog Devices - Author: Jean-Marc Valin */ -/** - @file fixed_bfin.h - @brief Blackfin fixed-point operations -*/ -/* - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - - Neither the name of the Xiph.org Foundation nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FIXED_BFIN_H -#define FIXED_BFIN_H - -#include "bfin.h" - -#undef PDIV32_16 -static inline spx_word16_t PDIV32_16(spx_word32_t a, spx_word16_t b) -{ - spx_word32_t res, bb; - bb = b; - a += b>>1; - __asm__ ( - "P0 = 15;\n\t" - "R0 = %1;\n\t" - "R1 = %2;\n\t" - //"R0 = R0 + R1;\n\t" - "R0 <<= 1;\n\t" - "DIVS (R0, R1);\n\t" - "LOOP divide%= LC0 = P0;\n\t" - "LOOP_BEGIN divide%=;\n\t" - "DIVQ (R0, R1);\n\t" - "LOOP_END divide%=;\n\t" - "R0 = R0.L;\n\t" - "%0 = R0;\n\t" - : "=m" (res) - : "m" (a), "m" (bb) - : "P0", "R0", "R1", "ASTAT" BFIN_HWLOOP0_REGS); - return res; -} - -#undef DIV32_16 -static inline spx_word16_t DIV32_16(spx_word32_t a, spx_word16_t b) -{ - spx_word32_t res, bb; - bb = b; - /* Make the roundinf consistent with the C version - (do we need to do that?)*/ - if (a<0) - a += (b-1); - __asm__ ( - "P0 = 15;\n\t" - "R0 = %1;\n\t" - "R1 = %2;\n\t" - "R0 <<= 1;\n\t" - "DIVS (R0, R1);\n\t" - "LOOP divide%= LC0 = P0;\n\t" - "LOOP_BEGIN divide%=;\n\t" - "DIVQ (R0, R1);\n\t" - "LOOP_END divide%=;\n\t" - "R0 = R0.L;\n\t" - "%0 = R0;\n\t" - : "=m" (res) - : "m" (a), "m" (bb) - : "P0", "R0", "R1", "ASTAT" BFIN_HWLOOP0_REGS); - return res; -} - -#undef MAX16 -static inline spx_word16_t MAX16(spx_word16_t a, spx_word16_t b) -{ - spx_word32_t res; - __asm__ ( - "%1 = %1.L (X);\n\t" - "%2 = %2.L (X);\n\t" - "%0 = MAX(%1,%2);" - : "=d" (res) - : "%d" (a), "d" (b) - : "ASTAT" - ); - return res; -} - -#undef MULT16_32_Q15 -static inline spx_word32_t MULT16_32_Q15(spx_word16_t a, spx_word32_t b) -{ - spx_word32_t res; - __asm__ - ( - "A1 = %2.L*%1.L (M);\n\t" - "A1 = A1 >>> 15;\n\t" - "%0 = (A1 += %2.L*%1.H) ;\n\t" - : "=&W" (res), "=&d" (b) - : "d" (a), "1" (b) - : "A1", "ASTAT" - ); - return res; -} - -#undef MAC16_32_Q15 -static inline spx_word32_t MAC16_32_Q15(spx_word32_t c, spx_word16_t a, spx_word32_t b) -{ - spx_word32_t res; - __asm__ - ( - "A1 = %2.L*%1.L (M);\n\t" - "A1 = A1 >>> 15;\n\t" - "%0 = (A1 += %2.L*%1.H);\n\t" - "%0 = %0 + %4;\n\t" - : "=&W" (res), "=&d" (b) - : "d" (a), "1" (b), "d" (c) - : "A1", "ASTAT" - ); - return res; -} - -#undef MULT16_32_Q14 -static inline spx_word32_t MULT16_32_Q14(spx_word16_t a, spx_word32_t b) -{ - spx_word32_t res; - __asm__ - ( - "%2 <<= 1;\n\t" - "A1 = %1.L*%2.L (M);\n\t" - "A1 = A1 >>> 15;\n\t" - "%0 = (A1 += %1.L*%2.H);\n\t" - : "=W" (res), "=d" (a), "=d" (b) - : "1" (a), "2" (b) - : "A1", "ASTAT" - ); - return res; -} - -#undef MAC16_32_Q14 -static inline spx_word32_t MAC16_32_Q14(spx_word32_t c, spx_word16_t a, spx_word32_t b) -{ - spx_word32_t res; - __asm__ - ( - "%1 <<= 1;\n\t" - "A1 = %2.L*%1.L (M);\n\t" - "A1 = A1 >>> 15;\n\t" - "%0 = (A1 += %2.L*%1.H);\n\t" - "%0 = %0 + %4;\n\t" - : "=&W" (res), "=&d" (b) - : "d" (a), "1" (b), "d" (c) - : "A1", "ASTAT" - ); - return res; -} - -#endif diff --git a/gst/audioresample/fixed_debug.h b/gst/audioresample/fixed_debug.h deleted file mode 100644 index 54f3866e8f..0000000000 --- a/gst/audioresample/fixed_debug.h +++ /dev/null @@ -1,487 +0,0 @@ -/* Copyright (C) 2003 Jean-Marc Valin */ -/** - @file fixed_debug.h - @brief Fixed-point operations with debugging -*/ -/* - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - - Neither the name of the Xiph.org Foundation nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FIXED_DEBUG_H -#define FIXED_DEBUG_H - -#include - -extern long long spx_mips; -#define MIPS_INC spx_mips++, - -#define QCONST16(x,bits) ((spx_word16_t)(.5+(x)*(((spx_word32_t)1)<<(bits)))) -#define QCONST32(x,bits) ((spx_word32_t)(.5+(x)*(((spx_word32_t)1)<<(bits)))) - - -#define VERIFY_SHORT(x) ((x)<=32767&&(x)>=-32768) -#define VERIFY_INT(x) ((x)<=2147483647LL&&(x)>=-2147483648LL) - -static inline short NEG16(int x) -{ - int res; - if (!VERIFY_SHORT(x)) - { - fprintf (stderr, "NEG16: input is not short: %d\n", (int)x); - } - res = -x; - if (!VERIFY_SHORT(res)) - fprintf (stderr, "NEG16: output is not short: %d\n", (int)res); - spx_mips++; - return res; -} -static inline int NEG32(long long x) -{ - long long res; - if (!VERIFY_INT(x)) - { - fprintf (stderr, "NEG16: input is not int: %d\n", (int)x); - } - res = -x; - if (!VERIFY_INT(res)) - fprintf (stderr, "NEG16: output is not int: %d\n", (int)res); - spx_mips++; - return res; -} - -#define EXTRACT16(x) _EXTRACT16(x, __FILE__, __LINE__) -static inline short _EXTRACT16(int x, char *file, int line) -{ - int res; - if (!VERIFY_SHORT(x)) - { - fprintf (stderr, "EXTRACT16: input is not short: %d in %s: line %d\n", x, file, line); - } - res = x; - spx_mips++; - return res; -} - -#define EXTEND32(x) _EXTEND32(x, __FILE__, __LINE__) -static inline int _EXTEND32(int x, char *file, int line) -{ - int res; - if (!VERIFY_SHORT(x)) - { - fprintf (stderr, "EXTEND32: input is not short: %d in %s: line %d\n", x, file, line); - } - res = x; - spx_mips++; - return res; -} - -#define SHR16(a, shift) _SHR16(a, shift, __FILE__, __LINE__) -static inline short _SHR16(int a, int shift, char *file, int line) -{ - int res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift)) - { - fprintf (stderr, "SHR16: inputs are not short: %d >> %d in %s: line %d\n", a, shift, file, line); - } - res = a>>shift; - if (!VERIFY_SHORT(res)) - fprintf (stderr, "SHR16: output is not short: %d in %s: line %d\n", res, file, line); - spx_mips++; - return res; -} -#define SHL16(a, shift) _SHL16(a, shift, __FILE__, __LINE__) -static inline short _SHL16(int a, int shift, char *file, int line) -{ - int res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(shift)) - { - fprintf (stderr, "SHL16: inputs are not short: %d %d in %s: line %d\n", a, shift, file, line); - } - res = a<>shift; - if (!VERIFY_INT(res)) - { - fprintf (stderr, "SHR32: output is not int: %d\n", (int)res); - } - spx_mips++; - return res; -} -static inline int SHL32(long long a, int shift) -{ - long long res; - if (!VERIFY_INT(a) || !VERIFY_SHORT(shift)) - { - fprintf (stderr, "SHL32: inputs are not int: %d %d\n", (int)a, shift); - } - res = a<>1))),shift)) -#define PSHR32(a,shift) (SHR32(ADD32((a),((EXTEND32(1)<<((shift))>>1))),shift)) -#define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift))) - -#define SATURATE16(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) -#define SATURATE32(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) - -//#define SHR(a,shift) ((a) >> (shift)) -//#define SHL(a,shift) ((a) << (shift)) - -#define ADD16(a, b) _ADD16(a, b, __FILE__, __LINE__) -static inline short _ADD16(int a, int b, char *file, int line) -{ - int res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "ADD16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line); - } - res = a+b; - if (!VERIFY_SHORT(res)) - { - fprintf (stderr, "ADD16: output is not short: %d+%d=%d in %s: line %d\n", a,b,res, file, line); - } - spx_mips++; - return res; -} - -#define SUB16(a, b) _SUB16(a, b, __FILE__, __LINE__) -static inline short _SUB16(int a, int b, char *file, int line) -{ - int res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "SUB16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line); - } - res = a-b; - if (!VERIFY_SHORT(res)) - fprintf (stderr, "SUB16: output is not short: %d in %s: line %d\n", res, file, line); - spx_mips++; - return res; -} - -#define ADD32(a, b) _ADD32(a, b, __FILE__, __LINE__) -static inline int _ADD32(long long a, long long b, char *file, int line) -{ - long long res; - if (!VERIFY_INT(a) || !VERIFY_INT(b)) - { - fprintf (stderr, "ADD32: inputs are not int: %d %d in %s: line %d\n", (int)a, (int)b, file, line); - } - res = a+b; - if (!VERIFY_INT(res)) - { - fprintf (stderr, "ADD32: output is not int: %d in %s: line %d\n", (int)res, file, line); - } - spx_mips++; - return res; -} - -static inline int SUB32(long long a, long long b) -{ - long long res; - if (!VERIFY_INT(a) || !VERIFY_INT(b)) - { - fprintf (stderr, "SUB32: inputs are not int: %d %d\n", (int)a, (int)b); - } - res = a-b; - if (!VERIFY_INT(res)) - fprintf (stderr, "SUB32: output is not int: %d\n", (int)res); - spx_mips++; - return res; -} - -#define ADD64(a,b) (MIPS_INC(a)+(b)) - -/* result fits in 16 bits */ -static inline short MULT16_16_16(int a, int b) -{ - int res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "MULT16_16_16: inputs are not short: %d %d\n", a, b); - } - res = a*b; - if (!VERIFY_SHORT(res)) - fprintf (stderr, "MULT16_16_16: output is not short: %d\n", res); - spx_mips++; - return res; -} - -#define MULT16_16(a, b) _MULT16_16(a, b, __FILE__, __LINE__) -static inline int _MULT16_16(int a, int b, char *file, int line) -{ - long long res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "MULT16_16: inputs are not short: %d %d in %s: line %d\n", a, b, file, line); - } - res = ((long long)a)*b; - if (!VERIFY_INT(res)) - fprintf (stderr, "MULT16_16: output is not int: %d in %s: line %d\n", (int)res, file, line); - spx_mips++; - return res; -} - -#define MAC16_16(c,a,b) (spx_mips--,ADD32((c),MULT16_16((a),(b)))) -#define MAC16_16_Q11(c,a,b) (EXTRACT16(ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),11))))) -#define MAC16_16_Q13(c,a,b) (EXTRACT16(ADD16((c),EXTRACT16(SHR32(MULT16_16((a),(b)),13))))) -#define MAC16_16_P13(c,a,b) (EXTRACT16(ADD32((c),SHR32(ADD32(4096,MULT16_16((a),(b))),13)))) - - -#define MULT16_32_QX(a, b, Q) _MULT16_32_QX(a, b, Q, __FILE__, __LINE__) -static inline int _MULT16_32_QX(int a, long long b, int Q, char *file, int line) -{ - long long res; - if (!VERIFY_SHORT(a) || !VERIFY_INT(b)) - { - fprintf (stderr, "MULT16_32_Q%d: inputs are not short+int: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line); - } - if (ABS32(b)>=(EXTEND32(1)<<(15+Q))) - fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d in %s: line %d\n", Q, (int)a, (int)b, file, line); - res = (((long long)a)*(long long)b) >> Q; - if (!VERIFY_INT(res)) - fprintf (stderr, "MULT16_32_Q%d: output is not int: %d*%d=%d in %s: line %d\n", Q, (int)a, (int)b,(int)res, file, line); - spx_mips+=5; - return res; -} - -static inline int MULT16_32_PX(int a, long long b, int Q) -{ - long long res; - if (!VERIFY_SHORT(a) || !VERIFY_INT(b)) - { - fprintf (stderr, "MULT16_32_P%d: inputs are not short+int: %d %d\n", Q, (int)a, (int)b); - } - if (ABS32(b)>=(EXTEND32(1)<<(15+Q))) - fprintf (stderr, "MULT16_32_Q%d: second operand too large: %d %d\n", Q, (int)a, (int)b); - res = ((((long long)a)*(long long)b) + ((EXTEND32(1)<>1))>> Q; - if (!VERIFY_INT(res)) - fprintf (stderr, "MULT16_32_P%d: output is not int: %d*%d=%d\n", Q, (int)a, (int)b,(int)res); - spx_mips+=5; - return res; -} - - -#define MULT16_32_Q11(a,b) MULT16_32_QX(a,b,11) -#define MAC16_32_Q11(c,a,b) ADD32((c),MULT16_32_Q11((a),(b))) -#define MULT16_32_Q12(a,b) MULT16_32_QX(a,b,12) -#define MULT16_32_Q13(a,b) MULT16_32_QX(a,b,13) -#define MULT16_32_Q14(a,b) MULT16_32_QX(a,b,14) -#define MULT16_32_Q15(a,b) MULT16_32_QX(a,b,15) -#define MULT16_32_P15(a,b) MULT16_32_PX(a,b,15) -#define MAC16_32_Q15(c,a,b) ADD32((c),MULT16_32_Q15((a),(b))) - -static inline int SATURATE(int a, int b) -{ - if (a>b) - a=b; - if (a<-b) - a = -b; - return a; -} - -static inline int MULT16_16_Q11_32(int a, int b) -{ - long long res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "MULT16_16_Q11: inputs are not short: %d %d\n", a, b); - } - res = ((long long)a)*b; - res >>= 11; - if (!VERIFY_INT(res)) - fprintf (stderr, "MULT16_16_Q11: output is not short: %d*%d=%d\n", (int)a, (int)b, (int)res); - spx_mips+=3; - return res; -} -static inline short MULT16_16_Q13(int a, int b) -{ - long long res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "MULT16_16_Q13: inputs are not short: %d %d\n", a, b); - } - res = ((long long)a)*b; - res >>= 13; - if (!VERIFY_SHORT(res)) - fprintf (stderr, "MULT16_16_Q13: output is not short: %d*%d=%d\n", a, b, (int)res); - spx_mips+=3; - return res; -} -static inline short MULT16_16_Q14(int a, int b) -{ - long long res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "MULT16_16_Q14: inputs are not short: %d %d\n", a, b); - } - res = ((long long)a)*b; - res >>= 14; - if (!VERIFY_SHORT(res)) - fprintf (stderr, "MULT16_16_Q14: output is not short: %d\n", (int)res); - spx_mips+=3; - return res; -} -static inline short MULT16_16_Q15(int a, int b) -{ - long long res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "MULT16_16_Q15: inputs are not short: %d %d\n", a, b); - } - res = ((long long)a)*b; - res >>= 15; - if (!VERIFY_SHORT(res)) - { - fprintf (stderr, "MULT16_16_Q15: output is not short: %d\n", (int)res); - } - spx_mips+=3; - return res; -} - -static inline short MULT16_16_P13(int a, int b) -{ - long long res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "MULT16_16_P13: inputs are not short: %d %d\n", a, b); - } - res = ((long long)a)*b; - res += 4096; - if (!VERIFY_INT(res)) - fprintf (stderr, "MULT16_16_P13: overflow: %d*%d=%d\n", a, b, (int)res); - res >>= 13; - if (!VERIFY_SHORT(res)) - fprintf (stderr, "MULT16_16_P13: output is not short: %d*%d=%d\n", a, b, (int)res); - spx_mips+=4; - return res; -} -static inline short MULT16_16_P14(int a, int b) -{ - long long res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "MULT16_16_P14: inputs are not short: %d %d\n", a, b); - } - res = ((long long)a)*b; - res += 8192; - if (!VERIFY_INT(res)) - fprintf (stderr, "MULT16_16_P14: overflow: %d*%d=%d\n", a, b, (int)res); - res >>= 14; - if (!VERIFY_SHORT(res)) - fprintf (stderr, "MULT16_16_P14: output is not short: %d*%d=%d\n", a, b, (int)res); - spx_mips+=4; - return res; -} -static inline short MULT16_16_P15(int a, int b) -{ - long long res; - if (!VERIFY_SHORT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "MULT16_16_P15: inputs are not short: %d %d\n", a, b); - } - res = ((long long)a)*b; - res += 16384; - if (!VERIFY_INT(res)) - fprintf (stderr, "MULT16_16_P15: overflow: %d*%d=%d\n", a, b, (int)res); - res >>= 15; - if (!VERIFY_SHORT(res)) - fprintf (stderr, "MULT16_16_P15: output is not short: %d*%d=%d\n", a, b, (int)res); - spx_mips+=4; - return res; -} - -#define DIV32_16(a, b) _DIV32_16(a, b, __FILE__, __LINE__) - -static inline int _DIV32_16(long long a, long long b, char *file, int line) -{ - long long res; - if (b==0) - { - fprintf(stderr, "DIV32_16: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line); - return 0; - } - if (!VERIFY_INT(a) || !VERIFY_SHORT(b)) - { - fprintf (stderr, "DIV32_16: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line); - } - res = a/b; - if (!VERIFY_SHORT(res)) - { - fprintf (stderr, "DIV32_16: output is not short: %d / %d = %d in %s: line %d\n", (int)a,(int)b,(int)res, file, line); - if (res>32767) - res = 32767; - if (res<-32768) - res = -32768; - } - spx_mips+=20; - return res; -} - -#define DIV32(a, b) _DIV32(a, b, __FILE__, __LINE__) -static inline int _DIV32(long long a, long long b, char *file, int line) -{ - long long res; - if (b==0) - { - fprintf(stderr, "DIV32: divide by zero: %d/%d in %s: line %d\n", (int)a, (int)b, file, line); - return 0; - } - - if (!VERIFY_INT(a) || !VERIFY_INT(b)) - { - fprintf (stderr, "DIV32: inputs are not int/short: %d %d in %s: line %d\n", (int)a, (int)b, file, line); - } - res = a/b; - if (!VERIFY_INT(res)) - fprintf (stderr, "DIV32: output is not int: %d in %s: line %d\n", (int)res, file, line); - spx_mips+=36; - return res; -} -#define PDIV32(a,b) DIV32(ADD32((a),(b)>>1),b) -#define PDIV32_16(a,b) DIV32_16(ADD32((a),(b)>>1),b) - -#endif diff --git a/gst/audioresample/fixed_generic.h b/gst/audioresample/fixed_generic.h deleted file mode 100644 index 6991352159..0000000000 --- a/gst/audioresample/fixed_generic.h +++ /dev/null @@ -1,110 +0,0 @@ -/* Copyright (C) 2003 Jean-Marc Valin */ -/** - @file fixed_generic.h - @brief Generic fixed-point operations -*/ -/* - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - - Neither the name of the Xiph.org Foundation nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifndef FIXED_GENERIC_H -#define FIXED_GENERIC_H - -#define QCONST16(x,bits) ((spx_word16_t)(.5+(x)*(((spx_word32_t)1)<<(bits)))) -#define QCONST32(x,bits) ((spx_word32_t)(.5+(x)*(((spx_word32_t)1)<<(bits)))) - -#define NEG16(x) (-(x)) -#define NEG32(x) (-(x)) -#define EXTRACT16(x) ((spx_word16_t)(x)) -#define EXTEND32(x) ((spx_word32_t)(x)) -#define SHR16(a,shift) ((a) >> (shift)) -#define SHL16(a,shift) ((a) << (shift)) -#define SHR32(a,shift) ((a) >> (shift)) -#define SHL32(a,shift) ((a) << (shift)) -#define PSHR16(a,shift) (SHR16((a)+((1<<((shift))>>1)),shift)) -#define PSHR32(a,shift) (SHR32((a)+((EXTEND32(1)<<((shift))>>1)),shift)) -#define VSHR32(a, shift) (((shift)>0) ? SHR32(a, shift) : SHL32(a, -(shift))) -#define SATURATE16(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) -#define SATURATE32(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) - -#define SATURATE32PSHR(x,shift,a) (((x)>=(SHL32(a,shift))) ? (a) : \ - (x)<=-(SHL32(a,shift)) ? -(a) : \ - (PSHR32(x, shift))) - -#define SHR(a,shift) ((a) >> (shift)) -#define SHL(a,shift) ((spx_word32_t)(a) << (shift)) -#define PSHR(a,shift) (SHR((a)+((EXTEND32(1)<<((shift))>>1)),shift)) -#define SATURATE(x,a) (((x)>(a) ? (a) : (x)<-(a) ? -(a) : (x))) - - -#define ADD16(a,b) ((spx_word16_t)((spx_word16_t)(a)+(spx_word16_t)(b))) -#define SUB16(a,b) ((spx_word16_t)(a)-(spx_word16_t)(b)) -#define ADD32(a,b) ((spx_word32_t)(a)+(spx_word32_t)(b)) -#define SUB32(a,b) ((spx_word32_t)(a)-(spx_word32_t)(b)) - - -/* result fits in 16 bits */ -#define MULT16_16_16(a,b) ((((spx_word16_t)(a))*((spx_word16_t)(b)))) - -/* (spx_word32_t)(spx_word16_t) gives TI compiler a hint that it's 16x16->32 multiply */ -#define MULT16_16(a,b) (((spx_word32_t)(spx_word16_t)(a))*((spx_word32_t)(spx_word16_t)(b))) - -#define MAC16_16(c,a,b) (ADD32((c),MULT16_16((a),(b)))) -#define MULT16_32_Q12(a,b) ADD32(MULT16_16((a),SHR((b),12)), SHR(MULT16_16((a),((b)&0x00000fff)),12)) -#define MULT16_32_Q13(a,b) ADD32(MULT16_16((a),SHR((b),13)), SHR(MULT16_16((a),((b)&0x00001fff)),13)) -#define MULT16_32_Q14(a,b) ADD32(MULT16_16((a),SHR((b),14)), SHR(MULT16_16((a),((b)&0x00003fff)),14)) - -#define MULT16_32_Q11(a,b) ADD32(MULT16_16((a),SHR((b),11)), SHR(MULT16_16((a),((b)&0x000007ff)),11)) -#define MAC16_32_Q11(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),11)), SHR(MULT16_16((a),((b)&0x000007ff)),11))) - -#define MULT16_32_P15(a,b) ADD32(MULT16_16((a),SHR((b),15)), PSHR(MULT16_16((a),((b)&0x00007fff)),15)) -#define MULT16_32_Q15(a,b) ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15)) -#define MAC16_32_Q15(c,a,b) ADD32(c,ADD32(MULT16_16((a),SHR((b),15)), SHR(MULT16_16((a),((b)&0x00007fff)),15))) - - -#define MAC16_16_Q11(c,a,b) (ADD32((c),SHR(MULT16_16((a),(b)),11))) -#define MAC16_16_Q13(c,a,b) (ADD32((c),SHR(MULT16_16((a),(b)),13))) -#define MAC16_16_P13(c,a,b) (ADD32((c),SHR(ADD32(4096,MULT16_16((a),(b))),13))) - -#define MULT16_16_Q11_32(a,b) (SHR(MULT16_16((a),(b)),11)) -#define MULT16_16_Q13(a,b) (SHR(MULT16_16((a),(b)),13)) -#define MULT16_16_Q14(a,b) (SHR(MULT16_16((a),(b)),14)) -#define MULT16_16_Q15(a,b) (SHR(MULT16_16((a),(b)),15)) - -#define MULT16_16_P13(a,b) (SHR(ADD32(4096,MULT16_16((a),(b))),13)) -#define MULT16_16_P14(a,b) (SHR(ADD32(8192,MULT16_16((a),(b))),14)) -#define MULT16_16_P15(a,b) (SHR(ADD32(16384,MULT16_16((a),(b))),15)) - -#define MUL_16_32_R15(a,bh,bl) ADD32(MULT16_16((a),(bh)), SHR(MULT16_16((a),(bl)),15)) - -#define DIV32_16(a,b) ((spx_word16_t)(((spx_word32_t)(a))/((spx_word16_t)(b)))) -#define PDIV32_16(a,b) ((spx_word16_t)(((spx_word32_t)(a)+((spx_word16_t)(b)>>1))/((spx_word16_t)(b)))) -#define DIV32(a,b) (((spx_word32_t)(a))/((spx_word32_t)(b))) -#define PDIV32(a,b) (((spx_word32_t)(a)+((spx_word16_t)(b)>>1))/((spx_word32_t)(b))) - -#endif diff --git a/gst/audioresample/gstaudioresample.c b/gst/audioresample/gstaudioresample.c index 9f66096bfe..c29acc1336 100644 --- a/gst/audioresample/gstaudioresample.c +++ b/gst/audioresample/gstaudioresample.c @@ -71,7 +71,11 @@ GST_DEBUG_CATEGORY (audio_resample_debug); GST_DEBUG_CATEGORY_STATIC (GST_CAT_PERFORMANCE); #endif -#define GST_TYPE_SPEEX_RESAMPLER_SINC_FILTER_MODE (speex_resampler_sinc_filter_mode_get_type ()) +#undef USE_SPEEX + +#define DEFAULT_QUALITY GST_AUDIO_RESAMPLER_QUALITY_DEFAULT +#define DEFAULT_SINC_FILTER_MODE GST_AUDIO_RESAMPLER_FILTER_MODE_AUTO +#define DEFAULT_SINC_FILTER_AUTO_THRESHOLD (1*1048576) enum { @@ -83,11 +87,11 @@ enum #if G_BYTE_ORDER == G_LITTLE_ENDIAN #define SUPPORTED_CAPS \ - GST_AUDIO_CAPS_MAKE ("{ F32LE, F64LE, S32LE, S24LE, S16LE, S8 }") \ + GST_AUDIO_CAPS_MAKE ("{ F32LE, F64LE, S32LE, S16LE }") \ ", layout = (string) { interleaved, non-interleaved }" #else #define SUPPORTED_CAPS \ - GST_AUDIO_CAPS_MAKE ("{ F32BE, F64BE, S32BE, S24BE, S16BE, S8 }") \ + GST_AUDIO_CAPS_MAKE ("{ F32BE, F64BE, S32BE, S16BE }") \ ", layout = (string) { interleaved, non-interleaved }" #endif @@ -117,8 +121,6 @@ static void gst_audio_resample_set_property (GObject * object, static void gst_audio_resample_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec); -static GType speex_resampler_sinc_filter_mode_get_type (void); - /* vmethods */ static gboolean gst_audio_resample_get_unit_size (GstBaseTransform * base, GstCaps * caps, gsize * size); @@ -159,15 +161,15 @@ gst_audio_resample_class_init (GstAudioResampleClass * klass) g_object_class_install_property (gobject_class, PROP_QUALITY, g_param_spec_int ("quality", "Quality", "Resample quality with 0 being " "the lowest and 10 being the best", - SPEEX_RESAMPLER_QUALITY_MIN, SPEEX_RESAMPLER_QUALITY_MAX, - SPEEX_RESAMPLER_QUALITY_DEFAULT, + GST_AUDIO_RESAMPLER_QUALITY_MIN, GST_AUDIO_RESAMPLER_QUALITY_MAX, + DEFAULT_QUALITY, G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (gobject_class, PROP_SINC_FILTER_MODE, g_param_spec_enum ("sinc-filter-mode", "Sinc filter table mode", "What sinc filter table mode to use", - GST_TYPE_SPEEX_RESAMPLER_SINC_FILTER_MODE, - SPEEX_RESAMPLER_SINC_FILTER_DEFAULT, + GST_TYPE_AUDIO_RESAMPLER_FILTER_MODE, + DEFAULT_SINC_FILTER_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); g_object_class_install_property (gobject_class, @@ -175,7 +177,7 @@ gst_audio_resample_class_init (GstAudioResampleClass * klass) g_param_spec_uint ("sinc-filter-auto-threshold", "Sinc filter auto mode threshold", "Memory usage threshold to use if sinc filter mode is AUTO, given in bytes", - 0, G_MAXUINT, SPEEX_RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT, + 0, G_MAXUINT, DEFAULT_SINC_FILTER_AUTO_THRESHOLD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); gst_element_class_add_static_pad_template (gstelement_class, @@ -218,10 +220,10 @@ gst_audio_resample_init (GstAudioResample * resample) { GstBaseTransform *trans = GST_BASE_TRANSFORM (resample); - resample->quality = SPEEX_RESAMPLER_QUALITY_DEFAULT; - resample->sinc_filter_mode = SPEEX_RESAMPLER_SINC_FILTER_DEFAULT; - resample->sinc_filter_auto_threshold = - SPEEX_RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT; + resample->method = GST_AUDIO_RESAMPLER_METHOD_KAISER; + resample->quality = DEFAULT_QUALITY; + resample->sinc_filter_mode = DEFAULT_SINC_FILTER_MODE; + resample->sinc_filter_auto_threshold = DEFAULT_SINC_FILTER_AUTO_THRESHOLD; gst_base_transform_set_gap_aware (trans, TRUE); gst_pad_set_query_function (trans->srcpad, gst_audio_resample_query); @@ -243,11 +245,6 @@ gst_audio_resample_start (GstBaseTransform * base) resample->samples_in = 0; resample->samples_out = 0; - resample->tmp_in = NULL; - resample->tmp_in_size = 0; - resample->tmp_out = NULL; - resample->tmp_out_size = 0; - return TRUE; } @@ -256,21 +253,10 @@ gst_audio_resample_stop (GstBaseTransform * base) { GstAudioResample *resample = GST_AUDIO_RESAMPLE (base); - if (resample->state) { - resample->funcs->destroy (resample->state); - resample->state = NULL; + if (resample->resamp) { + gst_audio_resampler_free (resample->resamp); + resample->resamp = NULL; } - - resample->funcs = NULL; - - g_free (resample->tmp_in); - resample->tmp_in = NULL; - resample->tmp_in_size = 0; - - g_free (resample->tmp_out); - resample->tmp_out = NULL; - resample->tmp_out_size = 0; - return TRUE; } @@ -370,121 +356,92 @@ gst_audio_resample_fixate_caps (GstBaseTransform * base, return othercaps; } -static const SpeexResampleFuncs * -gst_audio_resample_get_funcs (gint width, gboolean fp) +static GstStructure * +make_options (GstAudioResample * resample, GstAudioInfo * in, + GstAudioInfo * out) { - const SpeexResampleFuncs *funcs = NULL; + GstStructure *options; - if (gst_audio_resample_use_int && (width == 8 || width == 16) && !fp) - funcs = &int_funcs; - else if ((!gst_audio_resample_use_int && (width == 8 || width == 16) && !fp) - || (width == 32 && fp)) - funcs = &float_funcs; - else if ((width == 64 && fp) || ((width == 32 || width == 24) && !fp)) - funcs = &double_funcs; - else - g_assert_not_reached (); + options = gst_structure_new_empty ("resampler-options"); + gst_audio_resampler_options_set_quality (resample->method, + resample->quality, in->rate, out->rate, options); - return funcs; -} + gst_structure_set (options, + GST_AUDIO_RESAMPLER_OPT_FILTER_MODE, GST_TYPE_AUDIO_RESAMPLER_FILTER_MODE, + resample->sinc_filter_mode, GST_AUDIO_RESAMPLER_OPT_FILTER_MODE_THRESHOLD, + G_TYPE_UINT, resample->sinc_filter_auto_threshold, NULL); -static SpeexResamplerState * -gst_audio_resample_init_state (GstAudioResample * resample, gint width, - gint channels, gint inrate, gint outrate, gint quality, gboolean fp, - SpeexResamplerSincFilterMode sinc_filter_mode, - guint32 sinc_filter_auto_threshold) -{ - SpeexResamplerState *ret = NULL; - gint err = RESAMPLER_ERR_SUCCESS; - const SpeexResampleFuncs *funcs = gst_audio_resample_get_funcs (width, fp); - - ret = funcs->init (channels, inrate, outrate, quality, - sinc_filter_mode, sinc_filter_auto_threshold, &err); - - if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) { - GST_ERROR_OBJECT (resample, "Failed to create resampler state: %s", - funcs->strerror (err)); - return NULL; - } - - if (sinc_filter_mode == SPEEX_RESAMPLER_SINC_FILTER_AUTO) { - GST_INFO_OBJECT (resample, "Using the %s sinc filter table", - funcs->get_sinc_filter_mode (ret) ? "full" : "interpolated"); - } - - funcs->skip_zeros (ret); - - return ret; + return options; } static gboolean -gst_audio_resample_update_state (GstAudioResample * resample, gint width, - gint channels, gint inrate, gint outrate, gint quality, gboolean fp, - SpeexResamplerSincFilterMode sinc_filter_mode, - guint32 sinc_filter_auto_threshold) +gst_audio_resample_update_state (GstAudioResample * resample, GstAudioInfo * in, + GstAudioInfo * out) { - gboolean ret = TRUE; gboolean updated_latency = FALSE; + gsize old_latency = -1; + GstStructure *options; - updated_latency = (resample->inrate != inrate - || quality != resample->quality) && resample->state != NULL; + if (resample->resamp == NULL && in == NULL && out == NULL) + return TRUE; - if (resample->state == NULL) { - ret = TRUE; - } else if (resample->channels != channels || fp != resample->fp - || width != resample->width - || sinc_filter_mode != resample->sinc_filter_mode - || sinc_filter_auto_threshold != resample->sinc_filter_auto_threshold) { - resample->funcs->destroy (resample->state); - resample->state = - gst_audio_resample_init_state (resample, width, channels, inrate, - outrate, quality, fp, sinc_filter_mode, sinc_filter_auto_threshold); + options = make_options (resample, in, out); - resample->funcs = gst_audio_resample_get_funcs (width, fp); - ret = (resample->state != NULL); - } else if (resample->inrate != inrate || resample->outrate != outrate) { - gint err = RESAMPLER_ERR_SUCCESS; + if (resample->resamp) + old_latency = gst_audio_resampler_get_max_latency (resample->resamp); - err = resample->funcs->set_rate (resample->state, inrate, outrate); - - if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) - GST_ERROR_OBJECT (resample, "Failed to update rate: %s", - resample->funcs->strerror (err)); - - ret = (err == RESAMPLER_ERR_SUCCESS); - } else if (quality != resample->quality) { - gint err = RESAMPLER_ERR_SUCCESS; - - err = resample->funcs->set_quality (resample->state, quality); - - if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) - GST_ERROR_OBJECT (resample, "Failed to update quality: %s", - resample->funcs->strerror (err)); - - ret = (err == RESAMPLER_ERR_SUCCESS); + /* if channels and layout changed, destroy existing resampler */ + if ((in->finfo != resample->in.finfo || + in->channels != resample->in.channels || + in->layout != resample->in.layout) && resample->resamp) { + gst_audio_resampler_free (resample->resamp); + resample->resamp = NULL; } + if (resample->resamp == NULL) { + GstAudioResamplerFlags flags = 0; - resample->width = width; - resample->channels = channels; - resample->fp = fp; - resample->quality = quality; - resample->inrate = inrate; - resample->outrate = outrate; - resample->sinc_filter_mode = sinc_filter_mode; - resample->sinc_filter_auto_threshold = sinc_filter_auto_threshold; + if (in->layout == GST_AUDIO_LAYOUT_NON_INTERLEAVED) + flags |= GST_AUDIO_RESAMPLER_FLAG_NON_INTERLEAVED; + + resample->resamp = gst_audio_resampler_new (resample->method, + flags, in->finfo->format, in->channels, in->rate, out->rate, options); + if (resample->resamp == NULL) + goto resampler_failed; + } else { + gboolean ret; + + ret = + gst_audio_resampler_update (resample->resamp, in->rate, out->rate, + options); + if (!ret) + goto update_failed; + } + if (old_latency != -1) + updated_latency = + old_latency != gst_audio_resampler_get_max_latency (resample->resamp); if (updated_latency) gst_element_post_message (GST_ELEMENT (resample), gst_message_new_latency (GST_OBJECT (resample))); - return ret; + return TRUE; + + /* ERRORS */ +resampler_failed: + { + GST_ERROR_OBJECT (resample, "failed to create resampler"); + return FALSE; + } +update_failed: + { + GST_ERROR_OBJECT (resample, "failed to update resampler"); + return FALSE; + } } static void gst_audio_resample_reset_state (GstAudioResample * resample) { - if (resample->state) - resample->funcs->reset_mem (resample->state); } static gint @@ -556,9 +513,6 @@ static gboolean gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps, GstCaps * outcaps) { - gboolean ret; - gint width, inrate, outrate, channels; - gboolean fp; GstAudioResample *resample = GST_AUDIO_RESAMPLE (base); GstAudioInfo in, out; @@ -571,21 +525,10 @@ gst_audio_resample_set_caps (GstBaseTransform * base, GstCaps * incaps, goto invalid_outcaps; /* FIXME do some checks */ + gst_audio_resample_update_state (resample, &in, &out); - /* take new values */ - width = GST_AUDIO_FORMAT_INFO_WIDTH (in.finfo); - channels = GST_AUDIO_INFO_CHANNELS (&in); - inrate = GST_AUDIO_INFO_RATE (&in); - outrate = GST_AUDIO_INFO_RATE (&out); - fp = GST_AUDIO_FORMAT_INFO_IS_FLOAT (in.finfo); - - ret = - gst_audio_resample_update_state (resample, width, channels, inrate, - outrate, resample->quality, fp, resample->sinc_filter_mode, - resample->sinc_filter_auto_threshold); - - if (G_UNLIKELY (!ret)) - return FALSE; + resample->in = in; + resample->out = out; return TRUE; @@ -602,192 +545,18 @@ invalid_outcaps: } } -#define GST_MAXINT24 (8388607) -#define GST_MININT24 (-8388608) - -#if (G_BYTE_ORDER == G_LITTLE_ENDIAN) -#define GST_READ_UINT24 GST_READ_UINT24_LE -#define GST_WRITE_UINT24 GST_WRITE_UINT24_LE -#else -#define GST_READ_UINT24 GST_READ_UINT24_BE -#define GST_WRITE_UINT24 GST_WRITE_UINT24_BE -#endif - -static void -gst_audio_resample_convert_buffer (GstAudioResample * resample, - const guint8 * in, guint8 * out, guint len, gboolean inverse) -{ - len *= resample->channels; - - if (inverse) { - if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) { - gint8 *o = (gint8 *) out; - gint16 *i = (gint16 *) in; - gint32 tmp; - - while (len) { - tmp = *i + (G_MAXINT8 >> 1); - *o = CLAMP (tmp >> 8, G_MININT8, G_MAXINT8); - o++; - i++; - len--; - } - } else if (!gst_audio_resample_use_int && resample->width == 8 - && !resample->fp) { - gint8 *o = (gint8 *) out; - gfloat *i = (gfloat *) in; - gfloat tmp; - - while (len) { - tmp = *i; - *o = (gint8) CLAMP (tmp * G_MAXINT8 + 0.5, G_MININT8, G_MAXINT8); - o++; - i++; - len--; - } - } else if (!gst_audio_resample_use_int && resample->width == 16 - && !resample->fp) { - gint16 *o = (gint16 *) out; - gfloat *i = (gfloat *) in; - gfloat tmp; - - while (len) { - tmp = *i; - *o = (gint16) CLAMP (tmp * G_MAXINT16 + 0.5, G_MININT16, G_MAXINT16); - o++; - i++; - len--; - } - } else if (resample->width == 24 && !resample->fp) { - guint8 *o = (guint8 *) out; - gdouble *i = (gdouble *) in; - gdouble tmp; - - while (len) { - tmp = *i; - GST_WRITE_UINT24 (o, (gint32) CLAMP (tmp * GST_MAXINT24 + 0.5, - GST_MININT24, GST_MAXINT24)); - o += 3; - i++; - len--; - } - } else if (resample->width == 32 && !resample->fp) { - gint32 *o = (gint32 *) out; - gdouble *i = (gdouble *) in; - gdouble tmp; - - while (len) { - tmp = *i; - *o = (gint32) CLAMP (tmp * G_MAXINT32 + 0.5, G_MININT32, G_MAXINT32); - o++; - i++; - len--; - } - } else { - g_assert_not_reached (); - } - } else { - if (gst_audio_resample_use_int && resample->width == 8 && !resample->fp) { - gint8 *i = (gint8 *) in; - gint16 *o = (gint16 *) out; - gint32 tmp; - - while (len) { - tmp = *i; - *o = tmp << 8; - o++; - i++; - len--; - } - } else if (!gst_audio_resample_use_int && resample->width == 8 - && !resample->fp) { - gint8 *i = (gint8 *) in; - gfloat *o = (gfloat *) out; - gfloat tmp; - - while (len) { - tmp = *i; - *o = tmp / G_MAXINT8; - o++; - i++; - len--; - } - } else if (!gst_audio_resample_use_int && resample->width == 16 - && !resample->fp) { - gint16 *i = (gint16 *) in; - gfloat *o = (gfloat *) out; - gfloat tmp; - - while (len) { - tmp = *i; - *o = tmp / G_MAXINT16; - o++; - i++; - len--; - } - } else if (resample->width == 24 && !resample->fp) { - guint8 *i = (guint8 *) in; - gdouble *o = (gdouble *) out; - gdouble tmp; - guint32 tmp2; - - while (len) { - tmp2 = GST_READ_UINT24 (i); - if (tmp2 & 0x00800000) - tmp2 |= 0xff000000; - tmp = (gint32) tmp2; - *o = tmp / GST_MAXINT24; - o++; - i += 3; - len--; - } - } else if (resample->width == 32 && !resample->fp) { - gint32 *i = (gint32 *) in; - gdouble *o = (gdouble *) out; - gdouble tmp; - - while (len) { - tmp = *i; - *o = tmp / G_MAXINT32; - o++; - i++; - len--; - } - } else { - g_assert_not_reached (); - } - } -} - -static guint8 * -gst_audio_resample_workspace_realloc (guint8 ** workspace, guint * size, - guint new_size) -{ - guint8 *new; - if (new_size <= *size) - /* no need to resize */ - return *workspace; - new = g_realloc (*workspace, new_size); - if (!new) - /* failure (re)allocating memeory */ - return NULL; - /* success */ - *workspace = new; - *size = new_size; - return *workspace; -} - /* Push history_len zeros into the filter, but discard the output. */ static void gst_audio_resample_dump_drain (GstAudioResample * resample, guint history_len) { +#if 0 gint outsize; guint in_len G_GNUC_UNUSED, in_processed; guint out_len, out_processed; guint num, den; gpointer buf; - g_assert (resample->state != NULL); + g_assert (resample->resamp != NULL); resample->funcs->get_ratio (resample->state, &num, &den); @@ -805,6 +574,7 @@ gst_audio_resample_dump_drain (GstAudioResample * resample, guint history_len) g_free (buf); g_assert (in_len == in_processed); +#endif } static void @@ -813,77 +583,43 @@ gst_audio_resample_push_drain (GstAudioResample * resample, guint history_len) GstBuffer *outbuf; GstFlowReturn res; gint outsize; - guint in_len, in_processed; - guint out_len, out_processed; - gint err; - guint num, den; + gsize in_processed; + gsize out_len, out_processed; GstMapInfo map; + gpointer out[1]; - g_assert (resample->state != NULL); + g_assert (resample->resamp != NULL); /* Don't drain samples if we were reset. */ if (!GST_CLOCK_TIME_IS_VALID (resample->t0)) return; - resample->funcs->get_ratio (resample->state, &num, &den); - - in_len = in_processed = history_len; - out_len = out_processed = - gst_util_uint64_scale_int_ceil (history_len, den, num); - outsize = out_len * resample->channels * (resample->width / 8); - + out_len = gst_audio_resampler_get_out_frames (resample->resamp, history_len); if (out_len == 0) return; + outsize = out_len * resample->in.bpf; outbuf = gst_buffer_new_and_alloc (outsize); gst_buffer_map (outbuf, &map, GST_MAP_WRITE); - if (resample->funcs->width != resample->width) { - /* need to convert data format; allocate workspace */ - if (!gst_audio_resample_workspace_realloc (&resample->tmp_out, - &resample->tmp_out_size, (resample->funcs->width / 8) * out_len * - resample->channels)) { - GST_ERROR_OBJECT (resample, "failed to allocate workspace"); - return; - } - - /* process */ - err = resample->funcs->process (resample->state, NULL, &in_processed, - resample->tmp_out, &out_processed); - - /* convert output format */ - gst_audio_resample_convert_buffer (resample, resample->tmp_out, - map.data, out_processed, TRUE); - } else { - /* don't need to convert data format; process */ - err = resample->funcs->process (resample->state, NULL, &in_processed, - map.data, &out_processed); - } + out[0] = map.data; + gst_audio_resampler_resample (resample->resamp, NULL, history_len, + out, out_len, &in_processed, &out_processed); /* If we wrote more than allocated something is really wrong now * and we should better abort immediately */ - g_assert (out_len >= out_processed); - - outsize = out_processed * resample->channels * (resample->width / 8); + g_assert (out_len == out_processed); gst_buffer_unmap (outbuf, &map); - gst_buffer_resize (outbuf, 0, outsize); - - if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) { - GST_WARNING_OBJECT (resample, "Failed to process drain: %s", - resample->funcs->strerror (err)); - gst_buffer_unref (outbuf); - return; - } /* time */ if (GST_CLOCK_TIME_IS_VALID (resample->t0)) { GST_BUFFER_TIMESTAMP (outbuf) = resample->t0 + gst_util_uint64_scale_int_round (resample->samples_out, GST_SECOND, - resample->outrate); + resample->out.rate); GST_BUFFER_DURATION (outbuf) = resample->t0 + gst_util_uint64_scale_int_round (resample->samples_out + out_processed, - GST_SECOND, resample->outrate) - GST_BUFFER_TIMESTAMP (outbuf); + GST_SECOND, resample->out.rate) - GST_BUFFER_TIMESTAMP (outbuf); } else { GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE; GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE; @@ -900,12 +636,6 @@ gst_audio_resample_push_drain (GstAudioResample * resample, guint history_len) resample->samples_out += out_processed; resample->samples_in += history_len; - if (G_UNLIKELY (out_processed == 0 && in_len * den > num)) { - GST_WARNING_OBJECT (resample, "Failed to get drain, dropping buffer"); - gst_buffer_unref (outbuf); - return; - } - GST_LOG_OBJECT (resample, "Pushing drain buffer of %u bytes with timestamp %" GST_TIME_FORMAT " duration %" GST_TIME_FORMAT " offset %" G_GUINT64_FORMAT " offset_end %" @@ -931,8 +661,10 @@ gst_audio_resample_sink_event (GstBaseTransform * base, GstEvent * event) switch (GST_EVENT_TYPE (event)) { case GST_EVENT_FLUSH_STOP: gst_audio_resample_reset_state (resample); - if (resample->state) - resample->funcs->skip_zeros (resample->state); +#if 0 + if (resample->resamp) + resample->funcs->skip_zeros (resample->resamp); +#endif resample->num_gap_samples = 0; resample->num_nongap_samples = 0; resample->t0 = GST_CLOCK_TIME_NONE; @@ -943,13 +675,17 @@ gst_audio_resample_sink_event (GstBaseTransform * base, GstEvent * event) resample->need_discont = TRUE; break; case GST_EVENT_SEGMENT: - if (resample->state) { - guint latency = resample->funcs->get_input_latency (resample->state); +#if 0 + if (resample->resamp) { + guint latency = resample->funcs->get_input_latency (resample->resamp); gst_audio_resample_push_drain (resample, latency); } +#endif gst_audio_resample_reset_state (resample); - if (resample->state) - resample->funcs->skip_zeros (resample->state); +#if 0 + if (resample->resamp) + resample->funcs->skip_zeros (resample->resamp); +#endif resample->num_gap_samples = 0; resample->num_nongap_samples = 0; resample->t0 = GST_CLOCK_TIME_NONE; @@ -960,10 +696,12 @@ gst_audio_resample_sink_event (GstBaseTransform * base, GstEvent * event) resample->need_discont = TRUE; break; case GST_EVENT_EOS: - if (resample->state) { - guint latency = resample->funcs->get_input_latency (resample->state); +#if 0 + if (resample->resamp) { + guint latency = resample->funcs->get_input_latency (resample->resamp); gst_audio_resample_push_drain (resample, latency); } +#endif gst_audio_resample_reset_state (resample); break; default: @@ -991,7 +729,7 @@ gst_audio_resample_check_discont (GstAudioResample * resample, GstBuffer * buf) /* convert the inbound timestamp to an offset. */ offset = gst_util_uint64_scale_int_round (GST_BUFFER_TIMESTAMP (buf) - - resample->t0, resample->inrate, GST_SECOND); + resample->t0, resample->in.rate, GST_SECOND); /* many elements generate imperfect streams due to rounding errors, so we * permit a small error (up to one sample) without triggering a filter @@ -999,14 +737,14 @@ gst_audio_resample_check_discont (GstAudioResample * resample, GstBuffer * buf) /* allow even up to more samples, since sink is not so strict anyway, * so give that one a chance to handle this as configured */ delta = ABS ((gint64) (offset - resample->samples_in)); - if (delta <= (resample->inrate >> 5)) + if (delta <= (resample->in.rate >> 5)) return FALSE; GST_WARNING_OBJECT (resample, "encountered timestamp discontinuity of %" G_GUINT64_FORMAT " samples = %" GST_TIME_FORMAT, delta, GST_TIME_ARGS (gst_util_uint64_scale_int_round (delta, GST_SECOND, - resample->inrate))); + resample->in.rate))); return TRUE; } @@ -1018,16 +756,13 @@ gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf, gsize outsize; guint32 in_len, in_processed; guint32 out_len, out_processed; - guint filt_len = resample->funcs->get_filt_len (resample->state); + guint filt_len = gst_audio_resampler_get_max_latency (resample->resamp) * 2; gst_buffer_map (inbuf, &in_map, GST_MAP_READ); gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE); - in_len = in_map.size / resample->channels; - out_len = out_map.size / resample->channels; - - in_len /= (resample->width / 8); - out_len /= (resample->width / 8); + in_len = in_map.size / resample->in.bpf; + out_len = out_map.size / resample->out.bpf; in_processed = in_len; out_processed = out_len; @@ -1048,7 +783,10 @@ gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf, { guint num, den; - resample->funcs->get_ratio (resample->state, &num, &den); + + num = resample->in.rate; + den = resample->out.rate; + if (resample->samples_in + in_len >= filt_len / 2) out_processed = gst_util_uint64_scale_int_ceil (resample->samples_in + in_len - @@ -1062,13 +800,12 @@ gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf, in_processed = in_len; } } else { /* not a gap */ - - gint err; - if (resample->num_gap_samples > filt_len) { /* push in enough zeros to restore the filter to the right offset */ - guint num, den; - resample->funcs->get_ratio (resample->state, &num, &den); + guint num; + + num = resample->in.rate; + gst_audio_resample_dump_drain (resample, (resample->num_gap_samples - filt_len) % num); } @@ -1078,45 +815,27 @@ gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf, if (resample->num_nongap_samples > filt_len) resample->num_nongap_samples = filt_len; } - - if (resample->funcs->width != resample->width) { - /* need to convert data format for processing; ensure we have enough - * workspace available */ - if (!gst_audio_resample_workspace_realloc (&resample->tmp_in, - &resample->tmp_in_size, in_len * resample->channels * - (resample->funcs->width / 8)) || - !gst_audio_resample_workspace_realloc (&resample->tmp_out, - &resample->tmp_out_size, out_len * resample->channels * - (resample->funcs->width / 8))) { - GST_ERROR_OBJECT (resample, "failed to allocate workspace"); - gst_buffer_unmap (inbuf, &in_map); - gst_buffer_unmap (outbuf, &out_map); - return GST_FLOW_ERROR; - } - - /* convert input */ - gst_audio_resample_convert_buffer (resample, in_map.data, - resample->tmp_in, in_len, FALSE); - + { /* process */ - err = resample->funcs->process (resample->state, - resample->tmp_in, &in_processed, resample->tmp_out, &out_processed); + { + gsize in_proc, out_proc, out_test; + gpointer in[1], out[1]; - /* convert output */ - gst_audio_resample_convert_buffer (resample, resample->tmp_out, - out_map.data, out_processed, TRUE); - } else { - /* no format conversion required; process */ - err = resample->funcs->process (resample->state, - in_map.data, &in_processed, out_map.data, &out_processed); - } + out_test = + gst_audio_resampler_get_out_frames (resample->resamp, in_len); + out_test = MIN (out_test, out_len); - if (G_UNLIKELY (err != RESAMPLER_ERR_SUCCESS)) { - GST_ERROR_OBJECT (resample, "Failed to convert data: %s", - resample->funcs->strerror (err)); - gst_buffer_unmap (inbuf, &in_map); - gst_buffer_unmap (outbuf, &out_map); - return GST_FLOW_ERROR; + in[0] = in_map.data; + out[0] = out_map.data; + gst_audio_resampler_resample (resample->resamp, in, in_len, + out, out_len, &in_proc, &out_proc); + + in_processed = in_proc; + out_processed = out_proc; + + //g_printerr ("in %d, test %d, %d, real %d (%d)\n", (gint) in_len, (gint) out_test, (gint) out_len, (gint) out_proc, (gint) (out_test - out_proc)); + g_assert (out_test == out_proc); + } } } @@ -1133,10 +852,10 @@ gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf, if (GST_CLOCK_TIME_IS_VALID (resample->t0)) { GST_BUFFER_TIMESTAMP (outbuf) = resample->t0 + gst_util_uint64_scale_int_round (resample->samples_out, GST_SECOND, - resample->outrate); + resample->out.rate); GST_BUFFER_DURATION (outbuf) = resample->t0 + gst_util_uint64_scale_int_round (resample->samples_out + out_processed, - GST_SECOND, resample->outrate) - GST_BUFFER_TIMESTAMP (outbuf); + GST_SECOND, resample->out.rate) - GST_BUFFER_TIMESTAMP (outbuf); } else { GST_BUFFER_TIMESTAMP (outbuf) = GST_CLOCK_TIME_NONE; GST_BUFFER_DURATION (outbuf) = GST_CLOCK_TIME_NONE; @@ -1156,7 +875,7 @@ gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf, gst_buffer_unmap (inbuf, &in_map); gst_buffer_unmap (outbuf, &out_map); - outsize = out_processed * resample->channels * (resample->width / 8); + outsize = out_processed * resample->in.bpf; gst_buffer_resize (outbuf, 0, outsize); GST_LOG_OBJECT (resample, @@ -1168,7 +887,10 @@ gst_audio_resample_process (GstAudioResample * resample, GstBuffer * inbuf, GST_TIME_ARGS (GST_BUFFER_DURATION (outbuf)), GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf)); - return GST_FLOW_OK; + if (outsize == 0) + return GST_BASE_TRANSFORM_FLOW_DROPPED; + else + return GST_FLOW_OK; } static GstFlowReturn @@ -1178,18 +900,6 @@ gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf, GstAudioResample *resample = GST_AUDIO_RESAMPLE (base); GstFlowReturn ret; - if (resample->state == NULL) { - if (G_UNLIKELY (!(resample->state = - gst_audio_resample_init_state (resample, resample->width, - resample->channels, resample->inrate, resample->outrate, - resample->quality, resample->fp, resample->sinc_filter_mode, - resample->sinc_filter_auto_threshold)))) - return GST_FLOW_ERROR; - - resample->funcs = - gst_audio_resample_get_funcs (resample->width, resample->fp); - } - GST_LOG_OBJECT (resample, "transforming buffer of %" G_GSIZE_FORMAT " bytes," " ts %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ", offset %" G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT, @@ -1207,7 +917,9 @@ gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf, /* handle discontinuity */ if (G_UNLIKELY (resample->need_discont)) { +#if 0 resample->funcs->skip_zeros (resample->state); +#endif resample->num_gap_samples = 0; resample->num_nongap_samples = 0; /* reset */ @@ -1225,7 +937,7 @@ gst_audio_resample_transform (GstBaseTransform * base, GstBuffer * inbuf, resample->in_offset0 = GST_BUFFER_OFFSET (inbuf); resample->out_offset0 = gst_util_uint64_scale_int_round (resample->in_offset0, - resample->outrate, resample->inrate); + resample->out.rate, resample->in.rate); } else { GST_DEBUG_OBJECT (resample, "... but new offset is invalid"); resample->in_offset0 = GST_BUFFER_OFFSET_NONE; @@ -1279,8 +991,8 @@ gst_audio_resample_submit_input_buffer (GstBaseTransform * base, if (base->segment.format == GST_FORMAT_TIME) { input = - gst_audio_buffer_clip (input, &base->segment, resample->inrate, - resample->channels * resample->width); + gst_audio_buffer_clip (input, &base->segment, resample->in.rate, + resample->in.bpf); if (!input) return GST_FLOW_OK; @@ -1305,13 +1017,15 @@ gst_audio_resample_query (GstPad * pad, GstObject * parent, GstQuery * query) GstClockTime min, max; gboolean live; guint64 latency; - gint rate = resample->inrate; + gint rate = resample->in.rate; gint resampler_latency; +#if 0 if (resample->state) resampler_latency = resample->funcs->get_input_latency (resample->state); else +#endif resampler_latency = 0; if (gst_base_transform_is_passthrough (trans)) @@ -1360,43 +1074,26 @@ gst_audio_resample_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstAudioResample *resample; - gint quality; resample = GST_AUDIO_RESAMPLE (object); switch (prop_id) { case PROP_QUALITY: /* FIXME locking! */ - quality = g_value_get_int (value); - GST_DEBUG_OBJECT (resample, "new quality %d", quality); - - gst_audio_resample_update_state (resample, resample->width, - resample->channels, resample->inrate, resample->outrate, - quality, resample->fp, resample->sinc_filter_mode, - resample->sinc_filter_auto_threshold); + resample->quality = g_value_get_int (value); + GST_DEBUG_OBJECT (resample, "new quality %d", resample->quality); + gst_audio_resample_update_state (resample, NULL, NULL); break; - case PROP_SINC_FILTER_MODE:{ + case PROP_SINC_FILTER_MODE: /* FIXME locking! */ - SpeexResamplerSincFilterMode sinc_filter_mode = g_value_get_enum (value); - - gst_audio_resample_update_state (resample, resample->width, - resample->channels, resample->inrate, resample->outrate, - resample->quality, resample->fp, sinc_filter_mode, - resample->sinc_filter_auto_threshold); - + resample->sinc_filter_mode = g_value_get_enum (value); + gst_audio_resample_update_state (resample, NULL, NULL); break; - } - case PROP_SINC_FILTER_AUTO_THRESHOLD:{ + case PROP_SINC_FILTER_AUTO_THRESHOLD: /* FIXME locking! */ - guint32 sinc_filter_auto_threshold = g_value_get_uint (value); - - gst_audio_resample_update_state (resample, resample->width, - resample->channels, resample->inrate, resample->outrate, - resample->quality, resample->fp, resample->sinc_filter_mode, - sinc_filter_auto_threshold); - + resample->sinc_filter_auto_threshold = g_value_get_uint (value); + gst_audio_resample_update_state (resample, NULL, NULL); break; - } default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -1427,49 +1124,31 @@ gst_audio_resample_get_property (GObject * object, guint prop_id, } } -static GType -speex_resampler_sinc_filter_mode_get_type (void) -{ - static GType speex_resampler_sinc_filter_mode_type = 0; - - if (!speex_resampler_sinc_filter_mode_type) { - static const GEnumValue sinc_filter_modes[] = { - {SPEEX_RESAMPLER_SINC_FILTER_INTERPOLATED, "Use interpolated sinc table", - "interpolated"}, - {SPEEX_RESAMPLER_SINC_FILTER_FULL, "Use full sinc table", "full"}, - {SPEEX_RESAMPLER_SINC_FILTER_AUTO, - "Use full table if table size below threshold", "auto"}, - {0, NULL, NULL}, - }; - - speex_resampler_sinc_filter_mode_type = - g_enum_register_static ("SpeexResamplerSincFilterMode", - sinc_filter_modes); - } - - return speex_resampler_sinc_filter_mode_type; -} - /* FIXME: should have a benchmark fallback for the case where orc is disabled */ #if defined(AUDIORESAMPLE_FORMAT_AUTO) && !defined(DISABLE_ORC) #define BENCHMARK_SIZE 512 static gboolean -_benchmark_int_float (SpeexResamplerState * st) +_benchmark_int_float (GstAudioResampler * st) { gint16 in[BENCHMARK_SIZE] = { 0, }, G_GNUC_UNUSED out[BENCHMARK_SIZE / 2]; gfloat in_tmp[BENCHMARK_SIZE], out_tmp[BENCHMARK_SIZE / 2]; gint i; guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2; + gpointer inp[1], outp[1]; + gsize produced, consumed; for (i = 0; i < BENCHMARK_SIZE; i++) { gfloat tmp = in[i]; in_tmp[i] = tmp / G_MAXINT16; } - resample_float_resampler_process_interleaved_float (st, - (const guint8 *) in_tmp, &inlen, (guint8 *) out_tmp, &outlen); + inp[0] = in_tmp; + outp[0] = out_tmp; + + gst_audio_resampler_resample (st, + inp, inlen, outp, outlen, &produced, &consumed); if (outlen == 0) { GST_ERROR ("Failed to use float resampler"); @@ -1485,13 +1164,18 @@ _benchmark_int_float (SpeexResamplerState * st) } static gboolean -_benchmark_int_int (SpeexResamplerState * st) +_benchmark_int_int (GstAudioResampler * st) { gint16 in[BENCHMARK_SIZE] = { 0, }, out[BENCHMARK_SIZE / 2]; guint32 inlen = BENCHMARK_SIZE, outlen = BENCHMARK_SIZE / 2; + gpointer inp[1], outp[1]; + gsize produced, consumed; - resample_int_resampler_process_interleaved_int (st, (const guint8 *) in, - &inlen, (guint8 *) out, &outlen); + inp[0] = in; + outp[0] = out; + + gst_audio_resampler_resample (st, inp, inlen, outp, outlen, &produced, + &consumed); if (outlen == 0) { GST_ERROR ("Failed to use int resampler"); @@ -1506,25 +1190,23 @@ _benchmark_integer_resampling (void) { OrcProfile a, b; gdouble av, bv; - SpeexResamplerState *sta, *stb; + GstAudioResampler *sta, *stb; int i; orc_profile_init (&a); orc_profile_init (&b); - sta = resample_float_resampler_init (1, 48000, 24000, 4, - SPEEX_RESAMPLER_SINC_FILTER_INTERPOLATED, - SPEEX_RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT, NULL); + sta = gst_audio_resampler_new (GST_AUDIO_RESAMPLER_METHOD_KAISER, + 0, GST_AUDIO_FORMAT_F32LE, 1, 48000, 24000, NULL); if (sta == NULL) { GST_ERROR ("Failed to create float resampler state"); return FALSE; } - stb = resample_int_resampler_init (1, 48000, 24000, 4, - SPEEX_RESAMPLER_SINC_FILTER_INTERPOLATED, - SPEEX_RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT, NULL); + stb = gst_audio_resampler_new (GST_AUDIO_RESAMPLER_METHOD_KAISER, + 0, GST_AUDIO_FORMAT_S32LE, 1, 48000, 24000, NULL); if (stb == NULL) { - resample_float_resampler_destroy (sta); + gst_audio_resampler_free (sta); GST_ERROR ("Failed to create int resampler state"); return FALSE; } @@ -1551,8 +1233,8 @@ _benchmark_integer_resampling (void) /* Remember benchmark result in global variable */ gst_audio_resample_use_int = (av > bv); - resample_float_resampler_destroy (sta); - resample_int_resampler_destroy (stb); + gst_audio_resampler_free (sta); + gst_audio_resampler_free (stb); if (av > bv) GST_INFO ("Using integer resampler if appropriate: %lf < %lf", bv, av); @@ -1562,8 +1244,8 @@ _benchmark_integer_resampling (void) return TRUE; error: - resample_float_resampler_destroy (sta); - resample_int_resampler_destroy (stb); + gst_audio_resampler_free (sta); + gst_audio_resampler_free (stb); return FALSE; } diff --git a/gst/audioresample/gstaudioresample.h b/gst/audioresample/gstaudioresample.h index 726236acb0..d4b45d3f72 100644 --- a/gst/audioresample/gstaudioresample.h +++ b/gst/audioresample/gstaudioresample.h @@ -26,8 +26,6 @@ #include #include -#include "speex_resampler_wrapper.h" - G_BEGIN_DECLS #define GST_TYPE_AUDIO_RESAMPLE \ @@ -60,31 +58,20 @@ struct _GstAudioResample { guint64 out_offset0; guint64 samples_in; guint64 samples_out; - + guint64 num_gap_samples; guint64 num_nongap_samples; /* properties */ + GstAudioResamplerMethod method; gint quality; - - /* state */ - gboolean fp; - gint width; - gint channels; - gint inrate; - gint outrate; - - SpeexResamplerSincFilterMode sinc_filter_mode; + GstAudioResamplerFilterMode sinc_filter_mode; guint32 sinc_filter_auto_threshold; - guint8 *tmp_in; - guint tmp_in_size; - - guint8 *tmp_out; - guint tmp_out_size; - - SpeexResamplerState *state; - const SpeexResampleFuncs *funcs; + /* state */ + GstAudioInfo in; + GstAudioInfo out; + GstAudioResampler *resamp; }; struct _GstAudioResampleClass { diff --git a/gst/audioresample/resample.c b/gst/audioresample/resample.c deleted file mode 100644 index c37553590e..0000000000 --- a/gst/audioresample/resample.c +++ /dev/null @@ -1,1516 +0,0 @@ -/* Copyright (C) 2007-2008 Jean-Marc Valin - Copyright (C) 2008 Thorvald Natvig - - File: resample.c - Arbitrary resampling code - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are - met: - - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - 3. The name of the author may not be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. -*/ - -/* - The design goals of this code are: - - Very fast algorithm - - SIMD-friendly algorithm - - Low memory requirement - - Good *perceptual* quality (and not best SNR) - - Warning: This resampler is relatively new. Although I think I got rid of - all the major bugs and I don't expect the API to change anymore, there - may be something I've missed. So use with caution. - - This algorithm is based on this original resampling algorithm: - Smith, Julius O. Digital Audio Resampling Home Page - Center for Computer Research in Music and Acoustics (CCRMA), - Stanford University, 2007. - Web published at http://www-ccrma.stanford.edu/~jos/resample/. - - There is one main difference, though. This resampler uses cubic - interpolation instead of linear interpolation in the above paper. This - makes the table much smaller and makes it possible to compute that table - on a per-stream basis. In turn, being able to tweak the table for each - stream makes it possible to both reduce complexity on simple ratios - (e.g. 2/3), and get rid of the rounding operations in the inner loop. - The latter both reduces CPU time and makes the algorithm more SIMD-friendly. -*/ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#ifdef OUTSIDE_SPEEX -#include - -#ifdef HAVE_STRING_H -#include -#endif - -#include - -#ifdef HAVE_ORC -#include -#endif - -#define EXPORT G_GNUC_INTERNAL - -#ifdef _USE_SSE -#if !defined(__SSE__) || !defined(HAVE_XMMINTRIN_H) -#undef _USE_SSE -#endif -#endif - -#ifdef _USE_SSE2 -#if !defined(__SSE2__) || !defined(HAVE_EMMINTRIN_H) -#undef _USE_SSE2 -#endif -#endif - -#ifdef _USE_NEON -#ifndef HAVE_ARM_NEON -#undef _USE_NEON -#endif -#endif - -static inline void * -speex_alloc (int size) -{ - return g_malloc0 (size); -} - -static inline void * -speex_realloc (void *ptr, int size) -{ - return g_realloc (ptr, size); -} - -static inline void -speex_free (void *ptr) -{ - g_free (ptr); -} - -#include "speex_resampler.h" -#include "arch.h" -#else /* OUTSIDE_SPEEX */ - -#include "../include/speex/speex_resampler.h" -#include "arch.h" -#include "os_support.h" -#endif /* OUTSIDE_SPEEX */ - -#include - -#ifdef FIXED_POINT -#define WORD2INT(x) ((x) < -32767 ? -32768 : ((x) > 32766 ? 32767 : (x))) -#else -#define WORD2INT(x) ((x) < -32767.5f ? -32768 : ((x) > 32766.5f ? 32767 : floor(.5+(x)))) -#endif - -#define IMAX(a,b) ((a) > (b) ? (a) : (b)) -#define IMIN(a,b) ((a) < (b) ? (a) : (b)) - -#ifndef NULL -#define NULL 0 -#endif - -#if defined _USE_SSE || defined _USE_SSE2 -#include "resample_sse.h" -#endif - -#ifdef _USE_NEON -#include "resample_neon.h" -#endif - -/* Numer of elements to allocate on the stack */ -#ifdef VAR_ARRAYS -#define FIXED_STACK_ALLOC 8192 -#else -#define FIXED_STACK_ALLOC 1024 -#endif - -/* Allow selecting SSE or not when compiled with SSE support */ -#ifdef _USE_SSE -#define SSE_FALLBACK(macro) \ - if (st->use_sse) goto sse_##macro##_sse; { -#define SSE_IMPLEMENTATION(macro) \ - goto sse_##macro##_end; } sse_##macro##_sse: { -#define SSE_END(macro) sse_##macro##_end:; } -#else -#define SSE_FALLBACK(macro) -#endif - -#ifdef _USE_SSE2 -#define SSE2_FALLBACK(macro) \ - if (st->use_sse2) goto sse2_##macro##_sse2; { -#define SSE2_IMPLEMENTATION(macro) \ - goto sse2_##macro##_end; } sse2_##macro##_sse2: { -#define SSE2_END(macro) sse2_##macro##_end:; } -#else -#define SSE2_FALLBACK(macro) -#endif - -#ifdef _USE_NEON -#define NEON_FALLBACK(macro) \ - if (st->use_neon) goto neon_##macro##_neon; { -#define NEON_IMPLEMENTATION(macro) \ - goto neon_##macro##_end; } neon_##macro##_neon: { -#define NEON_END(macro) neon_##macro##_end:; } -#else -#define NEON_FALLBACK(macro) -#endif - - -typedef int (*resampler_basic_func) (SpeexResamplerState *, spx_uint32_t, - const spx_word16_t *, spx_uint32_t *, spx_word16_t *, spx_uint32_t *); - -struct SpeexResamplerState_ -{ - spx_uint32_t in_rate; - spx_uint32_t out_rate; - spx_uint32_t num_rate; - spx_uint32_t den_rate; - - int quality; - spx_uint32_t nb_channels; - spx_uint32_t filt_len; - spx_uint32_t mem_alloc_size; - spx_uint32_t buffer_size; - int int_advance; - int frac_advance; - float cutoff; - spx_uint32_t oversample; - int initialised; - int started; - int use_full_sinc_table; - - /* These are per-channel */ - spx_int32_t *last_sample; - spx_uint32_t *samp_frac_num; - spx_uint32_t *magic_samples; - - spx_word16_t *mem; - spx_word16_t *sinc_table; - spx_uint32_t sinc_table_length; - resampler_basic_func resampler_ptr; - - int in_stride; - int out_stride; - - int use_sse:1; - int use_sse2:1; - int use_neon:1; -}; - -static const double kaiser12_table[68] = { - 0.99859849, 1.00000000, 0.99859849, 0.99440475, 0.98745105, 0.97779076, - 0.96549770, 0.95066529, 0.93340547, 0.91384741, 0.89213598, 0.86843014, - 0.84290116, 0.81573067, 0.78710866, 0.75723148, 0.72629970, 0.69451601, - 0.66208321, 0.62920216, 0.59606986, 0.56287762, 0.52980938, 0.49704014, - 0.46473455, 0.43304576, 0.40211431, 0.37206735, 0.34301800, 0.31506490, - 0.28829195, 0.26276832, 0.23854851, 0.21567274, 0.19416736, 0.17404546, - 0.15530766, 0.13794294, 0.12192957, 0.10723616, 0.09382272, 0.08164178, - 0.07063950, 0.06075685, 0.05193064, 0.04409466, 0.03718069, 0.03111947, - 0.02584161, 0.02127838, 0.01736250, 0.01402878, 0.01121463, 0.00886058, - 0.00691064, 0.00531256, 0.00401805, 0.00298291, 0.00216702, 0.00153438, - 0.00105297, 0.00069463, 0.00043489, 0.00025272, 0.00013031, 0.0000527734, - 0.00001000, 0.00000000 -}; - -/* -static const double kaiser12_table[36] = { - 0.99440475, 1.00000000, 0.99440475, 0.97779076, 0.95066529, 0.91384741, - 0.86843014, 0.81573067, 0.75723148, 0.69451601, 0.62920216, 0.56287762, - 0.49704014, 0.43304576, 0.37206735, 0.31506490, 0.26276832, 0.21567274, - 0.17404546, 0.13794294, 0.10723616, 0.08164178, 0.06075685, 0.04409466, - 0.03111947, 0.02127838, 0.01402878, 0.00886058, 0.00531256, 0.00298291, - 0.00153438, 0.00069463, 0.00025272, 0.0000527734, 0.00000500, 0.00000000}; -*/ -static const double kaiser10_table[36] = { - 0.99537781, 1.00000000, 0.99537781, 0.98162644, 0.95908712, 0.92831446, - 0.89005583, 0.84522401, 0.79486424, 0.74011713, 0.68217934, 0.62226347, - 0.56155915, 0.50119680, 0.44221549, 0.38553619, 0.33194107, 0.28205962, - 0.23636152, 0.19515633, 0.15859932, 0.12670280, 0.09935205, 0.07632451, - 0.05731132, 0.04193980, 0.02979584, 0.02044510, 0.01345224, 0.00839739, - 0.00488951, 0.00257636, 0.00115101, 0.00035515, 0.00000000, 0.00000000 -}; - -static const double kaiser8_table[36] = { - 0.99635258, 1.00000000, 0.99635258, 0.98548012, 0.96759014, 0.94302200, - 0.91223751, 0.87580811, 0.83439927, 0.78875245, 0.73966538, 0.68797126, - 0.63451750, 0.58014482, 0.52566725, 0.47185369, 0.41941150, 0.36897272, - 0.32108304, 0.27619388, 0.23465776, 0.19672670, 0.16255380, 0.13219758, - 0.10562887, 0.08273982, 0.06335451, 0.04724088, 0.03412321, 0.02369490, - 0.01563093, 0.00959968, 0.00527363, 0.00233883, 0.00050000, 0.00000000 -}; - -static const double kaiser6_table[36] = { - 0.99733006, 1.00000000, 0.99733006, 0.98935595, 0.97618418, 0.95799003, - 0.93501423, 0.90755855, 0.87598009, 0.84068475, 0.80211977, 0.76076565, - 0.71712752, 0.67172623, 0.62508937, 0.57774224, 0.53019925, 0.48295561, - 0.43647969, 0.39120616, 0.34752997, 0.30580127, 0.26632152, 0.22934058, - 0.19505503, 0.16360756, 0.13508755, 0.10953262, 0.08693120, 0.06722600, - 0.05031820, 0.03607231, 0.02432151, 0.01487334, 0.00752000, 0.00000000 -}; - -struct FuncDef -{ - const double *table; - int oversample; -}; - -static struct FuncDef _KAISER12 = { kaiser12_table, 64 }; - -#define KAISER12 (&_KAISER12) -/*static struct FuncDef _KAISER12 = {kaiser12_table, 32}; -#define KAISER12 (&_KAISER12)*/ -static struct FuncDef _KAISER10 = { kaiser10_table, 32 }; - -#define KAISER10 (&_KAISER10) -static struct FuncDef _KAISER8 = { kaiser8_table, 32 }; - -#define KAISER8 (&_KAISER8) -static struct FuncDef _KAISER6 = { kaiser6_table, 32 }; - -#define KAISER6 (&_KAISER6) - -struct QualityMapping -{ - int base_length; - int oversample; - float downsample_bandwidth; - float upsample_bandwidth; - struct FuncDef *window_func; -}; - - -/* This table maps conversion quality to internal parameters. There are two - reasons that explain why the up-sampling bandwidth is larger than the - down-sampling bandwidth: - 1) When up-sampling, we can assume that the spectrum is already attenuated - close to the Nyquist rate (from an A/D or a previous resampling filter) - 2) Any aliasing that occurs very close to the Nyquist rate will be masked - by the sinusoids/noise just below the Nyquist rate (guaranteed only for - up-sampling). -*/ -static const struct QualityMapping quality_map[11] = { - {8, 4, 0.830f, 0.860f, KAISER6}, /* Q0 */ - {16, 4, 0.850f, 0.880f, KAISER6}, /* Q1 */ - {32, 4, 0.882f, 0.910f, KAISER6}, /* Q2 *//* 82.3% cutoff ( ~60 dB stop) 6 */ - {48, 8, 0.895f, 0.917f, KAISER8}, /* Q3 *//* 84.9% cutoff ( ~80 dB stop) 8 */ - {64, 8, 0.921f, 0.940f, KAISER8}, /* Q4 *//* 88.7% cutoff ( ~80 dB stop) 8 */ - {80, 16, 0.922f, 0.940f, KAISER10}, /* Q5 *//* 89.1% cutoff (~100 dB stop) 10 */ - {96, 16, 0.940f, 0.945f, KAISER10}, /* Q6 *//* 91.5% cutoff (~100 dB stop) 10 */ - {128, 16, 0.950f, 0.950f, KAISER10}, /* Q7 *//* 93.1% cutoff (~100 dB stop) 10 */ - {160, 16, 0.960f, 0.960f, KAISER10}, /* Q8 *//* 94.5% cutoff (~100 dB stop) 10 */ - {192, 32, 0.968f, 0.968f, KAISER12}, /* Q9 *//* 95.5% cutoff (~100 dB stop) 10 */ - {256, 32, 0.975f, 0.975f, KAISER12}, /* Q10 *//* 96.6% cutoff (~100 dB stop) 10 */ -}; - -/*8,24,40,56,80,104,128,160,200,256,320*/ -#ifdef DOUBLE_PRECISION -static double -compute_func (double x, struct FuncDef *func) -{ - double y, frac; -#else -static double -compute_func (float x, struct FuncDef *func) -{ - float y, frac; -#endif - double interp[4]; - int ind; - y = x * func->oversample; - ind = (int) floor (y); - frac = (y - ind); - /* CSE with handle the repeated powers */ - interp[3] = -0.1666666667 * frac + 0.1666666667 * (frac * frac * frac); - interp[2] = frac + 0.5 * (frac * frac) - 0.5 * (frac * frac * frac); - /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac; */ - interp[0] = - -0.3333333333 * frac + 0.5 * (frac * frac) - - 0.1666666667 * (frac * frac * frac); - /* Just to make sure we don't have rounding problems */ - interp[1] = 1.f - interp[3] - interp[2] - interp[0]; - - /*sum = frac*accum[1] + (1-frac)*accum[2]; */ - return interp[0] * func->table[ind] + interp[1] * func->table[ind + 1] + - interp[2] * func->table[ind + 2] + interp[3] * func->table[ind + 3]; -} - -#if 0 -#include -int -main (int argc, char **argv) -{ - int i; - for (i = 0; i < 256; i++) { - printf ("%f\n", compute_func (i / 256., KAISER12)); - } - return 0; -} -#endif - -#ifdef FIXED_POINT -/* The slow way of computing a sinc for the table. Should improve that some day */ -static spx_word16_t -sinc (float cutoff, float x, int N, struct FuncDef *window_func) -{ - /*fprintf (stderr, "%f ", x); */ - float xx = x * cutoff; - if (fabs (x) < 1e-6f) - return WORD2INT (32768. * cutoff); - else if (fabs (x) > .5f * N) - return 0; - /*FIXME: Can it really be any slower than this? */ - return WORD2INT (32768. * cutoff * sin (G_PI * xx) / (G_PI * xx) * - compute_func (fabs (2. * x / N), window_func)); -} -#else -/* The slow way of computing a sinc for the table. Should improve that some day */ -#ifdef DOUBLE_PRECISION -static spx_word16_t -sinc (double cutoff, double x, int N, struct FuncDef *window_func) -{ - /*fprintf (stderr, "%f ", x); */ - double xx = x * cutoff; -#else -static spx_word16_t -sinc (float cutoff, float x, int N, struct FuncDef *window_func) -{ - /*fprintf (stderr, "%f ", x); */ - float xx = x * cutoff; -#endif - if (fabs (x) < 1e-6) - return cutoff; - else if (fabs (x) > .5 * N) - return 0; - /*FIXME: Can it really be any slower than this? */ - return cutoff * sin (G_PI * xx) / (G_PI * xx) * compute_func (fabs (2. * x / - N), window_func); -} -#endif - -#ifdef FIXED_POINT -static void -cubic_coef (spx_word16_t x, spx_word16_t interp[4]) -{ - /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation - but I know it's MMSE-optimal on a sinc */ - spx_word16_t x2, x3; - x2 = MULT16_16_P15 (x, x); - x3 = MULT16_16_P15 (x, x2); - interp[0] = - PSHR32 (MULT16_16 (QCONST16 (-0.16667f, 15), - x) + MULT16_16 (QCONST16 (0.16667f, 15), x3), 15); - interp[1] = - EXTRACT16 (EXTEND32 (x) + SHR32 (SUB32 (EXTEND32 (x2), EXTEND32 (x3)), - 1)); - interp[3] = - PSHR32 (MULT16_16 (QCONST16 (-0.33333f, 15), - x) + MULT16_16 (QCONST16 (.5f, 15), - x2) - MULT16_16 (QCONST16 (0.16667f, 15), x3), 15); - /* Just to make sure we don't have rounding problems */ - interp[2] = Q15_ONE - interp[0] - interp[1] - interp[3]; - if (interp[2] < 32767) - interp[2] += 1; -} -#else -static void -cubic_coef (spx_word16_t frac, spx_word16_t interp[4]) -{ - /* Compute interpolation coefficients. I'm not sure whether this corresponds to cubic interpolation - but I know it's MMSE-optimal on a sinc */ - interp[0] = -0.16667f * frac + 0.16667f * frac * frac * frac; - interp[1] = frac + 0.5f * frac * frac - 0.5f * frac * frac * frac; - /*interp[2] = 1.f - 0.5f*frac - frac*frac + 0.5f*frac*frac*frac; */ - interp[3] = - -0.33333f * frac + 0.5f * frac * frac - 0.16667f * frac * frac * frac; - /* Just to make sure we don't have rounding problems */ - interp[2] = 1. - interp[0] - interp[1] - interp[3]; -} -#endif - -#ifndef DOUBLE_PRECISION -static int -resampler_basic_direct_single (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, - spx_word16_t * out, spx_uint32_t * out_len) -{ - const int N = st->filt_len; - int out_sample = 0; - int last_sample = st->last_sample[channel_index]; - spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; - const spx_word16_t *sinc_table = st->sinc_table; - const int out_stride = st->out_stride; - const int int_advance = st->int_advance; - const int frac_advance = st->frac_advance; - const spx_uint32_t den_rate = st->den_rate; - spx_word32_t sum; - int j; - - while (!(last_sample >= (spx_int32_t) * in_len - || out_sample >= (spx_int32_t) * out_len)) { - const spx_word16_t *sinc = &sinc_table[samp_frac_num * N]; - const spx_word16_t *iptr = &in[last_sample]; - - SSE_FALLBACK (INNER_PRODUCT_SINGLE) - NEON_FALLBACK (INNER_PRODUCT_SINGLE) - sum = 0; - for (j = 0; j < N; j++) - sum += MULT16_16 (sinc[j], iptr[j]); - -/* This code is slower on most DSPs which have only 2 accumulators. - Plus this forces truncation to 32 bits and you lose the HW guard bits. - I think we can trust the compiler and let it vectorize and/or unroll itself. - spx_word32_t accum[4] = {0,0,0,0}; - for(j=0;j= den_rate) { - samp_frac_num -= den_rate; - last_sample++; - } - } - - st->last_sample[channel_index] = last_sample; - st->samp_frac_num[channel_index] = samp_frac_num; - return out_sample; -} -#endif - -#ifdef FIXED_POINT -#else -/* This is the same as the previous function, except with a double-precision accumulator */ -static int -resampler_basic_direct_double (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, - spx_word16_t * out, spx_uint32_t * out_len) -{ - const int N = st->filt_len; - int out_sample = 0; - int last_sample = st->last_sample[channel_index]; - spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; - const spx_word16_t *sinc_table = st->sinc_table; - const int out_stride = st->out_stride; - const int int_advance = st->int_advance; - const int frac_advance = st->frac_advance; - const spx_uint32_t den_rate = st->den_rate; - double sum; - int j; - - while (!(last_sample >= (spx_int32_t) * in_len - || out_sample >= (spx_int32_t) * out_len)) { - const spx_word16_t *sinc = &sinc_table[samp_frac_num * N]; - const spx_word16_t *iptr = &in[last_sample]; - - SSE2_FALLBACK (INNER_PRODUCT_DOUBLE) - double accum[4] = { 0, 0, 0, 0 }; - - for (j = 0; j < N; j += 4) { - accum[0] += sinc[j] * iptr[j]; - accum[1] += sinc[j + 1] * iptr[j + 1]; - accum[2] += sinc[j + 2] * iptr[j + 2]; - accum[3] += sinc[j + 3] * iptr[j + 3]; - } - sum = accum[0] + accum[1] + accum[2] + accum[3]; -#if defined(OVERRIDE_INNER_PRODUCT_DOUBLE) && defined(_USE_SSE2) - SSE2_IMPLEMENTATION (INNER_PRODUCT_DOUBLE) - sum = inner_product_double (sinc, iptr, N); - SSE2_END (INNER_PRODUCT_DOUBLE) -#endif - out[out_stride * out_sample++] = PSHR32 (sum, 15); - last_sample += int_advance; - samp_frac_num += frac_advance; - if (samp_frac_num >= den_rate) { - samp_frac_num -= den_rate; - last_sample++; - } - } - - st->last_sample[channel_index] = last_sample; - st->samp_frac_num[channel_index] = samp_frac_num; - return out_sample; -} -#endif - -#ifndef DOUBLE_PRECISION -static int -resampler_basic_interpolate_single (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, - spx_word16_t * out, spx_uint32_t * out_len) -{ - const int N = st->filt_len; - int out_sample = 0; - int last_sample = st->last_sample[channel_index]; - spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; - const int out_stride = st->out_stride; - const int int_advance = st->int_advance; - const int frac_advance = st->frac_advance; - const spx_uint32_t den_rate = st->den_rate; - int j; - spx_word32_t sum; - - while (!(last_sample >= (spx_int32_t) * in_len - || out_sample >= (spx_int32_t) * out_len)) { - const spx_word16_t *iptr = &in[last_sample]; - - const int offset = samp_frac_num * st->oversample / st->den_rate; -#ifdef FIXED_POINT - const spx_word16_t frac = - ((((gint64) samp_frac_num * (gint64) st->oversample) % st->den_rate) - << 15) / st->den_rate; -#else - const spx_word16_t frac = - ((float) ((samp_frac_num * st->oversample) % st->den_rate)) / - st->den_rate; -#endif - spx_word16_t interp[4]; - - - SSE_FALLBACK (INTERPOLATE_PRODUCT_SINGLE) - spx_word32_t accum[4] = { 0, 0, 0, 0 }; - - for (j = 0; j < N; j++) { - const spx_word16_t curr_in = iptr[j]; - accum[0] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset - 2]); - accum[1] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset - 1]); - accum[2] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset]); - accum[3] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset + 1]); - } - - cubic_coef (frac, interp); - sum = - MULT16_32_Q15 (interp[0], SHR32 (accum[0], - 1)) + MULT16_32_Q15 (interp[1], SHR32 (accum[1], - 1)) + MULT16_32_Q15 (interp[2], SHR32 (accum[2], - 1)) + MULT16_32_Q15 (interp[3], SHR32 (accum[3], 1)); -#if defined(OVERRIDE_INTERPOLATE_PRODUCT_SINGLE) && defined(_USE_SSE) - SSE_IMPLEMENTATION (INTERPOLATE_PRODUCT_SINGLE) - cubic_coef (frac, interp); - sum = - interpolate_product_single (iptr, - st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, - interp); - SSE_END (INTERPOLATE_PRODUCT_SINGLE) -#endif - out[out_stride * out_sample++] = SATURATE32PSHR (sum, 14, 32767); - last_sample += int_advance; - samp_frac_num += frac_advance; - if (samp_frac_num >= den_rate) { - samp_frac_num -= den_rate; - last_sample++; - } - } - - st->last_sample[channel_index] = last_sample; - st->samp_frac_num[channel_index] = samp_frac_num; - return out_sample; -} -#endif - -#ifdef FIXED_POINT -#else -/* This is the same as the previous function, except with a double-precision accumulator */ -static int -resampler_basic_interpolate_double (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_word16_t * in, spx_uint32_t * in_len, - spx_word16_t * out, spx_uint32_t * out_len) -{ - const int N = st->filt_len; - int out_sample = 0; - int last_sample = st->last_sample[channel_index]; - spx_uint32_t samp_frac_num = st->samp_frac_num[channel_index]; - const int out_stride = st->out_stride; - const int int_advance = st->int_advance; - const int frac_advance = st->frac_advance; - const spx_uint32_t den_rate = st->den_rate; - int j; - spx_word32_t sum; - - while (!(last_sample >= (spx_int32_t) * in_len - || out_sample >= (spx_int32_t) * out_len)) { - const spx_word16_t *iptr = &in[last_sample]; - - const int offset = samp_frac_num * st->oversample / st->den_rate; -#ifdef FIXED_POINT - const spx_word16_t frac = - PDIV32 (SHL32 ((samp_frac_num * st->oversample) % st->den_rate, 15), - st->den_rate); -#else -#ifdef DOUBLE_PRECISION - const spx_word16_t frac = - ((double) ((samp_frac_num * st->oversample) % st->den_rate)) / - st->den_rate; -#else - const spx_word16_t frac = - ((float) ((samp_frac_num * st->oversample) % st->den_rate)) / - st->den_rate; -#endif -#endif - spx_word16_t interp[4]; - - - SSE2_FALLBACK (INTERPOLATE_PRODUCT_DOUBLE) - double accum[4] = { 0, 0, 0, 0 }; - - for (j = 0; j < N; j++) { - const double curr_in = iptr[j]; - accum[0] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset - 2]); - accum[1] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset - 1]); - accum[2] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset]); - accum[3] += - MULT16_16 (curr_in, - st->sinc_table[4 + (j + 1) * st->oversample - offset + 1]); - } - - cubic_coef (frac, interp); - sum = - MULT16_32_Q15 (interp[0], accum[0]) + MULT16_32_Q15 (interp[1], - accum[1]) + MULT16_32_Q15 (interp[2], - accum[2]) + MULT16_32_Q15 (interp[3], accum[3]); -#if defined(OVERRIDE_INTERPOLATE_PRODUCT_DOUBLE) && defined(_USE_SSE2) - SSE2_IMPLEMENTATION (INTERPOLATE_PRODUCT_DOUBLE) - cubic_coef (frac, interp); - sum = - interpolate_product_double (iptr, - st->sinc_table + st->oversample + 4 - offset - 2, N, st->oversample, - interp); - SSE2_END (INTERPOLATE_PRODUCT_DOUBLE) -#endif - out[out_stride * out_sample++] = PSHR32 (sum, 15); - last_sample += int_advance; - samp_frac_num += frac_advance; - if (samp_frac_num >= den_rate) { - samp_frac_num -= den_rate; - last_sample++; - } - } - - st->last_sample[channel_index] = last_sample; - st->samp_frac_num[channel_index] = samp_frac_num; - return out_sample; -} -#endif - -static void -update_filter (SpeexResamplerState * st) -{ - spx_uint32_t old_length; - - old_length = st->filt_len; - st->oversample = quality_map[st->quality].oversample; - st->filt_len = quality_map[st->quality].base_length; - - if (st->num_rate > st->den_rate) { - /* down-sampling */ - st->cutoff = - quality_map[st->quality].downsample_bandwidth * st->den_rate / - st->num_rate; - /* FIXME: divide the numerator and denominator by a certain amount if they're too large */ - st->filt_len = st->filt_len * st->num_rate / st->den_rate; - /* Round down to make sure we have a multiple of 4 */ - st->filt_len &= (~0x3); - if (2 * st->den_rate < st->num_rate) - st->oversample >>= 1; - if (4 * st->den_rate < st->num_rate) - st->oversample >>= 1; - if (8 * st->den_rate < st->num_rate) - st->oversample >>= 1; - if (16 * st->den_rate < st->num_rate) - st->oversample >>= 1; - if (st->oversample < 1) - st->oversample = 1; - } else { - /* up-sampling */ - st->cutoff = quality_map[st->quality].upsample_bandwidth; - } - - /* Choose the resampling type that requires the least amount of memory */ - /* Or if the full sinc table is explicitely requested, use that */ - if (st->use_full_sinc_table || (st->den_rate <= st->oversample)) { - spx_uint32_t i; - if (!st->sinc_table) - st->sinc_table = - (spx_word16_t *) speex_alloc (st->filt_len * st->den_rate * - sizeof (spx_word16_t)); - else if (st->sinc_table_length < st->filt_len * st->den_rate) { - st->sinc_table = - (spx_word16_t *) speex_realloc (st->sinc_table, - st->filt_len * st->den_rate * sizeof (spx_word16_t)); - st->sinc_table_length = st->filt_len * st->den_rate; - } - for (i = 0; i < st->den_rate; i++) { - spx_int32_t j; - for (j = 0; j < st->filt_len; j++) { - st->sinc_table[i * st->filt_len + j] = - sinc (st->cutoff, ((j - (spx_int32_t) st->filt_len / 2 + 1) - -#ifdef DOUBLE_PRECISION - ((double) i) / st->den_rate), st->filt_len, -#else - ((float) i) / st->den_rate), st->filt_len, -#endif - quality_map[st->quality].window_func); - } - } -#ifdef FIXED_POINT - st->resampler_ptr = resampler_basic_direct_single; -#else -#ifdef DOUBLE_PRECISION - st->resampler_ptr = resampler_basic_direct_double; -#else - if (st->quality > 8) - st->resampler_ptr = resampler_basic_direct_double; - else - st->resampler_ptr = resampler_basic_direct_single; -#endif -#endif - /*fprintf (stderr, "resampler uses direct sinc table and normalised cutoff %f\n", cutoff); */ - } else { - spx_int32_t i; - if (!st->sinc_table) - st->sinc_table = - (spx_word16_t *) speex_alloc ((st->filt_len * st->oversample + - 8) * sizeof (spx_word16_t)); - else if (st->sinc_table_length < st->filt_len * st->oversample + 8) { - st->sinc_table = - (spx_word16_t *) speex_realloc (st->sinc_table, - (st->filt_len * st->oversample + 8) * sizeof (spx_word16_t)); - st->sinc_table_length = st->filt_len * st->oversample + 8; - } - for (i = -4; i < (spx_int32_t) (st->oversample * st->filt_len + 4); i++) - st->sinc_table[i + 4] = -#ifdef DOUBLE_PRECISION - sinc (st->cutoff, (i / (double) st->oversample - st->filt_len / 2), -#else - sinc (st->cutoff, (i / (float) st->oversample - st->filt_len / 2), -#endif - st->filt_len, quality_map[st->quality].window_func); -#ifdef FIXED_POINT - st->resampler_ptr = resampler_basic_interpolate_single; -#else -#ifdef DOUBLE_PRECISION - st->resampler_ptr = resampler_basic_interpolate_double; -#else - if (st->quality > 8) - st->resampler_ptr = resampler_basic_interpolate_double; - else - st->resampler_ptr = resampler_basic_interpolate_single; -#endif -#endif - /*fprintf (stderr, "resampler uses interpolated sinc table and normalised cutoff %f\n", cutoff); */ - } - st->int_advance = st->num_rate / st->den_rate; - st->frac_advance = st->num_rate % st->den_rate; - - - /* Here's the place where we update the filter memory to take into account - the change in filter length. It's probably the messiest part of the code - due to handling of lots of corner cases. */ - if (!st->mem) { - spx_uint32_t i; - st->mem_alloc_size = st->filt_len - 1 + st->buffer_size; - st->mem = - (spx_word16_t *) speex_alloc (st->nb_channels * st->mem_alloc_size * - sizeof (spx_word16_t)); - for (i = 0; i < st->nb_channels * st->mem_alloc_size; i++) - st->mem[i] = 0; - /*speex_warning("init filter"); */ - } else if (!st->started) { - spx_uint32_t i; - st->mem_alloc_size = st->filt_len - 1 + st->buffer_size; - st->mem = - (spx_word16_t *) speex_realloc (st->mem, - st->nb_channels * st->mem_alloc_size * sizeof (spx_word16_t)); - for (i = 0; i < st->nb_channels * st->mem_alloc_size; i++) - st->mem[i] = 0; - /*speex_warning("reinit filter"); */ - } else if (st->filt_len > old_length) { - spx_int32_t i; - /* Increase the filter length */ - /*speex_warning("increase filter size"); */ - int old_alloc_size = st->mem_alloc_size; - if ((st->filt_len - 1 + st->buffer_size) > st->mem_alloc_size) { - st->mem_alloc_size = st->filt_len - 1 + st->buffer_size; - st->mem = - (spx_word16_t *) speex_realloc (st->mem, - st->nb_channels * st->mem_alloc_size * sizeof (spx_word16_t)); - } - for (i = st->nb_channels - 1; i >= 0; i--) { - spx_int32_t j; - spx_uint32_t olen = old_length; - /*if (st->magic_samples[i]) */ - { - /* Try and remove the magic samples as if nothing had happened */ - - /* FIXME: This is wrong but for now we need it to avoid going over the array bounds */ - olen = old_length + 2 * st->magic_samples[i]; - for (j = old_length - 2 + st->magic_samples[i]; j >= 0; j--) - st->mem[i * st->mem_alloc_size + j + st->magic_samples[i]] = - st->mem[i * old_alloc_size + j]; - for (j = 0; j < st->magic_samples[i]; j++) - st->mem[i * st->mem_alloc_size + j] = 0; - st->magic_samples[i] = 0; - } - if (st->filt_len > olen) { - /* If the new filter length is still bigger than the "augmented" length */ - /* Copy data going backward */ - for (j = 0; j < olen - 1; j++) - st->mem[i * st->mem_alloc_size + (st->filt_len - 2 - j)] = - st->mem[i * st->mem_alloc_size + (olen - 2 - j)]; - /* Then put zeros for lack of anything better */ - for (; j < st->filt_len - 1; j++) - st->mem[i * st->mem_alloc_size + (st->filt_len - 2 - j)] = 0; - /* Adjust last_sample */ - st->last_sample[i] += (st->filt_len - olen) / 2; - } else { - /* Put back some of the magic! */ - st->magic_samples[i] = (olen - st->filt_len) / 2; - for (j = 0; j < st->filt_len - 1 + st->magic_samples[i]; j++) - st->mem[i * st->mem_alloc_size + j] = - st->mem[i * st->mem_alloc_size + j + st->magic_samples[i]]; - } - } - } else if (st->filt_len < old_length) { - spx_uint32_t i; - /* Reduce filter length, this a bit tricky. We need to store some of the memory as "magic" - samples so they can be used directly as input the next time(s) */ - for (i = 0; i < st->nb_channels; i++) { - spx_uint32_t j; - spx_uint32_t old_magic = st->magic_samples[i]; - st->magic_samples[i] = (old_length - st->filt_len) / 2; - /* We must copy some of the memory that's no longer used */ - /* Copy data going backward */ - for (j = 0; j < st->filt_len - 1 + st->magic_samples[i] + old_magic; j++) - st->mem[i * st->mem_alloc_size + j] = - st->mem[i * st->mem_alloc_size + j + st->magic_samples[i]]; - st->magic_samples[i] += old_magic; - } - } - -} - -EXPORT SpeexResamplerState * -speex_resampler_init (spx_uint32_t nb_channels, spx_uint32_t in_rate, - spx_uint32_t out_rate, int quality, - SpeexResamplerSincFilterMode sinc_filter_mode, - spx_uint32_t sinc_filter_auto_threshold, int *err) -{ - return speex_resampler_init_frac (nb_channels, in_rate, out_rate, in_rate, - out_rate, quality, sinc_filter_mode, sinc_filter_auto_threshold, err); -} - -#if defined HAVE_ORC && !defined DISABLE_ORC -static void -check_insn_set (SpeexResamplerState * st, const char *name) -{ - if (!name) - return; -#ifdef _USE_SSE - if (!strcmp (name, "sse")) - st->use_sse = 1; -#endif -#ifdef _USE_SSE2 - if (!strcmp (name, "sse2")) - st->use_sse = st->use_sse2 = 1; -#endif -#ifdef _USE_NEON - if (!strcmp (name, "neon")) - st->use_neon = 1; -#endif -} -#endif - -EXPORT SpeexResamplerState * -speex_resampler_init_frac (spx_uint32_t nb_channels, spx_uint32_t ratio_num, - spx_uint32_t ratio_den, spx_uint32_t in_rate, spx_uint32_t out_rate, - int quality, SpeexResamplerSincFilterMode sinc_filter_mode, - spx_uint32_t sinc_filter_auto_threshold, int *err) -{ - spx_uint32_t i; - SpeexResamplerState *st; - int use_full_sinc_table = 0; - if (quality > 10 || quality < 0) { - if (err) - *err = RESAMPLER_ERR_INVALID_ARG; - return NULL; - } - if (ratio_den == 0) { - if (err) - *err = RESAMPLER_ERR_INVALID_ARG; - return NULL; - } - switch (sinc_filter_mode) { - case RESAMPLER_SINC_FILTER_INTERPOLATED: - use_full_sinc_table = 0; - break; - case RESAMPLER_SINC_FILTER_FULL: - use_full_sinc_table = 1; - break; - case RESAMPLER_SINC_FILTER_AUTO: - /* Handled below */ - break; - default: - if (err) - *err = RESAMPLER_ERR_INVALID_ARG; - return NULL; - } - - st = (SpeexResamplerState *) speex_alloc (sizeof (SpeexResamplerState)); - st->initialised = 0; - st->started = 0; - st->in_rate = 0; - st->out_rate = 0; - st->num_rate = 0; - st->den_rate = 0; - st->quality = -1; - st->sinc_table_length = 0; - st->mem_alloc_size = 0; - st->filt_len = 0; - st->mem = 0; - st->resampler_ptr = 0; - st->use_full_sinc_table = use_full_sinc_table; - - st->cutoff = 1.f; - st->nb_channels = nb_channels; - st->in_stride = 1; - st->out_stride = 1; - -#ifdef FIXED_POINT - st->buffer_size = 160; -#else - st->buffer_size = 160; -#endif - - st->use_sse = st->use_sse2 = 0; - st->use_neon = 0; -#if defined HAVE_ORC && !defined DISABLE_ORC - orc_init (); - { - OrcTarget *target = orc_target_get_default (); - if (target) { - unsigned int flags = orc_target_get_default_flags (target); - check_insn_set (st, orc_target_get_name (target)); - for (i = 0; i < 32; ++i) { - if (flags & (1U << i)) { - check_insn_set (st, orc_target_get_flag_name (target, i)); - } - } - } - } -#endif - - /* Per channel data */ - st->last_sample = (spx_int32_t *) speex_alloc (nb_channels * sizeof (int)); - st->magic_samples = (spx_uint32_t *) speex_alloc (nb_channels * sizeof (int)); - st->samp_frac_num = (spx_uint32_t *) speex_alloc (nb_channels * sizeof (int)); - for (i = 0; i < nb_channels; i++) { - st->last_sample[i] = 0; - st->magic_samples[i] = 0; - st->samp_frac_num[i] = 0; - } - - speex_resampler_set_quality (st, quality); - speex_resampler_set_rate_frac (st, ratio_num, ratio_den, in_rate, out_rate); - - if (sinc_filter_mode == RESAMPLER_SINC_FILTER_AUTO) { - /* - Estimate how big the filter table would become if the full mode were to be used - calculations used correspond to the ones in update_filter() - if the size is bigger than the threshold, use interpolated sinc instead - */ - spx_uint32_t base_filter_length = st->filt_len = - quality_map[st->quality].base_length; - spx_uint32_t filter_table_size = - base_filter_length * st->den_rate * sizeof (spx_uint16_t); - st->use_full_sinc_table = - (filter_table_size > sinc_filter_auto_threshold) ? 0 : 1; - } - - update_filter (st); - - st->initialised = 1; - if (err) - *err = RESAMPLER_ERR_SUCCESS; - - return st; -} - -EXPORT void -speex_resampler_destroy (SpeexResamplerState * st) -{ - speex_free (st->mem); - speex_free (st->sinc_table); - speex_free (st->last_sample); - speex_free (st->magic_samples); - speex_free (st->samp_frac_num); - speex_free (st); -} - -static int -speex_resampler_process_native (SpeexResamplerState * st, - spx_uint32_t channel_index, spx_uint32_t * in_len, spx_word16_t * out, - spx_uint32_t * out_len) -{ - int j = 0; - const int N = st->filt_len; - int out_sample = 0; - spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size; - spx_uint32_t ilen; - - st->started = 1; - - /* Call the right resampler through the function ptr */ - out_sample = st->resampler_ptr (st, channel_index, mem, in_len, out, out_len); - - if (st->last_sample[channel_index] < (spx_int32_t) * in_len) - *in_len = st->last_sample[channel_index]; - *out_len = out_sample; - st->last_sample[channel_index] -= *in_len; - - ilen = *in_len; - - for (j = 0; j < N - 1; ++j) - mem[j] = mem[j + ilen]; - - return RESAMPLER_ERR_SUCCESS; -} - -static int -speex_resampler_magic (SpeexResamplerState * st, spx_uint32_t channel_index, - spx_word16_t ** out, spx_uint32_t out_len) -{ - spx_uint32_t tmp_in_len = st->magic_samples[channel_index]; - spx_word16_t *mem = st->mem + channel_index * st->mem_alloc_size; - const int N = st->filt_len; - - speex_resampler_process_native (st, channel_index, &tmp_in_len, *out, - &out_len); - - st->magic_samples[channel_index] -= tmp_in_len; - - /* If we couldn't process all "magic" input samples, save the rest for next time */ - if (st->magic_samples[channel_index]) { - spx_uint32_t i; - for (i = 0; i < st->magic_samples[channel_index]; i++) - mem[N - 1 + i] = mem[N - 1 + i + tmp_in_len]; - } - *out += out_len * st->out_stride; - return out_len; -} - -#ifdef FIXED_POINT -EXPORT int -speex_resampler_process_int (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_int16_t * in, spx_uint32_t * in_len, - spx_int16_t * out, spx_uint32_t * out_len) -#else -#ifdef DOUBLE_PRECISION -EXPORT int -speex_resampler_process_float (SpeexResamplerState * st, - spx_uint32_t channel_index, const double *in, spx_uint32_t * in_len, - double *out, spx_uint32_t * out_len) -#else -EXPORT int -speex_resampler_process_float (SpeexResamplerState * st, - spx_uint32_t channel_index, const float *in, spx_uint32_t * in_len, - float *out, spx_uint32_t * out_len) -#endif -#endif -{ - int j; - spx_uint32_t ilen = *in_len; - spx_uint32_t olen = *out_len; - spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size; - const int filt_offs = st->filt_len - 1; - const spx_uint32_t xlen = st->mem_alloc_size - filt_offs; - const int istride = st->in_stride; - - if (st->magic_samples[channel_index]) - olen -= speex_resampler_magic (st, channel_index, &out, olen); - if (!st->magic_samples[channel_index]) { - while (ilen) { - spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen; - spx_uint32_t ochunk = olen; - - if (in) { - for (j = 0; j < ichunk; ++j) - x[j + filt_offs] = in[j * istride]; - } else { - for (j = 0; j < ichunk; ++j) - x[j + filt_offs] = 0; - } - speex_resampler_process_native (st, channel_index, &ichunk, out, &ochunk); - ilen -= ichunk; - olen -= ochunk; - out += ochunk * st->out_stride; - if (in) - in += ichunk * istride; - if (olen == 0 && ichunk == 0) - break; - } - } - *in_len -= ilen; - *out_len -= olen; - return RESAMPLER_ERR_SUCCESS; -} - -#ifdef FIXED_POINT -EXPORT int -speex_resampler_process_float (SpeexResamplerState * st, - spx_uint32_t channel_index, const float *in, spx_uint32_t * in_len, - float *out, spx_uint32_t * out_len) -#else -EXPORT int -speex_resampler_process_int (SpeexResamplerState * st, - spx_uint32_t channel_index, const spx_int16_t * in, spx_uint32_t * in_len, - spx_int16_t * out, spx_uint32_t * out_len) -#endif -{ - int j; - const int istride_save = st->in_stride; - const int ostride_save = st->out_stride; - spx_uint32_t ilen = *in_len; - spx_uint32_t olen = *out_len; - spx_word16_t *x = st->mem + channel_index * st->mem_alloc_size; - const spx_uint32_t xlen = st->mem_alloc_size - (st->filt_len - 1); -#ifdef VAR_ARRAYS - const unsigned int ylen = - (olen < FIXED_STACK_ALLOC) ? olen : FIXED_STACK_ALLOC; - VARDECL (spx_word16_t * ystack); - ALLOC (ystack, ylen, spx_word16_t); -#else - const unsigned int ylen = FIXED_STACK_ALLOC; - spx_word16_t ystack[FIXED_STACK_ALLOC]; -#endif - - st->out_stride = 1; - - while (ilen) { - spx_word16_t *y = ystack; - spx_uint32_t ichunk = (ilen > xlen) ? xlen : ilen; - spx_uint32_t ochunk = (olen > ylen) ? ylen : olen; - spx_uint32_t omagic = 0; - - if (st->magic_samples[channel_index]) { - omagic = speex_resampler_magic (st, channel_index, &y, ochunk); - ochunk -= omagic; - olen -= omagic; - } - if (!st->magic_samples[channel_index]) { - if (in) { - for (j = 0; j < ichunk; ++j) -#ifdef FIXED_POINT - x[j + st->filt_len - 1] = WORD2INT (in[j * istride_save]); -#else - x[j + st->filt_len - 1] = in[j * istride_save]; -#endif - } else { - for (j = 0; j < ichunk; ++j) - x[j + st->filt_len - 1] = 0; - } - - speex_resampler_process_native (st, channel_index, &ichunk, y, &ochunk); - } else { - ichunk = 0; - ochunk = 0; - } - - for (j = 0; j < ochunk + omagic; ++j) -#ifdef FIXED_POINT - out[j * ostride_save] = ystack[j]; -#else - out[j * ostride_save] = WORD2INT (ystack[j]); -#endif - - ilen -= ichunk; - olen -= ochunk; - out += (ochunk + omagic) * ostride_save; - if (in) - in += ichunk * istride_save; - if (olen == 0 && ichunk == 0) - break; - } - st->out_stride = ostride_save; - *in_len -= ilen; - *out_len -= olen; - - return RESAMPLER_ERR_SUCCESS; -} - -#ifdef DOUBLE_PRECISION -EXPORT int -speex_resampler_process_interleaved_float (SpeexResamplerState * st, - const double *in, spx_uint32_t * in_len, double *out, - spx_uint32_t * out_len) -#else -EXPORT int -speex_resampler_process_interleaved_float (SpeexResamplerState * st, - const float *in, spx_uint32_t * in_len, float *out, spx_uint32_t * out_len) -#endif -{ - spx_uint32_t i; - int istride_save, ostride_save; - spx_uint32_t bak_len = *out_len; - istride_save = st->in_stride; - ostride_save = st->out_stride; - st->in_stride = st->out_stride = st->nb_channels; - for (i = 0; i < st->nb_channels; i++) { - *out_len = bak_len; - if (in != NULL) - speex_resampler_process_float (st, i, in + i, in_len, out + i, out_len); - else - speex_resampler_process_float (st, i, NULL, in_len, out + i, out_len); - } - st->in_stride = istride_save; - st->out_stride = ostride_save; - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT int -speex_resampler_process_interleaved_int (SpeexResamplerState * st, - const spx_int16_t * in, spx_uint32_t * in_len, spx_int16_t * out, - spx_uint32_t * out_len) -{ - spx_uint32_t i; - int istride_save, ostride_save; - spx_uint32_t bak_len = *out_len; - istride_save = st->in_stride; - ostride_save = st->out_stride; - st->in_stride = st->out_stride = st->nb_channels; - for (i = 0; i < st->nb_channels; i++) { - *out_len = bak_len; - if (in != NULL) - speex_resampler_process_int (st, i, in + i, in_len, out + i, out_len); - else - speex_resampler_process_int (st, i, NULL, in_len, out + i, out_len); - } - st->in_stride = istride_save; - st->out_stride = ostride_save; - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT int -speex_resampler_set_rate (SpeexResamplerState * st, spx_uint32_t in_rate, - spx_uint32_t out_rate) -{ - return speex_resampler_set_rate_frac (st, in_rate, out_rate, in_rate, - out_rate); -} - -EXPORT void -speex_resampler_get_rate (SpeexResamplerState * st, spx_uint32_t * in_rate, - spx_uint32_t * out_rate) -{ - *in_rate = st->in_rate; - *out_rate = st->out_rate; -} - -EXPORT 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) -{ - 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 <= IMIN (st->num_rate, st->den_rate); fact++) { - while ((st->num_rate % fact == 0) && (st->den_rate % fact == 0)) { - st->num_rate /= fact; - st->den_rate /= fact; - } - } - - if (old_den > 0) { - for (i = 0; i < st->nb_channels; i++) { - st->samp_frac_num[i] = - (gint64) st->samp_frac_num[i] * (gint64) 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; -} - -EXPORT void -speex_resampler_get_ratio (SpeexResamplerState * st, spx_uint32_t * ratio_num, - spx_uint32_t * ratio_den) -{ - *ratio_num = st->num_rate; - *ratio_den = st->den_rate; -} - -EXPORT int -speex_resampler_set_quality (SpeexResamplerState * st, int quality) -{ - if (quality > 10 || quality < 0) - return RESAMPLER_ERR_INVALID_ARG; - if (st->quality == quality) - return RESAMPLER_ERR_SUCCESS; - st->quality = quality; - if (st->initialised) - update_filter (st); - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT void -speex_resampler_get_quality (SpeexResamplerState * st, int *quality) -{ - *quality = st->quality; -} - -EXPORT void -speex_resampler_set_input_stride (SpeexResamplerState * st, spx_uint32_t stride) -{ - st->in_stride = stride; -} - -EXPORT void -speex_resampler_get_input_stride (SpeexResamplerState * st, - spx_uint32_t * stride) -{ - *stride = st->in_stride; -} - -EXPORT void -speex_resampler_set_output_stride (SpeexResamplerState * st, - spx_uint32_t stride) -{ - st->out_stride = stride; -} - -EXPORT void -speex_resampler_get_output_stride (SpeexResamplerState * st, - spx_uint32_t * stride) -{ - *stride = st->out_stride; -} - -EXPORT int -speex_resampler_get_input_latency (SpeexResamplerState * st) -{ - return st->filt_len / 2; -} - -EXPORT int -speex_resampler_get_output_latency (SpeexResamplerState * st) -{ - return ((st->filt_len / 2) * st->den_rate + - (st->num_rate >> 1)) / st->num_rate; -} - -EXPORT int -speex_resampler_get_filt_len (SpeexResamplerState * st) -{ - return st->filt_len; -} - -EXPORT int -speex_resampler_get_sinc_filter_mode (SpeexResamplerState * st) -{ - return st->use_full_sinc_table; -} - -EXPORT int -speex_resampler_skip_zeros (SpeexResamplerState * st) -{ - spx_uint32_t i; - for (i = 0; i < st->nb_channels; i++) - st->last_sample[i] = st->filt_len / 2; - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT int -speex_resampler_reset_mem (SpeexResamplerState * st) -{ - spx_uint32_t i; - for (i = 0; i < st->nb_channels * (st->filt_len - 1); i++) - st->mem[i] = 0; - return RESAMPLER_ERR_SUCCESS; -} - -EXPORT const char * -speex_resampler_strerror (int err) -{ - switch (err) { - case RESAMPLER_ERR_SUCCESS: - return "Success."; - case RESAMPLER_ERR_ALLOC_FAILED: - return "Memory allocation failed."; - case RESAMPLER_ERR_BAD_STATE: - return "Bad resampler state."; - case RESAMPLER_ERR_INVALID_ARG: - return "Invalid argument."; - case RESAMPLER_ERR_PTR_OVERLAP: - return "Input and output buffers overlap."; - default: - return "Unknown error. Bad error code or strange version mismatch."; - } -} diff --git a/gst/audioresample/resample_neon.h b/gst/audioresample/resample_neon.h deleted file mode 100644 index 478488f934..0000000000 --- a/gst/audioresample/resample_neon.h +++ /dev/null @@ -1,202 +0,0 @@ -/* Copyright (C) 2007-2008 Jean-Marc Valin - * Copyright (C) 2008 Thorvald Natvig - * Copyright (C) 2011 Texas Instruments - * author Jyri Sarha - */ -/** - @file resample_neon.h - @brief Resampler functions (NEON version) -*/ -/* - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - - Neither the name of the Xiph.org Foundation nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#include - -#ifdef FIXED_POINT -#ifdef __thumb2__ -static inline int32_t saturate_32bit_to_16bit(int32_t a) { - int32_t ret; - asm ("ssat %[ret], #16, %[a]" - : [ret] "=&r" (ret) - : [a] "r" (a) - : ); - return ret; -} -#else -static inline int32_t saturate_32bit_to_16bit(int32_t a) { - int32_t ret; - asm ("vmov.s32 d0[0], %[a]\n" - "vqmovn.s32 d0, q0\n" - "vmov.s16 %[ret], d0[0]\n" - : [ret] "=&r" (ret) - : [a] "r" (a) - : "q0"); - return ret; -} -#endif -#undef WORD2INT -#define WORD2INT(x) (saturate_32bit_to_16bit(x)) - -#define OVERRIDE_INNER_PRODUCT_SINGLE -/* Only works when len % 4 == 0 */ -static inline int32_t inner_product_single(const int16_t *a, const int16_t *b, unsigned int len) -{ - int32_t ret; - uint32_t remainder = len % 16; - len = len - remainder; - - asm volatile (" cmp %[len], #0\n" - " bne 1f\n" - " vld1.16 {d16}, [%[b]]!\n" - " vld1.16 {d20}, [%[a]]!\n" - " subs %[remainder], %[remainder], #4\n" - " vmull.s16 q0, d16, d20\n" - " beq 5f\n" - " b 4f\n" - "1:" - " vld1.16 {d16, d17, d18, d19}, [%[b]]!\n" - " vld1.16 {d20, d21, d22, d23}, [%[a]]!\n" - " subs %[len], %[len], #16\n" - " vmull.s16 q0, d16, d20\n" - " vmlal.s16 q0, d17, d21\n" - " vmlal.s16 q0, d18, d22\n" - " vmlal.s16 q0, d19, d23\n" - " beq 3f\n" - "2:" - " vld1.16 {d16, d17, d18, d19}, [%[b]]!\n" - " vld1.16 {d20, d21, d22, d23}, [%[a]]!\n" - " subs %[len], %[len], #16\n" - " vmlal.s16 q0, d16, d20\n" - " vmlal.s16 q0, d17, d21\n" - " vmlal.s16 q0, d18, d22\n" - " vmlal.s16 q0, d19, d23\n" - " bne 2b\n" - "3:" - " cmp %[remainder], #0\n" - " beq 5f\n" - "4:" - " vld1.16 {d16}, [%[b]]!\n" - " vld1.16 {d20}, [%[a]]!\n" - " subs %[remainder], %[remainder], #4\n" - " vmlal.s16 q0, d16, d20\n" - " bne 4b\n" - "5:" - " vaddl.s32 q0, d0, d1\n" - " vadd.s64 d0, d0, d1\n" - " vqmovn.s64 d0, q0\n" - " vqrshrn.s32 d0, q0, #15\n" - " vmov.s16 %[ret], d0[0]\n" - : [ret] "=&r" (ret), [a] "+r" (a), [b] "+r" (b), - [len] "+r" (len), [remainder] "+r" (remainder) - : - : "cc", "q0", - "d16", "d17", "d18", "d19", - "d20", "d21", "d22", "d23"); - - return ret; -} -#elif defined(FLOATING_POINT) - -static inline int32_t saturate_float_to_16bit(float a) { - int32_t ret; - asm ("vmov.f32 d0[0], %[a]\n" - "vcvt.s32.f32 d0, d0, #15\n" - "vqrshrn.s32 d0, q0, #15\n" - "vmov.s16 %[ret], d0[0]\n" - : [ret] "=&r" (ret) - : [a] "r" (a) - : "q0"); - return ret; -} -#undef WORD2INT -#define WORD2INT(x) (saturate_float_to_16bit(x)) - -#define OVERRIDE_INNER_PRODUCT_SINGLE -/* Only works when len % 4 == 0 */ -static inline float inner_product_single(const float *a, const float *b, unsigned int len) -{ - float ret; - uint32_t remainder = len % 16; - len = len - remainder; - - asm volatile (" cmp %[len], #0\n" - " bne 1f\n" - " vld1.32 {q4}, [%[b]]!\n" - " vld1.32 {q8}, [%[a]]!\n" - " subs %[remainder], %[remainder], #4\n" - " vmul.f32 q0, q4, q8\n" - " bne 4f\n" - " b 5f\n" - "1:" - " vld1.32 {q4, q5}, [%[b]]!\n" - " vld1.32 {q8, q9}, [%[a]]!\n" - " vld1.32 {q6, q7}, [%[b]]!\n" - " vld1.32 {q10, q11}, [%[a]]!\n" - " subs %[len], %[len], #16\n" - " vmul.f32 q0, q4, q8\n" - " vmul.f32 q1, q5, q9\n" - " vmul.f32 q2, q6, q10\n" - " vmul.f32 q3, q7, q11\n" - " beq 3f\n" - "2:" - " vld1.32 {q4, q5}, [%[b]]!\n" - " vld1.32 {q8, q9}, [%[a]]!\n" - " vld1.32 {q6, q7}, [%[b]]!\n" - " vld1.32 {q10, q11}, [%[a]]!\n" - " subs %[len], %[len], #16\n" - " vmla.f32 q0, q4, q8\n" - " vmla.f32 q1, q5, q9\n" - " vmla.f32 q2, q6, q10\n" - " vmla.f32 q3, q7, q11\n" - " bne 2b\n" - "3:" - " vadd.f32 q4, q0, q1\n" - " vadd.f32 q5, q2, q3\n" - " cmp %[remainder], #0\n" - " vadd.f32 q0, q4, q5\n" - " beq 5f\n" - "4:" - " vld1.32 {q6}, [%[b]]!\n" - " vld1.32 {q10}, [%[a]]!\n" - " subs %[remainder], %[remainder], #4\n" - " vmla.f32 q0, q6, q10\n" - " bne 4b\n" - "5:" - " vadd.f32 d0, d0, d1\n" - " vpadd.f32 d0, d0, d0\n" - " vmov.f32 %[ret], d0[0]\n" - : [ret] "=&r" (ret), [a] "+r" (a), [b] "+r" (b), - [len] "+l" (len), [remainder] "+l" (remainder) - : - : "cc", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", - "q9", "q10", "q11"); - return ret; -} -#endif - diff --git a/gst/audioresample/resample_sse.h b/gst/audioresample/resample_sse.h deleted file mode 100644 index 55d1f40da8..0000000000 --- a/gst/audioresample/resample_sse.h +++ /dev/null @@ -1,229 +0,0 @@ -/* Copyright (C) 2007-2008 Jean-Marc Valin - * Copyright (C) 2008 Thorvald Natvig - */ -/** - @file resample_sse.h - @brief Resampler functions (SSE version) -*/ -/* - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - - Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - - Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - - Neither the name of the Xiph.org Foundation nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -#ifdef HAVE_XMMINTRIN_H -#include -#endif - -#define OVERRIDE_INNER_PRODUCT_SINGLE -static inline float inner_product_single(const float *a, const float *b, unsigned int len) -{ - int i = 0; - float ret = 0; - __m128 sum = _mm_setzero_ps(); - - if (len > 7) - { - for (;i 1) - { - for(;i -#endif -#define OVERRIDE_INNER_PRODUCT_DOUBLE - -#ifdef DOUBLE_PRECISION -static inline double inner_product_double(const double *a, const double *b, unsigned int len) -{ - int i = 0; - double ret = 0; - __m128d sum = _mm_setzero_pd(); - - if (len > 3) - { - for (;i 7) - { - for (;i 1) - { - for(;i 1) - { - for(;i -#endif - -#endif /* OUTSIDE_SPEEX */ - -#ifdef __cplusplus -extern "C" { -#endif - -#define SPEEX_RESAMPLER_QUALITY_MAX 10 -#define SPEEX_RESAMPLER_QUALITY_MIN 0 -#define SPEEX_RESAMPLER_QUALITY_DEFAULT 4 -#define SPEEX_RESAMPLER_QUALITY_VOIP 3 -#define SPEEX_RESAMPLER_QUALITY_DESKTOP 5 - -enum { - RESAMPLER_ERR_SUCCESS = 0, - RESAMPLER_ERR_ALLOC_FAILED = 1, - RESAMPLER_ERR_BAD_STATE = 2, - RESAMPLER_ERR_INVALID_ARG = 3, - RESAMPLER_ERR_PTR_OVERLAP = 4, - - RESAMPLER_ERR_MAX_ERROR -}; - -typedef enum { - RESAMPLER_SINC_FILTER_INTERPOLATED = 0, - RESAMPLER_SINC_FILTER_FULL = 1, - RESAMPLER_SINC_FILTER_AUTO = 2 -} SpeexResamplerSincFilterMode; - -#define RESAMPLER_SINC_FILTER_DEFAULT RESAMPLER_SINC_FILTER_INTERPOLATED -#define RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT (1 * 1048576) - -struct SpeexResamplerState_; -typedef struct SpeexResamplerState_ SpeexResamplerState; - -/** Create a new resampler with integer input and output rates. - * @param nb_channels Number of channels to be processed - * @param in_rate Input sampling rate (integer number of Hz). - * @param out_rate Output sampling rate (integer number of Hz). - * @param quality Resampling quality between 0 and 10, where 0 has poor quality - * and 10 has very high quality. - * @param sinc_filter_mode Sinc filter table mode to use - * @param sinc_filter_auto_threshold Threshold to use if sinc filter mode is auto, in bytes - * @return Newly created resampler state - * @retval NULL Error: not enough memory - * - * If a full filter table would be larger than the auto threshold, and sinc_filter_mode is AUTO, - * the resample uses the interpolated mode instead - * - * @note A full sinc table can significantly improve the resampler's performance, but calculating the table - * takes longer, as opposed to the interpolated variant - */ -SpeexResamplerState *speex_resampler_init(spx_uint32_t nb_channels, - spx_uint32_t in_rate, - spx_uint32_t out_rate, - int quality, - SpeexResamplerSincFilterMode sinc_filter_mode, - spx_uint32_t sinc_filter_auto_threshold, - int *err); - -/** Create a new resampler with fractional input/output rates. The sampling - * rate ratio is an arbitrary rational number with both the numerator and - * denominator being 32-bit integers. - * @param nb_channels Number of channels to be processed - * @param ratio_num Numerator of the sampling rate ratio - * @param ratio_den Denominator of the sampling rate ratio - * @param in_rate Input sampling rate rounded to the nearest integer (in Hz). - * @param out_rate Output sampling rate rounded to the nearest integer (in Hz). - * @param quality Resampling quality between 0 and 10, where 0 has poor quality - * and 10 has very high quality. - * @param sinc_filter_mode Sinc filter table mode to use - * @param sinc_filter_auto_threshold Threshold to use if sinc filter mode is auto, in bytes - * @return Newly created resampler state - * @retval NULL Error: not enough memory - * - * If a full filter table would be larger than the auto threshold, and sinc_filter_mode is AUTO, - * the resample uses the interpolated mode instead - * - * @note A full sinc table can significantly improve the resampler's performance, but calculating the table - * takes longer, as opposed to the interpolated variant - */ -SpeexResamplerState *speex_resampler_init_frac(spx_uint32_t nb_channels, - spx_uint32_t ratio_num, - spx_uint32_t ratio_den, - spx_uint32_t in_rate, - spx_uint32_t out_rate, - int quality, - SpeexResamplerSincFilterMode sinc_filter_mode, - spx_uint32_t sinc_filter_auto_threshold, - int *err); - -/** Destroy a resampler state. - * @param st Resampler state - */ -void speex_resampler_destroy(SpeexResamplerState *st); - -/** Resample a float array. The input and output buffers must *not* overlap. - * @param st Resampler state - * @param channel_index Index of the channel to process for the multi-channel - * base (0 otherwise) - * @param in Input buffer - * @param in_len Number of input samples in the input buffer. Returns the - * number of samples processed - * @param out Output buffer - * @param out_len Size of the output buffer. Returns the number of samples written - */ -#ifdef DOUBLE_PRECISION -int speex_resampler_process_float(SpeexResamplerState *st, - spx_uint32_t channel_index, - const double *in, - spx_uint32_t *in_len, - double *out, - spx_uint32_t *out_len); -#else -int speex_resampler_process_float(SpeexResamplerState *st, - spx_uint32_t channel_index, - const float *in, - spx_uint32_t *in_len, - float *out, - spx_uint32_t *out_len); -#endif - -/** Resample an int array. The input and output buffers must *not* overlap. - * @param st Resampler state - * @param channel_index Index of the channel to process for the multi-channel - * base (0 otherwise) - * @param in Input buffer - * @param in_len Number of input samples in the input buffer. Returns the number - * of samples processed - * @param out Output buffer - * @param out_len Size of the output buffer. Returns the number of samples written - */ -int speex_resampler_process_int(SpeexResamplerState *st, - spx_uint32_t channel_index, - const spx_int16_t *in, - spx_uint32_t *in_len, - spx_int16_t *out, - spx_uint32_t *out_len); - -/** Resample an interleaved float array. The input and output buffers must *not* overlap. - * @param st Resampler state - * @param in Input buffer - * @param in_len Number of input samples in the input buffer. Returns the number - * of samples processed. This is all per-channel. - * @param out Output buffer - * @param out_len Size of the output buffer. Returns the number of samples written. - * This is all per-channel. - */ -#ifdef DOUBLE_PRECISION -int speex_resampler_process_interleaved_float(SpeexResamplerState *st, - const double *in, - spx_uint32_t *in_len, - double *out, - spx_uint32_t *out_len); -#else -int speex_resampler_process_interleaved_float(SpeexResamplerState *st, - const float *in, - spx_uint32_t *in_len, - float *out, - spx_uint32_t *out_len); -#endif - -/** Resample an interleaved int array. The input and output buffers must *not* overlap. - * @param st Resampler state - * @param in Input buffer - * @param in_len Number of input samples in the input buffer. Returns the number - * of samples processed. This is all per-channel. - * @param out Output buffer - * @param out_len Size of the output buffer. Returns the number of samples written. - * This is all per-channel. - */ -int speex_resampler_process_interleaved_int(SpeexResamplerState *st, - const spx_int16_t *in, - spx_uint32_t *in_len, - spx_int16_t *out, - spx_uint32_t *out_len); - -/** Set (change) the input/output sampling rates (integer value). - * @param st Resampler state - * @param in_rate Input sampling rate (integer number of Hz). - * @param out_rate Output sampling rate (integer number of Hz). - */ -int speex_resampler_set_rate(SpeexResamplerState *st, - spx_uint32_t in_rate, - spx_uint32_t out_rate); - -/** Get the current input/output sampling rates (integer value). - * @param st Resampler state - * @param in_rate Input sampling rate (integer number of Hz) copied. - * @param out_rate Output sampling rate (integer number of Hz) copied. - */ -void speex_resampler_get_rate(SpeexResamplerState *st, - spx_uint32_t *in_rate, - spx_uint32_t *out_rate); - -/** Set (change) the input/output sampling rates and resampling ratio - * (fractional values in Hz supported). - * @param st Resampler state - * @param ratio_num Numerator of the sampling rate ratio - * @param ratio_den Denominator of the sampling rate ratio - * @param in_rate Input sampling rate rounded to the nearest integer (in Hz). - * @param out_rate Output sampling rate rounded to the nearest integer (in Hz). - */ -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); - -/** Get the current resampling ratio. This will be reduced to the least - * common denominator. - * @param st Resampler state - * @param ratio_num Numerator of the sampling rate ratio copied - * @param ratio_den Denominator of the sampling rate ratio copied - */ -void speex_resampler_get_ratio(SpeexResamplerState *st, - spx_uint32_t *ratio_num, - spx_uint32_t *ratio_den); - -/** Set (change) the conversion quality. - * @param st Resampler state - * @param quality Resampling quality between 0 and 10, where 0 has poor - * quality and 10 has very high quality. - */ -int speex_resampler_set_quality(SpeexResamplerState *st, - int quality); - -/** Get the conversion quality. - * @param st Resampler state - * @param quality Resampling quality between 0 and 10, where 0 has poor - * quality and 10 has very high quality. - */ -void speex_resampler_get_quality(SpeexResamplerState *st, - int *quality); - -/** Set (change) the input stride. - * @param st Resampler state - * @param stride Input stride - */ -void speex_resampler_set_input_stride(SpeexResamplerState *st, - spx_uint32_t stride); - -/** Get the input stride. - * @param st Resampler state - * @param stride Input stride copied - */ -void speex_resampler_get_input_stride(SpeexResamplerState *st, - spx_uint32_t *stride); - -/** Set (change) the output stride. - * @param st Resampler state - * @param stride Output stride - */ -void speex_resampler_set_output_stride(SpeexResamplerState *st, - spx_uint32_t stride); - -/** Get the output stride. - * @param st Resampler state copied - * @param stride Output stride - */ -void speex_resampler_get_output_stride(SpeexResamplerState *st, - spx_uint32_t *stride); - -/** Get the latency introduced by the resampler measured in input samples. - * @param st Resampler state - */ -int speex_resampler_get_input_latency(SpeexResamplerState *st); - -/** Get the latency introduced by the resampler measured in output samples. - * @param st Resampler state - */ -int speex_resampler_get_output_latency(SpeexResamplerState *st); - -/** Get the length of the filter in input samples. - * @param st Resampler state - */ -int speex_resampler_get_filt_len(SpeexResamplerState *st); - -/** Returns 1 if the full sinc filter table is used, 0 if the interpolated one is used - * @param st Resampler state - * @return Sinc filter mode - */ -int speex_resampler_get_sinc_filter_mode(SpeexResamplerState *st); - -/** Make sure that the first samples to go out of the resamplers don't have - * leading zeros. This is only useful before starting to use a newly created - * resampler. It is recommended to use that when resampling an audio file, as - * it will generate a file with the same length. For real-time processing, - * it is probably easier not to use this call (so that the output duration - * is the same for the first frame). - * @param st Resampler state - */ -int speex_resampler_skip_zeros(SpeexResamplerState *st); - -/** Reset a resampler so a new (unrelated) stream can be processed. - * @param st Resampler state - */ -int speex_resampler_reset_mem(SpeexResamplerState *st); - -/** Returns the English meaning for an error code - * @param err Error code - * @return English string - */ -const char *speex_resampler_strerror(int err); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/gst/audioresample/speex_resampler_double.c b/gst/audioresample/speex_resampler_double.c deleted file mode 100644 index 6c5d63c342..0000000000 --- a/gst/audioresample/speex_resampler_double.c +++ /dev/null @@ -1,26 +0,0 @@ -/* GStreamer - * Copyright (C) 2007-2008 Sebastian Dröge - * - * 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 library 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., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -#define _USE_SSE2 -#define FLOATING_POINT -#define DOUBLE_PRECISION -#define OUTSIDE_SPEEX -#define RANDOM_PREFIX resample_double - -#include "resample.c" diff --git a/gst/audioresample/speex_resampler_float.c b/gst/audioresample/speex_resampler_float.c deleted file mode 100644 index 4f11197e23..0000000000 --- a/gst/audioresample/speex_resampler_float.c +++ /dev/null @@ -1,27 +0,0 @@ -/* GStreamer - * Copyright (C) 2007-2008 Sebastian Dröge - * - * 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 library 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., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -#define _USE_SSE -#define _USE_SSE2 -#define _USE_NEON -#define FLOATING_POINT -#define OUTSIDE_SPEEX -#define RANDOM_PREFIX resample_float - -#include "resample.c" diff --git a/gst/audioresample/speex_resampler_int.c b/gst/audioresample/speex_resampler_int.c deleted file mode 100644 index beeb045db1..0000000000 --- a/gst/audioresample/speex_resampler_int.c +++ /dev/null @@ -1,26 +0,0 @@ -/* GStreamer - * Copyright (C) 2007-2008 Sebastian Dröge - * - * 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 library 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., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -#define FIXED_POINT 1 -#define OUTSIDE_SPEEX 1 -/* disabled, 16-bit integer NEON support seems broken */ -/* #define _USE_NEON */ -#define RANDOM_PREFIX resample_int - -#include "resample.c" diff --git a/gst/audioresample/speex_resampler_wrapper.h b/gst/audioresample/speex_resampler_wrapper.h deleted file mode 100644 index cd13a88576..0000000000 --- a/gst/audioresample/speex_resampler_wrapper.h +++ /dev/null @@ -1,192 +0,0 @@ -/* GStreamer - * Copyright (C) 2007-2008 Sebastian Dröge - * - * 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 library 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., 51 Franklin St, Fifth Floor, - * Boston, MA 02110-1301, USA. - */ - -#ifndef __SPEEX_RESAMPLER_WRAPPER_H__ -#define __SPEEX_RESAMPLER_WRAPPER_H__ - -#define SPEEX_RESAMPLER_QUALITY_MAX 10 -#define SPEEX_RESAMPLER_QUALITY_MIN 0 -#define SPEEX_RESAMPLER_QUALITY_DEFAULT 4 -#define SPEEX_RESAMPLER_QUALITY_VOIP 3 -#define SPEEX_RESAMPLER_QUALITY_DESKTOP 5 - -#define SPEEX_RESAMPLER_SINC_FILTER_DEFAULT SPEEX_RESAMPLER_SINC_FILTER_AUTO -#define SPEEX_RESAMPLER_SINC_FILTER_AUTO_THRESHOLD_DEFAULT (1 * 1048576) - -enum -{ - RESAMPLER_ERR_SUCCESS = 0, - RESAMPLER_ERR_ALLOC_FAILED = 1, - RESAMPLER_ERR_BAD_STATE = 2, - RESAMPLER_ERR_INVALID_ARG = 3, - RESAMPLER_ERR_PTR_OVERLAP = 4, - - RESAMPLER_ERR_MAX_ERROR -}; - -typedef enum { - SPEEX_RESAMPLER_SINC_FILTER_INTERPOLATED = 0, - SPEEX_RESAMPLER_SINC_FILTER_FULL = 1, - SPEEX_RESAMPLER_SINC_FILTER_AUTO = 2 -} SpeexResamplerSincFilterMode; - -typedef struct SpeexResamplerState_ SpeexResamplerState; - -typedef struct { - SpeexResamplerState *(*init) (guint32 nb_channels, - guint32 in_rate, guint32 out_rate, gint quality, - SpeexResamplerSincFilterMode sinc_filter_mode, - guint32 sinc_filter_auto_threshold, gint * err); - void (*destroy) (SpeexResamplerState * st); - int (*process) (SpeexResamplerState * - st, const guint8 * in, guint32 * in_len, guint8 * out, guint32 * out_len); - int (*set_rate) (SpeexResamplerState * st, - guint32 in_rate, guint32 out_rate); - void (*get_rate) (SpeexResamplerState * st, - guint32 * in_rate, guint32 * out_rate); - void (*get_ratio) (SpeexResamplerState * st, - guint32 * ratio_num, guint32 * ratio_den); - int (*get_input_latency) (SpeexResamplerState * st); - int (*get_filt_len) (SpeexResamplerState * st); - int (*get_sinc_filter_mode) (SpeexResamplerState * st); - int (*set_quality) (SpeexResamplerState * st, gint quality); - int (*reset_mem) (SpeexResamplerState * st); - int (*skip_zeros) (SpeexResamplerState * st); - const char * (*strerror) (gint err); - unsigned int width; -} SpeexResampleFuncs; - -SpeexResamplerState *resample_float_resampler_init (guint32 nb_channels, - guint32 in_rate, guint32 out_rate, gint quality, - SpeexResamplerSincFilterMode sinc_filter_mode, - guint32 sinc_filter_auto_threshold, gint * err); -void resample_float_resampler_destroy (SpeexResamplerState * st); -int resample_float_resampler_process_interleaved_float (SpeexResamplerState * - st, const guint8 * in, guint32 * in_len, guint8 * out, guint32 * out_len); -int resample_float_resampler_set_rate (SpeexResamplerState * st, - guint32 in_rate, guint32 out_rate); -void resample_float_resampler_get_rate (SpeexResamplerState * st, - guint32 * in_rate, guint32 * out_rate); -void resample_float_resampler_get_ratio (SpeexResamplerState * st, - guint32 * ratio_num, guint32 * ratio_den); -int resample_float_resampler_get_input_latency (SpeexResamplerState * st); -int resample_float_resampler_get_filt_len (SpeexResamplerState * st); -int resample_float_resampler_get_sinc_filter_mode (SpeexResamplerState * st); -int resample_float_resampler_set_quality (SpeexResamplerState * st, gint quality); -int resample_float_resampler_reset_mem (SpeexResamplerState * st); -int resample_float_resampler_skip_zeros (SpeexResamplerState * st); -const char * resample_float_resampler_strerror (gint err); - -static const SpeexResampleFuncs float_funcs = -{ - resample_float_resampler_init, - resample_float_resampler_destroy, - resample_float_resampler_process_interleaved_float, - resample_float_resampler_set_rate, - resample_float_resampler_get_rate, - resample_float_resampler_get_ratio, - resample_float_resampler_get_input_latency, - resample_float_resampler_get_filt_len, - resample_float_resampler_get_sinc_filter_mode, - resample_float_resampler_set_quality, - resample_float_resampler_reset_mem, - resample_float_resampler_skip_zeros, - resample_float_resampler_strerror, - 32 -}; - -SpeexResamplerState *resample_double_resampler_init (guint32 nb_channels, - guint32 in_rate, guint32 out_rate, gint quality, - SpeexResamplerSincFilterMode sinc_filter_mode, - guint32 sinc_filter_auto_threshold, gint * err); -void resample_double_resampler_destroy (SpeexResamplerState * st); -int resample_double_resampler_process_interleaved_float (SpeexResamplerState * - st, const guint8 * in, guint32 * in_len, guint8 * out, guint32 * out_len); -int resample_double_resampler_set_rate (SpeexResamplerState * st, - guint32 in_rate, guint32 out_rate); -void resample_double_resampler_get_rate (SpeexResamplerState * st, - guint32 * in_rate, guint32 * out_rate); -void resample_double_resampler_get_ratio (SpeexResamplerState * st, - guint32 * ratio_num, guint32 * ratio_den); -int resample_double_resampler_get_input_latency (SpeexResamplerState * st); -int resample_double_resampler_get_filt_len (SpeexResamplerState * st); -int resample_double_resampler_get_sinc_filter_mode (SpeexResamplerState * st); -int resample_double_resampler_set_quality (SpeexResamplerState * st, gint quality); -int resample_double_resampler_reset_mem (SpeexResamplerState * st); -int resample_double_resampler_skip_zeros (SpeexResamplerState * st); -const char * resample_double_resampler_strerror (gint err); - -static const SpeexResampleFuncs double_funcs = -{ - resample_double_resampler_init, - resample_double_resampler_destroy, - resample_double_resampler_process_interleaved_float, - resample_double_resampler_set_rate, - resample_double_resampler_get_rate, - resample_double_resampler_get_ratio, - resample_double_resampler_get_input_latency, - resample_double_resampler_get_filt_len, - resample_double_resampler_get_sinc_filter_mode, - resample_double_resampler_set_quality, - resample_double_resampler_reset_mem, - resample_double_resampler_skip_zeros, - resample_double_resampler_strerror, - 64 -}; - -SpeexResamplerState *resample_int_resampler_init (guint32 nb_channels, - guint32 in_rate, guint32 out_rate, gint quality, - SpeexResamplerSincFilterMode sinc_filter_mode, - guint32 sinc_filter_auto_threshold, gint * err); -void resample_int_resampler_destroy (SpeexResamplerState * st); -int resample_int_resampler_process_interleaved_int (SpeexResamplerState * - st, const guint8 * in, guint32 * in_len, guint8 * out, guint32 * out_len); -int resample_int_resampler_set_rate (SpeexResamplerState * st, - guint32 in_rate, guint32 out_rate); -void resample_int_resampler_get_rate (SpeexResamplerState * st, - guint32 * in_rate, guint32 * out_rate); -void resample_int_resampler_get_ratio (SpeexResamplerState * st, - guint32 * ratio_num, guint32 * ratio_den); -int resample_int_resampler_get_input_latency (SpeexResamplerState * st); -int resample_int_resampler_get_filt_len (SpeexResamplerState * st); -int resample_int_resampler_get_sinc_filter_mode (SpeexResamplerState * st); -int resample_int_resampler_set_quality (SpeexResamplerState * st, gint quality); -int resample_int_resampler_reset_mem (SpeexResamplerState * st); -int resample_int_resampler_skip_zeros (SpeexResamplerState * st); -const char * resample_int_resampler_strerror (gint err); - -static const SpeexResampleFuncs int_funcs = -{ - resample_int_resampler_init, - resample_int_resampler_destroy, - resample_int_resampler_process_interleaved_int, - resample_int_resampler_set_rate, - resample_int_resampler_get_rate, - resample_int_resampler_get_ratio, - resample_int_resampler_get_input_latency, - resample_int_resampler_get_filt_len, - resample_int_resampler_get_sinc_filter_mode, - resample_int_resampler_set_quality, - resample_int_resampler_reset_mem, - resample_int_resampler_skip_zeros, - resample_int_resampler_strerror, - 16 -}; - -#endif /* __SPEEX_RESAMPLER_WRAPPER_H__ */