[vlc-devel] [PATCH] Speex resampler (for FL32 and S16N)

Rémi Denis-Courmont remi at remlab.net
Sun Oct 16 16:18:43 CEST 2011


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                                   |    1 +
 configure.ac                           |   15 +++-
 modules/LIST                           |    1 +
 modules/audio_filter/Modules.am        |    8 ++
 modules/audio_filter/resampler/speex.c |  146 ++++++++++++++++++++++++++++++++
 po/POTFILES.in                         |    1 +
 6 files changed, 170 insertions(+), 2 deletions(-)
 create mode 100644 modules/audio_filter/resampler/speex.c

diff --git a/NEWS b/NEWS
index 97f1388..fa0ddbb 100644
--- 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
diff --git a/configure.ac b/configure.ac
index 925fd14..bb2187e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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}"], [
+  PKG_CHECK_MODULES([SPEEXDSP], [speexdsp], [
+    have_speexdsp="yes"
+  ], [
+    AS_IF([test -n "${enable_speex}"], [
+      AC_MSG_ERROR([$SPEEXDSP_PKG_ERRORS])
+    ])
+  ])
+])
+AM_CONDITIONAL([HAVE_SPEEXDSP], [test "$have_speexdsp" = "yes"])
 
 dnl
 dnl  theora decoder plugin
diff --git a/modules/LIST b/modules/LIST
index ec2509e..28e08a3 100644
--- a/modules/LIST
+++ b/modules/LIST
@@ -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
diff --git a/modules/audio_filter/Modules.am b/modules/audio_filter/Modules.am
index 6643c84..8f9e5ef 100644
--- a/modules/audio_filter/Modules.am
+++ b/modules/audio_filter/Modules.am
@@ -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
index 0000000..866a359
--- /dev/null
+++ b/modules/audio_filter/resampler/speex.c
@@ -0,0 +1,146 @@
+/*****************************************************************************
+ * 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>
+#include <math.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;
+}
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 8f7b86d..69b96fd 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -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
-- 
1.7.7




More information about the vlc-devel mailing list