[FFmpeg-cvslog] avfilter: add aphasemeter filter

Paul B Mahol git at videolan.org
Wed Aug 12 22:28:21 CEST 2015


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Tue Aug 11 16:37:45 2015 +0200| [f9905e13ea1e8f3cc3971fdfc7fc8283a9779740] | committer: Paul B Mahol

avfilter: add aphasemeter filter

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=f9905e13ea1e8f3cc3971fdfc7fc8283a9779740
---

 Changelog                     |    1 +
 MAINTAINERS                   |    1 +
 doc/filters.texi              |   32 ++++++
 libavfilter/Makefile          |    1 +
 libavfilter/allfilters.c      |    1 +
 libavfilter/avf_aphasemeter.c |  241 +++++++++++++++++++++++++++++++++++++++++
 libavfilter/version.h         |    2 +-
 7 files changed, 278 insertions(+), 1 deletion(-)

diff --git a/Changelog b/Changelog
index dc37343..0e87c09 100644
--- a/Changelog
+++ b/Changelog
@@ -30,6 +30,7 @@ version <next>:
 - allyuv video source
 - atadenoise video filter
 - OS X VideoToolbox support
+- aphasemeter filter
 
 
 version 2.7:
diff --git a/MAINTAINERS b/MAINTAINERS
index b0d0dc7..e1eb556 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -354,6 +354,7 @@ Filters:
   af_pan.c                              Nicolas George
   af_sidechaincompress.c                Paul B Mahol
   af_silenceremove.c                    Paul B Mahol
+  avf_aphasemeter.c                     Paul B Mahol
   avf_avectorscope.c                    Paul B Mahol
   avf_showcqt.c                         Muhammad Faiz
   vf_blend.c                            Paul B Mahol
diff --git a/doc/filters.texi b/doc/filters.texi
index 148dce3..e99a308 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -11656,6 +11656,38 @@ tools.
 
 Below is a description of the currently available multimedia filters.
 
+ at section aphasemeter
+
+Convert input audio to a video output, displaying the audio phase.
+
+The filter accepts the following options:
+
+ at table @option
+ at item rate, r
+Set the output frame rate. Default value is @code{25}.
+
+ at item size, s
+Set the video size for the output. For the syntax of this option, check the
+ at ref{video size syntax,,"Video size" section in the ffmpeg-utils manual,ffmpeg-utils}.
+Default value is @code{800x400}.
+
+ at item rc
+ at item gc
+ at item bc
+Specify the red, green, blue contrast. Default values are @code{2},
+ at code{7} and @code{1}.
+Allowed range is @code{[0, 255]}.
+
+ at item mpc
+Set color which will be used for drawing median phase. If color is
+ at code{none} which is default, no median phase value will be drawn.
+ at end table
+
+The filter also exports the frame metadata @code{lavfi.aphasemeter.phase} which
+represents mean phase of current audio frame. Value is in range @code{[-1, 1]}.
+The @code{-1} means left and right channels are completely out of phase and
+ at code{1} means channels are in phase.
+
 @section avectorscope
 
 Convert input audio to a video output, representing the audio vector
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 1865e49..d431f99 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -253,6 +253,7 @@ OBJS-$(CONFIG_NULLSINK_FILTER)               += vsink_nullsink.o
 
 # multimedia filters
 OBJS-$(CONFIG_ADRAWGRAPH_FILTER)             += f_drawgraph.o
+OBJS-$(CONFIG_APHASEMETER_FILTER)            += avf_aphasemeter.o
 OBJS-$(CONFIG_AVECTORSCOPE_FILTER)           += avf_avectorscope.o
 OBJS-$(CONFIG_CONCAT_FILTER)                 += avf_concat.o
 OBJS-$(CONFIG_SHOWCQT_FILTER)                += avf_showcqt.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index f8e97bc..2900e88 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -268,6 +268,7 @@ void avfilter_register_all(void)
 
     /* multimedia filters */
     REGISTER_FILTER(ADRAWGRAPH,     adrawgraph,     avf);
+    REGISTER_FILTER(APHASEMETER,    aphasemeter,    avf);
     REGISTER_FILTER(AVECTORSCOPE,   avectorscope,   avf);
     REGISTER_FILTER(CONCAT,         concat,         avf);
     REGISTER_FILTER(SHOWCQT,        showcqt,        avf);
diff --git a/libavfilter/avf_aphasemeter.c b/libavfilter/avf_aphasemeter.c
new file mode 100644
index 0000000..5a470cf
--- /dev/null
+++ b/libavfilter/avf_aphasemeter.c
@@ -0,0 +1,241 @@
+/*
+ * Copyright (c) 2015 Paul B Mahol
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * audio to video multimedia aphasemeter filter
+ */
+
+#include "libavutil/avassert.h"
+#include "libavutil/channel_layout.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/opt.h"
+#include "libavutil/parseutils.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "audio.h"
+#include "video.h"
+#include "internal.h"
+
+typedef struct AudioPhaseMeterContext {
+    const AVClass *class;
+    AVFrame *out;
+    int w, h;
+    AVRational frame_rate;
+    int contrast[4];
+    uint8_t *mpc_str;
+    uint8_t mpc[4];
+    int draw_median_phase;
+} AudioPhaseMeterContext;
+
+#define OFFSET(x) offsetof(AudioPhaseMeterContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption aphasemeter_options[] = {
+    { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
+    { "r",    "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, 0, 0, FLAGS },
+    { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="800x400"}, 0, 0, FLAGS },
+    { "s",    "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="800x400"}, 0, 0, FLAGS },
+    { "rc", "set red contrast",   OFFSET(contrast[0]), AV_OPT_TYPE_INT, {.i64=2}, 0, 255, FLAGS },
+    { "gc", "set green contrast", OFFSET(contrast[1]), AV_OPT_TYPE_INT, {.i64=7}, 0, 255, FLAGS },
+    { "bc", "set blue contrast",  OFFSET(contrast[2]), AV_OPT_TYPE_INT, {.i64=1}, 0, 255, FLAGS },
+    { "mpc", "set median phase color", OFFSET(mpc_str), AV_OPT_TYPE_STRING, {.str = "none"}, 0, 0, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(aphasemeter);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    AVFilterFormats *formats = NULL;
+    AVFilterChannelLayouts *layout = NULL;
+    AVFilterLink *inlink = ctx->inputs[0];
+    AVFilterLink *outlink = ctx->outputs[0];
+    static const enum AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_NONE };
+    static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_RGBA, AV_PIX_FMT_NONE };
+
+    formats = ff_make_format_list(sample_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    ff_formats_ref(formats, &inlink->out_formats);
+
+    ff_add_channel_layout(&layout, AV_CH_LAYOUT_STEREO);
+    ff_channel_layouts_ref(layout, &inlink->out_channel_layouts);
+
+    formats = ff_all_samplerates();
+    if (!formats)
+        return AVERROR(ENOMEM);
+    ff_formats_ref(formats, &inlink->out_samplerates);
+
+    formats = ff_make_format_list(pix_fmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    ff_formats_ref(formats, &outlink->in_formats);
+
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AudioPhaseMeterContext *s = ctx->priv;
+    int nb_samples;
+
+    nb_samples = FFMAX(1024, ((double)inlink->sample_rate / av_q2d(s->frame_rate)) + 0.5);
+    inlink->partial_buf_size =
+    inlink->min_samples =
+    inlink->max_samples = nb_samples;
+
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AudioPhaseMeterContext *s = ctx->priv;
+
+    outlink->w = s->w;
+    outlink->h = s->h;
+    outlink->sample_aspect_ratio = (AVRational){1,1};
+    outlink->frame_rate = s->frame_rate;
+
+    if (!strcmp(s->mpc_str, "none"))
+        s->draw_median_phase = 0;
+    else if (av_parse_color(s->mpc, s->mpc_str, -1, ctx) >= 0)
+        s->draw_median_phase = 1;
+    else
+        return AVERROR(EINVAL);
+
+    return 0;
+}
+
+static inline int get_x(float phase, int w)
+{
+  return (phase + 1.) / 2. * (w - 1);
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    AudioPhaseMeterContext *s = ctx->priv;
+    AVDictionary **metadata;
+    const int rc = s->contrast[0];
+    const int gc = s->contrast[1];
+    const int bc = s->contrast[2];
+    float fphase = 0;
+    AVFrame *out;
+    uint8_t *dst;
+    int i;
+
+    if (!s->out || s->out->width  != outlink->w ||
+                   s->out->height != outlink->h) {
+        av_frame_free(&s->out);
+        s->out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!s->out) {
+            av_frame_free(&in);
+            return AVERROR(ENOMEM);
+        }
+
+        out = s->out;
+        for (i = 0; i < outlink->h; i++)
+            memset(out->data[0] + i * out->linesize[0], 0, outlink->w * 4);
+    } else {
+        out = s->out;
+        for (i = outlink->h - 1; i >= 10; i--)
+            memmove(out->data[0] + (i  ) * out->linesize[0],
+                    out->data[0] + (i-1) * out->linesize[0],
+                    outlink->w * 4);
+        for (i = 0; i < outlink->w; i++)
+            AV_WL32(out->data[0] + i * 4, 0);
+    }
+    s->out->pts = in->pts;
+
+    for (i = 0; i < in->nb_samples; i++) {
+        const float *src = (float *)in->data[0] + i * 2;
+        const float f = src[0] * src[1] / (src[0]*src[0] + src[1] * src[1]) * 2;
+        const float phase = isnan(f) ? 1 : f;
+        const int x = get_x(phase, s->w);
+
+        dst = out->data[0] + x * 4;
+        dst[0] = FFMIN(255, dst[0] + rc);
+        dst[1] = FFMIN(255, dst[1] + gc);
+        dst[2] = FFMIN(255, dst[2] + bc);
+        dst[3] = 255;
+        fphase += phase;
+    }
+    fphase /= in->nb_samples;
+
+    if (s->draw_median_phase) {
+        dst = out->data[0] + get_x(fphase, s->w) * 4;
+        AV_WL32(dst, AV_RL32(s->mpc));
+    }
+
+    for (i = 1; i < 10 && i < outlink->h; i++)
+        memcpy(out->data[0] + i * out->linesize[0], out->data[0], outlink->w * 4);
+
+    metadata = avpriv_frame_get_metadatap(out);
+    if (metadata) {
+        uint8_t value[128];
+
+        snprintf(value, sizeof(value), "%f", fphase);
+        av_dict_set(metadata, "lavfi.aphasemeter.phase", value, 0);
+    }
+
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, av_frame_clone(s->out));
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    AudioPhaseMeterContext *s = ctx->priv;
+
+    av_frame_free(&s->out);
+}
+
+static const AVFilterPad inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_AUDIO,
+        .config_props = config_input,
+        .filter_frame = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad outputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = config_output,
+    },
+    { NULL }
+};
+
+AVFilter ff_avf_aphasemeter = {
+    .name          = "aphasemeter",
+    .description   = NULL_IF_CONFIG_SMALL("Convert input audio to phase meter video output."),
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .priv_size     = sizeof(AudioPhaseMeterContext),
+    .inputs        = inputs,
+    .outputs       = outputs,
+    .priv_class    = &aphasemeter_class,
+};
diff --git a/libavfilter/version.h b/libavfilter/version.h
index b3b637e..993d9ee 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,7 +30,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVFILTER_VERSION_MAJOR  5
-#define LIBAVFILTER_VERSION_MINOR  32
+#define LIBAVFILTER_VERSION_MINOR  33
 #define LIBAVFILTER_VERSION_MICRO 100
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \



More information about the ffmpeg-cvslog mailing list