[FFmpeg-devel] [PATCH] avfilter: add mkmap source filter

Paul B Mahol onemda at gmail.com
Sun Jan 29 01:37:33 EET 2017


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 doc/filters.texi         |  39 ++++++++++++
 libavfilter/Makefile     |   1 +
 libavfilter/allfilters.c |   1 +
 libavfilter/vsrc_mkmap.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 204 insertions(+)
 create mode 100644 libavfilter/vsrc_mkmap.c

diff --git a/doc/filters.texi b/doc/filters.texi
index cd1aaab..d1790ac 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -15248,6 +15248,45 @@ Set the initial y position. Must be a floating point value between
 -100 and 100. Default value is -0.131825904205311970493132056385139.
 @end table
 
+ at section mkmap
+
+Generate map of pixels for one of axis, either X or Y. Mainly useful
+as providing input frames for remap filter.
+
+This source accepts the following options:
+
+ at table @option
+
+ at item size, s
+Set the size of the output video. For the syntax of this option, check the
+ at ref{video size syntax,,"Video size" section in the ffmpeg-utils manual,ffmpeg-utils}.
+
+ at item rate, r
+Set the video rate, that is the number of frames generated per second.
+Default is 25.
+
+ at item expr, e
+Set map expression.
+
+The expressions can contain the following constants and functions:
+
+ at table @option
+ at item W
+ at item H
+The output width and height.
+
+ at item X
+ at item Y
+The current column and row.
+
+ at item N
+The current frame number, starting from 0.
+
+ at item T
+Time of the current frame, expressed in seconds.
+ at end table
+ at end table
+
 @section mptestsrc
 
 Generate various test patterns, as generated by the MPlayer test filter.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 91621d9..68e1923 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -321,6 +321,7 @@ OBJS-$(CONFIG_FREI0R_SRC_FILTER)             += vf_frei0r.o
 OBJS-$(CONFIG_HALDCLUTSRC_FILTER)            += vsrc_testsrc.o
 OBJS-$(CONFIG_LIFE_FILTER)                   += vsrc_life.o
 OBJS-$(CONFIG_MANDELBROT_FILTER)             += vsrc_mandelbrot.o
+OBJS-$(CONFIG_MKMAP_FILTER)                  += vsrc_mkmap.o
 OBJS-$(CONFIG_MPTESTSRC_FILTER)              += vsrc_mptestsrc.o
 OBJS-$(CONFIG_NULLSRC_FILTER)                += vsrc_testsrc.o
 OBJS-$(CONFIG_RGBTESTSRC_FILTER)             += vsrc_testsrc.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index d71d14a..1d8ca94 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -336,6 +336,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(HALDCLUTSRC,    haldclutsrc,    vsrc);
     REGISTER_FILTER(LIFE,           life,           vsrc);
     REGISTER_FILTER(MANDELBROT,     mandelbrot,     vsrc);
+    REGISTER_FILTER(MKMAP,          mkmap,          vsrc);
     REGISTER_FILTER(MPTESTSRC,      mptestsrc,      vsrc);
     REGISTER_FILTER(NULLSRC,        nullsrc,        vsrc);
     REGISTER_FILTER(RGBTESTSRC,     rgbtestsrc,     vsrc);
diff --git a/libavfilter/vsrc_mkmap.c b/libavfilter/vsrc_mkmap.c
new file mode 100644
index 0000000..e699c5f
--- /dev/null
+++ b/libavfilter/vsrc_mkmap.c
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2017 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 "libavutil/eval.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct MkMapContext {
+    const AVClass *class;
+
+    int w, h;
+    AVRational frame_rate;
+    char *expr_str;
+
+    AVExpr *expr;
+    int64_t pts;
+} MkMapContext;
+
+static const char *const var_names[] = {
+    "W", "H", "X", "Y", "N", "T",
+    NULL
+};
+
+enum var_name {
+    VAR_W, VAR_H, VAR_X, VAR_Y, VAR_N, VAR_T,
+    VAR_VARS_NB
+};
+
+#define OFFSET(x) offsetof(MkMapContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption mkmap_options[] = {
+    { "size",     "set video size",     OFFSET(w),          AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"},  0, 0,       FLAGS },
+    { "s",        "set video size",     OFFSET(w),          AV_OPT_TYPE_IMAGE_SIZE, {.str = "hd720"},  0, 0,       FLAGS },
+    { "rate",     "set video rate",     OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"},     0, INT_MAX, FLAGS },
+    { "r",        "set video rate",     OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"},     0, INT_MAX, FLAGS },
+    { "expr",     "set map expression", OFFSET(expr_str),   AV_OPT_TYPE_STRING,     {.str = "Y*W+X" }, 0, 0,       FLAGS },
+    { "e",        "set map expression", OFFSET(expr_str),   AV_OPT_TYPE_STRING,     {.str = "Y*W+X" }, 0, 0,       FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(mkmap);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_GRAY16,
+        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 mkmap(MkMapContext *s, AVFrame *out, AVFilterLink *outlink)
+{
+    double var_values[VAR_VARS_NB];
+    uint16_t *dst;
+    int ret, x, y;
+
+    av_expr_free(s->expr);
+    s->expr = NULL;
+    ret = av_expr_parse(&s->expr, s->expr_str,
+                        var_names, NULL, NULL, NULL, NULL, 0, s);
+    if (ret < 0) {
+        av_log(s, AV_LOG_ERROR,
+               "Error when parsing the expression '%s'.\n",
+               s->expr_str);
+        return AVERROR(EINVAL);
+    }
+
+    var_values[VAR_N] = outlink->frame_count_out,
+    var_values[VAR_T] = out->pts * av_q2d(outlink->time_base),
+    var_values[VAR_W] = out->width;
+    var_values[VAR_H] = out->height;
+
+    dst = (uint16_t *)out->data[0];
+    for (y = 0; y < out->height; y++) {
+        var_values[VAR_Y] = y;
+        for (x = 0; x < out->width; x++) {
+            var_values[VAR_X] = x;
+            dst[x] = av_expr_eval(s->expr, var_values, s);
+        }
+        dst += out->linesize[0] / 2;
+    }
+    out->pts++;
+
+    return ret;
+}
+
+static int config_props(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    MkMapContext *s = ctx->priv;
+
+    outlink->w = s->w;
+    outlink->h = s->h;
+    outlink->time_base = av_inv_q(s->frame_rate);
+
+    return 0;
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    MkMapContext *s = outlink->src->priv;
+    AVFrame *out = ff_get_video_buffer(outlink, s->w, s->h);
+    if (!out)
+        return AVERROR(ENOMEM);
+    out->sample_aspect_ratio = (AVRational) {1, 1};
+    mkmap(s, out, outlink);
+    return ff_filter_frame(outlink, out);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    MkMapContext *s = ctx->priv;
+
+    av_expr_free(s->expr);
+    s->expr = NULL;
+}
+
+static const AVFilterPad mkmap_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .request_frame = request_frame,
+        .config_props  = config_props,
+    },
+    { NULL }
+};
+
+AVFilter ff_vsrc_mkmap = {
+    .name          = "mkmap",
+    .description   = NULL_IF_CONFIG_SMALL("Make mapping for pixels."),
+    .priv_size     = sizeof(MkMapContext),
+    .priv_class    = &mkmap_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = NULL,
+    .outputs       = mkmap_outputs,
+};
-- 
2.9.3



More information about the ffmpeg-devel mailing list