[FFmpeg-devel] avfilter: add mcompensate filter

Submitted by Paul B Mahol on Nov. 17, 2018, 10:54 p.m.

Details

Message ID 20181117225405.28957-1-onemda@gmail.com
State New
Headers show

Commit Message

Paul B Mahol Nov. 17, 2018, 10:54 p.m.
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
Works only after mestimate or when motion vectors are exported.
---
 libavfilter/Makefile         |   1 +
 libavfilter/allfilters.c     |   1 +
 libavfilter/vf_mcompensate.c | 173 +++++++++++++++++++++++++++++++++++
 3 files changed, 175 insertions(+)
 create mode 100644 libavfilter/vf_mcompensate.c

Patch hide | download patch | download mbox

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 6058fb97bc..8a3fc541d2 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -278,6 +278,7 @@  OBJS-$(CONFIG_LUTYUV_FILTER)                 += vf_lut.o
 OBJS-$(CONFIG_MASKEDCLAMP_FILTER)            += vf_maskedclamp.o framesync.o
 OBJS-$(CONFIG_MASKEDMERGE_FILTER)            += vf_maskedmerge.o framesync.o
 OBJS-$(CONFIG_MCDEINT_FILTER)                += vf_mcdeint.o
+OBJS-$(CONFIG_MCOMPENSATE_FILTER)            += vf_mcompensate.o
 OBJS-$(CONFIG_MERGEPLANES_FILTER)            += vf_mergeplanes.o framesync.o
 OBJS-$(CONFIG_MESTIMATE_FILTER)              += vf_mestimate.o motion_estimation.o
 OBJS-$(CONFIG_METADATA_FILTER)               += f_metadata.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 8dfa572fde..35afafb364 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -264,6 +264,7 @@  extern AVFilter ff_vf_lutyuv;
 extern AVFilter ff_vf_maskedclamp;
 extern AVFilter ff_vf_maskedmerge;
 extern AVFilter ff_vf_mcdeint;
+extern AVFilter ff_vf_mcompensate;
 extern AVFilter ff_vf_mergeplanes;
 extern AVFilter ff_vf_mestimate;
 extern AVFilter ff_vf_metadata;
diff --git a/libavfilter/vf_mcompensate.c b/libavfilter/vf_mcompensate.c
new file mode 100644
index 0000000000..3fc2ca3947
--- /dev/null
+++ b/libavfilter/vf_mcompensate.c
@@ -0,0 +1,173 @@ 
+/*
+ * 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 "motion_estimation.h"
+#include "libavcodec/mathops.h"
+#include "libavutil/avassert.h"
+#include "libavutil/common.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/motion_vector.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct MCContext {
+    const AVClass *class;
+    int direction;
+
+    int width[4], height[4];
+    int hsub, vsub;
+    int nb_planes;
+    int depth;
+} MCContext;
+
+#define OFFSET(x) offsetof(MCContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
+
+static const AVOption mcompensate_options[] = {
+    { "dir", "set compensate direction", OFFSET(direction), AV_OPT_TYPE_INT, {.i64=0}, -1, 1, FLAGS, "dir" },
+        { "forward",   0,  0, AV_OPT_TYPE_CONST, {.i64= 1}, .unit = "dir" },
+        { "backward",  0,  0, AV_OPT_TYPE_CONST, {.i64=-1}, .unit = "dir" },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(mcompensate);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
+        AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
+        AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
+        AV_PIX_FMT_YUVJ411P,
+        AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
+        AV_PIX_FMT_GRAY8,
+        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 config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    MCContext *s = ctx->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
+
+    s->hsub = desc->log2_chroma_w;
+    s->vsub = desc->log2_chroma_h;
+    s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, s->vsub);
+    s->height[0] = s->height[3] = inlink->h;
+    s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(inlink->w, s->hsub);
+    s->width[0]  = s->width[3]  = inlink->w;
+
+    s->depth = desc->comp[0].depth;
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    MCContext *s = ctx->priv;
+    AVFrameSideData *sd;
+    AVFrame *out = ff_get_video_buffer(outlink, in->width, in->height);
+
+    if (!out) {
+        av_frame_free(&in);
+        return AVERROR(ENOMEM);
+    }
+    av_frame_copy_props(out, in);
+    av_frame_copy(out, in);
+    sd = av_frame_get_side_data(in, AV_FRAME_DATA_MOTION_VECTORS);
+    if (sd) {
+        const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
+
+        for (int i = 0; i < sd->size / sizeof(*mvs); i++) {
+            const AVMotionVector *mv = &mvs[i];
+            const int direction = mv->source;
+            int w = mv->w, h = mv->h;
+            int sy, sx, dy, dx;
+
+            if (mv->src_y == mv->dst_y && mv->src_x == mv->dst_x)
+                continue;
+
+            sy = av_clip(mv->src_y, 0, inlink->h - h - 1);
+            sx = av_clip(mv->src_x, 0, inlink->w - w - 1);
+            dy = av_clip(mv->dst_y, 0, inlink->h - h - 1);
+            dx = av_clip(mv->dst_x, 0, inlink->w - w - 1);
+
+            if (direction == s->direction) {
+                for (int p = 0; p < s->nb_planes; p++) {
+                    const int vsub = p ? s->vsub : 0;
+                    const int hsub = p ? s->hsub : 0;
+                    const uint8_t *src = in->data[p];
+                    uint8_t *dst = out->data[p];
+
+                    av_image_copy_plane(dst + (dy >> vsub) * out->linesize[p] + (dx >> hsub),
+                                        out->linesize[p],
+                                        src + (sy >> vsub) * in->linesize[p] + (sx >> hsub),
+                                        in->linesize[p],
+                                        w >> hsub, h >> vsub);
+                }
+            }
+        }
+    }
+    av_frame_free(&in);
+    return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad inputs[] = {
+    {
+        .name         = "default",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .config_props = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_mcompensate = {
+    .name          = "mcompensate",
+    .description   = NULL_IF_CONFIG_SMALL("Motion compensate frames."),
+    .priv_size     = sizeof(MCContext),
+    .priv_class    = &mcompensate_class,
+    .query_formats = query_formats,
+    .inputs        = inputs,
+    .outputs       = outputs,
+};