[FFmpeg-devel] [RFC] [WIP] [PATCH] lavfi: add drawgraph filter

Paul B Mahol onemda at gmail.com
Wed Jun 24 19:49:35 CEST 2015


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
Example:

ffplay movie.webm -vf signalstats,drawgraph=lavfi.signalstats.YAVG:min=0:max=255:mode=0

---
 libavfilter/Makefile       |   1 +
 libavfilter/allfilters.c   |   1 +
 libavfilter/vf_drawgraph.c | 186 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 188 insertions(+)
 create mode 100644 libavfilter/vf_drawgraph.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 55cd055..54a8bbb 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -117,6 +117,7 @@ OBJS-$(CONFIG_DELOGO_FILTER)                 += vf_delogo.o
 OBJS-$(CONFIG_DESHAKE_FILTER)                += vf_deshake.o
 OBJS-$(CONFIG_DETELECINE_FILTER)             += vf_detelecine.o
 OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
+OBJS-$(CONFIG_DRAWGRAPH_FILTER)              += vf_drawgraph.o
 OBJS-$(CONFIG_DRAWGRID_FILTER)               += vf_drawbox.o
 OBJS-$(CONFIG_DRAWTEXT_FILTER)               += vf_drawtext.o
 OBJS-$(CONFIG_ELBG_FILTER)                   += vf_elbg.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 3898498..b9508f5 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -133,6 +133,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(DESHAKE,        deshake,        vf);
     REGISTER_FILTER(DETELECINE,     detelecine,     vf);
     REGISTER_FILTER(DRAWBOX,        drawbox,        vf);
+    REGISTER_FILTER(DRAWGRAPH,      drawgraph,      vf);
     REGISTER_FILTER(DRAWGRID,       drawgrid,       vf);
     REGISTER_FILTER(DRAWTEXT,       drawtext,       vf);
     REGISTER_FILTER(EDGEDETECT,     edgedetect,     vf);
diff --git a/libavfilter/vf_drawgraph.c b/libavfilter/vf_drawgraph.c
new file mode 100644
index 0000000..3c9eed8
--- /dev/null
+++ b/libavfilter/vf_drawgraph.c
@@ -0,0 +1,186 @@
+/*
+ * 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
+ */
+
+#include "float.h"
+
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct DrawGraphContext {
+    const AVClass *class;
+
+    char          *key;
+    float         min;
+    float         max;
+    uint8_t       bg[4];
+    uint8_t       fg[4];
+    int           mode;
+    int           slide;
+
+    AVFrame       *out;
+    int           x;
+} DrawGraphContext;
+
+#define OFFSET(x) offsetof(DrawGraphContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption drawgraph_options[] = {
+    { "metadata", "set metadata key", OFFSET(key), AV_OPT_TYPE_STRING, {.str=""}, CHAR_MIN, CHAR_MAX, FLAGS },
+    { "min", "set minimal value", OFFSET(min), AV_OPT_TYPE_FLOAT, {.dbl=-1.}, INT_MIN, INT_MAX, FLAGS },
+    { "max", "set maximal value", OFFSET(max), AV_OPT_TYPE_FLOAT, {.dbl=1.},  INT_MIN, INT_MAX, FLAGS },
+    { "background", "set background color", OFFSET(bg), AV_OPT_TYPE_COLOR, {.str="white"},  CHAR_MIN, CHAR_MAX, FLAGS },
+    { "foreground", "set foreground color", OFFSET(fg), AV_OPT_TYPE_COLOR, {.str="blue"},   CHAR_MIN, CHAR_MAX, FLAGS },
+    { "mode", "set graph mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    { "slide", "set slide mode", OFFSET(slide), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(drawgraph);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_GBRP,
+        AV_PIX_FMT_NONE
+    };
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+    if (!fmts_list)
+        return AVERROR(ENOMEM);
+    return ff_set_common_formats(ctx, fmts_list);
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    DrawGraphContext *s = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    AVDictionary *metadata;
+    AVDictionaryEntry *e;
+    AVFrame *out = s->out;
+    int i, j;
+    float vf;
+    int vd;
+
+    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);
+        out = s->out;
+        if (!s->out) {
+            av_frame_free(&in);
+            return AVERROR(ENOMEM);
+        }
+
+        for (i = 0; i < out->height; i++) {
+            memset(out->data[0] + i * out->linesize[0], s->bg[1], outlink->w);
+            memset(out->data[1] + i * out->linesize[1], s->bg[2], outlink->w);
+            memset(out->data[2] + i * out->linesize[2], s->bg[0], outlink->w);
+        }
+    }
+    s->out->pts = in->pts;
+
+    metadata = av_frame_get_metadata(in);
+    e = av_dict_get(metadata, s->key, NULL, 0);
+
+    if (e && e->value) {
+        if (sscanf(e->value, "%d", &vd) != 1)
+            goto out;
+        vf = vd;
+
+        if (vf >= s->min && vf <= s->max) {
+            int y, x;
+
+            if (s->x >= outlink->w) {
+                if (s->slide == 0 || s->slide == 1)
+                    s->x = 0;
+
+                if (s->slide == 0) {
+                    for (i = 0; i < out->height; i++) {
+                        memset(out->data[0] + i * out->linesize[0], s->bg[1], outlink->w);
+                        memset(out->data[1] + i * out->linesize[1], s->bg[2], outlink->w);
+                        memset(out->data[2] + i * out->linesize[2], s->bg[0], outlink->w);
+                    }
+                }
+            }
+
+            x = s->x;
+            y = outlink->h * (1 - (vf / (s->max - s->min)));
+
+            switch (s->mode) {
+            case 0:
+                for (j = y; j < outlink->h; j++) {
+                    out->data[0][j * out->linesize[0] + x] = s->fg[1];
+                    out->data[1][j * out->linesize[1] + x] = s->fg[2];
+                    out->data[2][j * out->linesize[2] + x] = s->fg[0];
+                }
+                break;
+            case 1:
+                out->data[0][y * out->linesize[0] + x] = s->fg[1];
+                out->data[1][y * out->linesize[1] + x] = s->fg[2];
+                out->data[2][y * out->linesize[2] + x] = s->fg[0];
+                break;
+            }
+            s->x++;
+        }
+    }
+
+out:
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, av_frame_clone(s->out));
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    DrawGraphContext *s = ctx->priv;
+
+    av_frame_free(&s->out);
+}
+
+static const AVFilterPad drawgraph_inputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_VIDEO,
+        .filter_frame   = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad drawgraph_outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_drawgraph = {
+    .name          = "drawgraph",
+    .description   = NULL_IF_CONFIG_SMALL("Draw a graph using input video metadata."),
+    .priv_size     = sizeof(DrawGraphContext),
+    .priv_class    = &drawgraph_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = drawgraph_inputs,
+    .outputs       = drawgraph_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};
-- 
1.7.11.2



More information about the ffmpeg-devel mailing list