]> git.sesse.net Git - vlc/commitdiff
Speex resampler (for FL32 and S16N)
authorRémi Denis-Courmont <remi@remlab.net>
Sun, 16 Oct 2011 14:16:21 +0000 (17:16 +0300)
committerRémi Denis-Courmont <remi@remlab.net>
Sun, 16 Oct 2011 18:33:15 +0000 (21:33 +0300)
This adds a good resampler for integers, while reusing a source package
(speex) that is already dependend on.

Contrary to SRC, the library is BSD and the plugin is LGPL.

NEWS
configure.ac
modules/LIST
modules/audio_filter/Modules.am
modules/audio_filter/resampler/speex.c [new file with mode: 0644]
po/POTFILES.in

diff --git a/NEWS b/NEWS
index 97f1388a4447957b8e573f17121f8fde13841e3b..fa0ddbb074ac143d17f6d8fb1011b537fcb16179 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -134,6 +134,7 @@ Audio Output and Filters:
  * New audio output in memory (amem)
  * Important simplification and improvements in the core audio output
  * New audio output based on OpenSL ES API for Android
+ * New audio resampler using Speex (DSP)
  * New audio resampler using the Secret Rabbit Code (a.k.a. libsamplerate)
  * New Compressor filter, a dynamic range compressor
  * New simplistic Karaoke filter
index 925fd142bd8d08db0aa0a8f57710875e5315dc6b..212d9fd5f4b43fc2cb0887ff1ad2a9a220d9fc11 100644 (file)
@@ -2843,9 +2843,20 @@ then
 fi
 
 dnl
-dnl  Speex plugin
+dnl  Speex plugins
 dnl
-PKG_ENABLE_MODULES_VLC([SPEEX], [], [ogg speex >= 1.0.5], [Speex decoder support], [auto])
+PKG_ENABLE_MODULES_VLC([SPEEX], [], [ogg speex >= 1.0.5], [Speex support], [auto])
+have_speexdsp="no"
+AS_IF([test "${enable_speex}" != "no"], [
+  PKG_CHECK_MODULES([SPEEXDSP], [speexdsp], [
+    have_speexdsp="yes"
+  ], [
+    AS_IF([test "${enable_speex}" = "yes"], [
+      AC_MSG_ERROR([${SPEEXDSP_PKG_ERRORS}.])
+    ])
+  ])
+])
+AM_CONDITIONAL([HAVE_SPEEXDSP], [test "$have_speexdsp" = "yes"])
 
 dnl
 dnl  theora decoder plugin
index ec2509e87ad6f3774ad922c36ebf43cd5eeb6791..28e08a34c005ad28d33ce8cca8a6901add4dc01e 100644 (file)
@@ -296,6 +296,7 @@ $Id$
  * smf: Standard MIDI file demuxer
  * spatializer: A spatializer audio filter
  * speex: a speex audio decoder/packetizer using the libspeex library
+ * speex_resampler: audio resampler using the libspeexdsp library
  * spudec: RLE DVD subtitles decoder
  * sqlite: manage an SQLite database
  * stats: Stats encoder function
index 6643c84eaf48e588a0f89244a453bc7fd3cf9406..8f9e5ef69ce84da8fd25e5d5be3d73fc2c82d6ac 100644 (file)
@@ -64,3 +64,11 @@ libvlc_LTLIBRARIES += \
        libugly_resampler_plugin.la
 EXTRA_LTLIBRARIES += \
        libbandlimited_resampler_plugin.la
+
+libspeex_resampler_plugin_la_SOURCES = resampler/speex.c
+libspeex_resampler_plugin_la_CFLAGS = $(AM_CFLAGS) $(SPEEXDSP_CFLAGS)
+libspeex_resampler_plugin_la_LIBADD = $(AM_LIBADD) $(SPEEXDSP_LIBS)
+libspeex_resampler_plugin_la_DEPENDENCIES =
+if HAVE_SPEEXDSP
+libvlc_LTLIBRARIES += libspeex_resampler_plugin.la
+endif
diff --git a/modules/audio_filter/resampler/speex.c b/modules/audio_filter/resampler/speex.c
new file mode 100644 (file)
index 0000000..06ebec7
--- /dev/null
@@ -0,0 +1,145 @@
+/*****************************************************************************
+ * speex.c : libspeex DSP resampler
+ *****************************************************************************
+ * Copyright © 2011 Rémi Denis-Courmont
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <vlc_common.h>
+#include <vlc_aout.h>
+#include <vlc_filter.h>
+#include <vlc_plugin.h>
+
+#include <speex/speex_resampler.h>
+
+#define QUALITY_TEXT N_("Resampling quality")
+#define QUALITY_LONGTEXT N_( \
+    "Resampling quality (0 = worst and fastest, 10 = best and slowest).")
+
+static int Open (vlc_object_t *);
+static void Close (vlc_object_t *);
+
+vlc_module_begin ()
+    set_shortname (N_("Speex resampler"))
+    set_description (N_("Speex resampler") )
+    set_category (CAT_AUDIO)
+    set_subcategory (SUBCAT_AUDIO_MISC)
+    add_integer ("speex-resampler-quality", 3,
+                 QUALITY_TEXT, QUALITY_LONGTEXT, true)
+        change_integer_range (0, 10)
+    set_capability ("audio filter", 60)
+    set_callbacks (Open, Close)
+vlc_module_end ()
+
+static block_t *Resample (filter_t *, block_t *);
+
+static int Open (vlc_object_t *obj)
+{
+    filter_t *filter = (filter_t *)obj;
+
+    /* Will change rate */
+    if (filter->fmt_in.audio.i_rate == filter->fmt_out.audio.i_rate
+    /* Cannot convert format */
+     || filter->fmt_in.audio.i_format != filter->fmt_out.audio.i_format
+    /* Cannot remix */
+     || filter->fmt_in.audio.i_physical_channels
+                                  != filter->fmt_out.audio.i_physical_channels
+     || filter->fmt_in.audio.i_original_channels
+                                  != filter->fmt_out.audio.i_original_channels)
+        return VLC_EGENERIC;
+
+    switch (filter->fmt_in.audio.i_format)
+    {
+        case VLC_CODEC_FL32: break;
+        case VLC_CODEC_S16N: break;
+        default:             return VLC_EGENERIC;
+    }
+
+    SpeexResamplerState *st;
+
+    unsigned channels = aout_FormatNbChannels (&filter->fmt_in.audio);
+    unsigned q = var_InheritInteger (obj, "speex-resampler-quality");
+    if (unlikely(q > 10))
+        q = 3;
+
+    int err;
+    st = speex_resampler_init(channels, filter->fmt_in.audio.i_rate,
+                              filter->fmt_out.audio.i_rate, q, &err);
+    if (unlikely(st == NULL))
+    {
+        msg_Err (obj, "cannot initialize resampler: %s",
+                 speex_resampler_strerror (err));
+        return VLC_ENOMEM;
+    }
+
+    filter->p_sys = (filter_sys_t *)st;
+    filter->pf_audio_filter = Resample;
+    return VLC_SUCCESS;
+}
+
+static void Close (vlc_object_t *obj)
+{
+    filter_t *filter = (filter_t *)obj;
+    SpeexResamplerState *st = (SpeexResamplerState *)filter->p_sys;
+
+    speex_resampler_destroy (st);
+}
+
+static block_t *Resample (filter_t *filter, block_t *in)
+{
+    SpeexResamplerState *st = (SpeexResamplerState *)filter->p_sys;
+
+    const size_t framesize = filter->fmt_out.audio.i_bytes_per_frame;
+    const unsigned irate = filter->fmt_in.audio.i_rate;
+    const unsigned orate = filter->fmt_out.audio.i_rate;
+
+    spx_uint32_t ilen = in->i_nb_samples;
+    spx_uint32_t olen = ((ilen + 1) * orate) / irate;
+
+    block_t *out = block_Alloc (olen * framesize);
+    if (unlikely(out == NULL))
+        goto error;
+
+    speex_resampler_set_rate (st, irate, orate);
+
+    int err;
+    if (filter->fmt_in.audio.i_format == VLC_CODEC_FL32)
+        err = speex_resampler_process_interleaved_float (st,
+            (float *)in->p_buffer, &ilen, (float *)out->p_buffer, &olen);
+    else
+        err = speex_resampler_process_interleaved_int (st,
+            (int16_t *)in->p_buffer, &ilen, (int16_t *)out->p_buffer, &olen);
+    if (err != 0)
+    {
+        msg_Err (filter, "cannot resample: %s",
+                 speex_resampler_strerror (err));
+        block_Release (out);
+        out = NULL;
+        goto error;
+    }
+
+    out->i_buffer = olen * framesize;
+    out->i_nb_samples = olen;
+    out->i_pts = in->i_pts;
+    out->i_length = olen * CLOCK_FREQ / filter->fmt_out.audio.i_rate;
+error:
+    block_Release (in);
+    return out;
+}
index 8f7b86d0224d10b80edcc09f6ac988930a3fce51..69b96fd99341794a5785a1d652e149823ab26516 100644 (file)
@@ -306,6 +306,7 @@ modules/audio_filter/normvol.c
 modules/audio_filter/param_eq.c
 modules/audio_filter/resampler/bandlimited.c
 modules/audio_filter/resampler/bandlimited.h
+modules/audio_filter/resampler/speex.c
 modules/audio_filter/resampler/src.c
 modules/audio_filter/resampler/ugly.c
 modules/audio_filter/scaletempo.c