[FFmpeg-devel] [PATCH] avfilter: add stereo tools filter
Paul B Mahol
onemda at gmail.com
Tue Sep 15 19:26:00 CEST 2015
Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
doc/filters.texi | 84 +++++++++++++
libavfilter/Makefile | 1 +
libavfilter/af_stereotools.c | 290 +++++++++++++++++++++++++++++++++++++++++++
libavfilter/allfilters.c | 1 +
4 files changed, 376 insertions(+)
create mode 100644 libavfilter/af_stereotools.c
diff --git a/doc/filters.texi b/doc/filters.texi
index c4360fa..9f17cfd 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -2394,6 +2394,90 @@ silenceremove=1:5:0.02
@end example
@end itemize
+ at section stereotools
+
+This filter have some handy utilities to manage stereo signals, for converting
+M/S stereo recordings to L/R signal while having control over the parameters
+or spreading the stereo image of master track.
+
+The filter accepts the following options:
+
+ at table @option
+ at item ibalance
+Set input balance between both channels. Default is 0.
+Allowed range is from -1 to 1.
+
+ at item obalance
+Set output balance between both channels. Default is 0.
+Allowed range is from -1 to 1.
+
+ at item softclip
+Enable softclipping. This is analog distortion instead of harsh digital 0dB
+clipping. By default is disabled.
+
+ at item mutel
+Mute the left channel.
+
+ at item muter
+Mute the right channel.
+
+ at item phasel
+Change the phase of the left channel.
+
+ at item phaser
+Change the phase of the right channel.
+
+ at item mode
+Set stereo mode. Available values are:
+
+ at table @samp
+ at item lr>lr
+Left/Right to Left/Right.
+
+ at item lr>ms
+Left/Right to Mid/Side.
+
+ at item ms>lr
+Mid/Side to Left/Right.
+
+ at item lr>ll
+Left/Right to Left/Left.
+
+ at item lr>rr
+Left/Right to Right/Right.
+
+ at item lr>l+r
+Left/Right to Left + Right
+
+ at item lr>rl
+Left/Right to Right/Left
+ at end table
+
+ at item slev
+Set level of side signal. Default is 1.
+
+ at item sbal
+Set balance of side signal. Default is 0.
+
+ at item mlev
+Set level of the middle signal. Default is 1.
+
+ at item mpan
+Set middle signal pan. Default is 0.
+
+ at item base
+Set stereo base. Default is 0.
+
+ at item delay
+Set delay in milliseconds. Default is 0. Allowed range is from -20 to -20.
+
+ at item sclevel
+Set S/C level. Default is 1. Allowed range is from 1 to 100.
+
+ at item phase
+Set the stereo phase in degrees. Default is 0. Allowed range is from 0 to 360.
+ at end table
+
@section stereowiden
This filter enhance the stereo effect by suppressing signal common to both
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 03e18d2..05effd6 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -80,6 +80,7 @@ OBJS-$(CONFIG_RESAMPLE_FILTER) += af_resample.o
OBJS-$(CONFIG_SIDECHAINCOMPRESS_FILTER) += af_sidechaincompress.o
OBJS-$(CONFIG_SILENCEDETECT_FILTER) += af_silencedetect.o
OBJS-$(CONFIG_SILENCEREMOVE_FILTER) += af_silenceremove.o
+OBJS-$(CONFIG_STEREOTOOLS_FILTER) += af_stereotools.o
OBJS-$(CONFIG_STEREOWIDEN_FILTER) += af_stereowiden.o
OBJS-$(CONFIG_TREBLE_FILTER) += af_biquads.o
OBJS-$(CONFIG_VOLUME_FILTER) += af_volume.o
diff --git a/libavfilter/af_stereotools.c b/libavfilter/af_stereotools.c
new file mode 100644
index 0000000..a659294
--- /dev/null
+++ b/libavfilter/af_stereotools.c
@@ -0,0 +1,290 @@
+/*
+ * Copyright (C) 2001-2010 Krzysztof Foltman, Markus Schmidt, Thor Harald Johansen
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/channel_layout.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "audio.h"
+#include "formats.h"
+
+typedef struct StereoToolsContext {
+ const AVClass *class;
+
+ int softclip;
+ int mute_l;
+ int mute_r;
+ int phase_l;
+ int phase_r;
+ int mode;
+ double slev;
+ double sbal;
+ double mlev;
+ double mpan;
+ double phase;
+ double base;
+ double delay;
+ double balance_in;
+ double balance_out;
+ double phase_sin_coef;
+ double phase_cos_coef;
+ double sc_level;
+ double inv_atan_shape;
+
+ double *buffer;
+ int length;
+ int pos;
+} StereoToolsContext;
+
+#define OFFSET(x) offsetof(StereoToolsContext, x)
+#define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption stereotools_options[] = {
+ { "ibalance", "set balance in", OFFSET(balance_in), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
+ { "obalance", "set balance out", OFFSET(balance_out), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
+ { "softclip", "enable softclip", OFFSET(softclip), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
+ { "mutel", "mute L", OFFSET(mute_l), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
+ { "muter", "mute R", OFFSET(mute_r), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
+ { "phasel", "phase L", OFFSET(phase_l), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
+ { "phaser", "phase R", OFFSET(phase_r), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, A },
+ { "mode", "set stereo mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 6, A, "mode" },
+ { "lr>lr", 0, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, A, "mode" },
+ { "lr>ms", 0, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, A, "mode" },
+ { "ms>lr", 0, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, A, "mode" },
+ { "lr>ll", 0, 0, AV_OPT_TYPE_CONST, {.i64=3}, 0, 0, A, "mode" },
+ { "lr>rr", 0, 0, AV_OPT_TYPE_CONST, {.i64=4}, 0, 0, A, "mode" },
+ { "lr>l+r", 0, 0, AV_OPT_TYPE_CONST, {.i64=5}, 0, 0, A, "mode" },
+ { "lr>rl", 0, 0, AV_OPT_TYPE_CONST, {.i64=6}, 0, 0, A, "mode" },
+ { "slev", "set side level", OFFSET(slev), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
+ { "sbal", "set side balance", OFFSET(sbal), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
+ { "mlev", "set middle level", OFFSET(mlev), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 0.015625, 64, A },
+ { "mpan", "set middle pan", OFFSET(mpan), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
+ { "base", "set stereo base", OFFSET(base), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -1, 1, A },
+ { "delay", "set delay", OFFSET(delay), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -20, 20, A },
+ { "sclevel", "set S/C level", OFFSET(sc_level), AV_OPT_TYPE_DOUBLE, {.dbl=1}, 1, 100, A },
+ { "phase", "set stereo phase", OFFSET(phase), AV_OPT_TYPE_DOUBLE, {.dbl=0}, 0, 360, A },
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(stereotools);
+
+static int query_formats(AVFilterContext *ctx)
+{
+ AVFilterFormats *formats = NULL;
+ AVFilterChannelLayouts *layout = NULL;
+
+ ff_add_format(&formats, AV_SAMPLE_FMT_DBL);
+ ff_set_common_formats(ctx, formats);
+ ff_add_channel_layout(&layout, AV_CH_LAYOUT_STEREO);
+ ff_set_common_channel_layouts(ctx, layout);
+
+ formats = ff_all_samplerates();
+ if (!formats)
+ return AVERROR(ENOMEM);
+
+ return ff_set_common_samplerates(ctx, formats);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+ AVFilterContext *ctx = inlink->dst;
+ StereoToolsContext *s = ctx->priv;
+
+ s->length = 2 * inlink->sample_rate * 0.05;
+ s->buffer = av_calloc(s->length, sizeof(*s->buffer));
+ if (!s->buffer)
+ return AVERROR(ENOMEM);
+
+ s->inv_atan_shape = 1.0 / atan(s->sc_level);
+ s->phase_cos_coef = cos(s->phase / 180 * M_PI);
+ s->phase_sin_coef = sin(s->phase / 180 * M_PI);
+
+ return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+ AVFilterContext *ctx = inlink->dst;
+ AVFilterLink *outlink = ctx->outputs[0];
+ StereoToolsContext *s = ctx->priv;
+ const double *src = (const double *)in->data[0];
+ AVFrame *out = NULL;
+ const double sb = s->base < 0 ? s->base * 0.5 : s->base;
+ const double sbal = 1 + s->sbal;
+ const double mpan = 1 + s->mpan;
+ const double slev = s->slev;
+ const double mlev = s->mlev;
+ const double balance_in = s->balance_in;
+ const double balance_out = s->balance_out;
+ const double sc_level = s->sc_level;
+ const double delay = s->delay;
+ const int length = s->length;
+ const int mute_l = floor(s->mute_l + 0.5);
+ const int mute_r = floor(s->mute_r + 0.5);
+ const int phase_l = floor(s->phase_l + 0.5);
+ const int phase_r = floor(s->phase_r + 0.5);
+ double *buffer = s->buffer;
+ double *dst;
+ int nbuf = inlink->sample_rate * (FFABS(delay) / 1000.);
+ int n;
+
+ nbuf -= nbuf % 2;
+ if (av_frame_is_writable(in)) {
+ out = in;
+ } else {
+ AVFrame *out = ff_get_audio_buffer(inlink, in->nb_samples);
+ if (!out) {
+ av_frame_free(&in);
+ return AVERROR(ENOMEM);
+ }
+ av_frame_copy_props(out, in);
+ }
+ dst = (double *)out->data[0];
+
+ for (n = 0; n < in->nb_samples; n++, src += 2, dst += 2) {
+ double L = src[0], R = src[1], l, r, m, S;
+
+ L *= 1. - FFMAX(0., balance_in);
+ R *= 1. + FFMIN(0., balance_in);
+
+ if (s->softclip) {
+ R = s->inv_atan_shape * atan(R * sc_level);
+ L = s->inv_atan_shape * atan(L * sc_level);
+ }
+
+ switch (s->mode) {
+ case 0:
+ m = (L + R) * 0.5;
+ S = (L - R) * 0.5;
+ l = m * mlev * FFMIN(1., 2. - mpan) + S * slev * FFMIN(1., 2. - sbal);
+ r = m * mlev * FFMIN(1., mpan) - S * slev * FFMIN(1., sbal);
+ L = l;
+ R = r;
+ break;
+ case 1:
+ l = L * FFMIN(1., 2. - sbal);
+ r = R * FFMIN(1., sbal);
+ L = 0.5 * (l + r) * mlev;
+ R = 0.5 * (l - r) * slev;
+ break;
+ case 2:
+ l = L * mlev * FFMIN(1., 2. - mpan) + R * slev * FFMIN(1., 2. - sbal);
+ r = L * mlev * FFMIN(1., mpan) - R * slev * FFMIN(1., sbal);
+ L = l;
+ R = r;
+ break;
+ case 3:
+ R = L;
+ break;
+ case 4:
+ L = R;
+ break;
+ case 5:
+ L = (L + R) / 2;
+ R = L;
+ break;
+ case 6:
+ l = L;
+ L = R;
+ R = l;
+ m = (L + R) * 0.5;
+ S = (L - R) * 0.5;
+ l = m * mlev * FFMIN(1., 2. - mpan) + S * slev * FFMIN(1., 2. - sbal);
+ r = m * mlev * FFMIN(1., mpan) - S * slev * FFMIN(1., sbal);
+ L = l;
+ R = r;
+ break;
+ }
+
+ L *= 1. - mute_l;
+ R *= 1. - mute_r;
+
+ L *= (2. * (1. - phase_l)) - 1.;
+ R *= (2. * (1. - phase_r)) - 1.;
+
+ buffer[s->pos ] = L;
+ buffer[s->pos+1] = R;
+
+ if (delay > 0.) {
+ R = buffer[(s->pos - (int)nbuf + 1 + length) % length];
+ } else if (delay < 0.) {
+ L = buffer[(s->pos - (int)nbuf + length) % length];
+ }
+
+ l = L + sb * L - sb * R;
+ r = R + sb * R - sb * L;
+
+ L = l;
+ R = r;
+
+ l = L * s->phase_cos_coef - R * s->phase_sin_coef;
+ r = L * s->phase_sin_coef + R * s->phase_cos_coef;
+
+ L = l;
+ R = r;
+
+ s->pos = (s->pos + 2) % s->length;
+
+ L *= 1. - FFMAX(0., balance_out);
+ R *= 1. + FFMIN(0., balance_out);
+
+ dst[0] = L;
+ dst[1] = R;
+ }
+
+ if (out != in)
+ av_frame_free(&in);
+ return ff_filter_frame(outlink, out);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ StereoToolsContext *s = ctx->priv;
+
+ av_freep(&s->buffer);
+}
+
+static const AVFilterPad inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .filter_frame = filter_frame,
+ .config_props = config_input,
+ },
+ { NULL }
+};
+
+static const AVFilterPad outputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO,
+ },
+ { NULL }
+};
+
+AVFilter ff_af_stereotools = {
+ .name = "stereotools",
+ .description = NULL_IF_CONFIG_SMALL("Apply various stereo tools."),
+ .query_formats = query_formats,
+ .priv_size = sizeof(StereoToolsContext),
+ .priv_class = &stereotools_class,
+ .uninit = uninit,
+ .inputs = inputs,
+ .outputs = outputs,
+};
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 89390aa..cab4564 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -102,6 +102,7 @@ void avfilter_register_all(void)
REGISTER_FILTER(SIDECHAINCOMPRESS, sidechaincompress, af);
REGISTER_FILTER(SILENCEDETECT, silencedetect, af);
REGISTER_FILTER(SILENCEREMOVE, silenceremove, af);
+ REGISTER_FILTER(STEREOTOOLS, stereotools, af);
REGISTER_FILTER(STEREOWIDEN, stereowiden, af);
REGISTER_FILTER(TREBLE, treble, af);
REGISTER_FILTER(VOLUME, volume, af);
--
1.7.11.2
More information about the ffmpeg-devel
mailing list