[FFmpeg-devel] [PATCH]{WIP} avfilter: add moddif video filter

Paul B Mahol onemda at gmail.com
Sun Nov 7 16:52:10 EET 2021


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

Allows combination of output of spatial only deinterlacers and spatio-temporal deinterlacers.
Gives overall higher PSNR result.

Spatial only deinterlacer output is used in case spatio-temporal one would use simple spatial interpolations.

---
 libavfilter/Makefile     |   1 +
 libavfilter/allfilters.c |   1 +
 libavfilter/vf_moddif.c  | 335 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 337 insertions(+)
 create mode 100644 libavfilter/vf_moddif.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index aa499696d7..6fa344489f 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -349,6 +349,7 @@ OBJS-$(CONFIG_METADATA_FILTER)               += f_metadata.o
 OBJS-$(CONFIG_MIDEQUALIZER_FILTER)           += vf_midequalizer.o framesync.o
 OBJS-$(CONFIG_MINTERPOLATE_FILTER)           += vf_minterpolate.o motion_estimation.o
 OBJS-$(CONFIG_MIX_FILTER)                    += vf_mix.o framesync.o
+OBJS-$(CONFIG_MODDIF_FILTER)                 += vf_moddif.o
 OBJS-$(CONFIG_MONOCHROME_FILTER)             += vf_monochrome.o
 OBJS-$(CONFIG_MORPHO_FILTER)                 += vf_morpho.o
 OBJS-$(CONFIG_MPDECIMATE_FILTER)             += vf_mpdecimate.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 71087fbf60..88d91db3f1 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -334,6 +334,7 @@ extern const AVFilter ff_vf_metadata;
 extern const AVFilter ff_vf_midequalizer;
 extern const AVFilter ff_vf_minterpolate;
 extern const AVFilter ff_vf_mix;
+extern const AVFilter ff_vf_moddif;
 extern const AVFilter ff_vf_monochrome;
 extern const AVFilter ff_vf_morpho;
 extern const AVFilter ff_vf_mpdecimate;
diff --git a/libavfilter/vf_moddif.c b/libavfilter/vf_moddif.c
new file mode 100644
index 0000000000..90aaa5328d
--- /dev/null
+++ b/libavfilter/vf_moddif.c
@@ -0,0 +1,335 @@
+/*
+ * Copyright (c) 2016 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/imgutils.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/opt.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+#include "framesync.h"
+
+#define OFFSET(x) offsetof(ModDifContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
+
+typedef struct ThreadData {
+    AVFrame *input, *spatial, *temporal, *output;
+    int parity;
+} ThreadData;
+
+typedef struct ModDifContext {
+    const AVClass *class;
+
+    int linesize[4];
+    int planewidth[4], planeheight[4];
+    int nb_planes;
+    int depth;
+    int parity;
+    int is_second;
+    int64_t pts;
+    AVFrame *prev, *cur, *next;
+    FFFrameSync fs;
+} ModDifContext;
+
+#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit }
+
+static const AVOption moddif_options[] = {
+    { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, FLAGS, "parity" },
+    CONST("tff",  "assume top field first",     0, "parity"),
+    CONST("bff",  "assume bottom field first",  1, "parity"),
+    CONST("auto", "auto detect parity",        -1, "parity"),
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(moddif);
+
+static const enum AVPixelFormat pix_fmts[] = {
+    AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
+    AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
+    AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
+    AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
+    AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
+    AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
+    AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
+    AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
+    AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
+    AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
+    AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
+    AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
+    AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
+    AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
+    AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
+    AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
+    AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
+    AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
+    AV_PIX_FMT_NONE
+};
+
+static int moddif_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    ModDifContext *s = ctx->priv;
+    ThreadData *td = arg;
+    const int parity = td->parity;
+
+    for (int p = 0; p < s->nb_planes; p++) {
+        const ptrdiff_t in_linesize = td->input->linesize[p];
+        const ptrdiff_t tlinesize = td->temporal->linesize[p];
+        const ptrdiff_t slinesize = td->spatial->linesize[p];
+        const ptrdiff_t out_linesize = td->output->linesize[p];
+        const int w = s->planewidth[p];
+        const int h = s->planeheight[p];
+        const int slice_start = (h * jobnr) / nb_jobs;
+        const int slice_end = (h * (jobnr+1)) / nb_jobs;
+        const uint8_t *in_src = td->input->data[p] + slice_start * in_linesize;
+        const uint8_t *spatial_src = td->spatial->data[p] + slice_start * slinesize;
+        const uint8_t *temporal_src = td->temporal->data[p] + slice_start * tlinesize;
+        uint8_t *dst = td->output->data[p] + slice_start * out_linesize;
+
+        for (int y = slice_start; y < slice_end; y++) {
+            if ((y ^ parity) & 1) {
+                const ptrdiff_t cur_linesize = y > 0 && y < h - 1 ? s->cur->linesize[p] : 0;
+                const uint8_t *cur = s->cur->data[p] + y * cur_linesize;
+                const uint8_t *prev = s->prev->data[p] + y * s->prev->linesize[p];
+                const uint8_t *next = s->next->data[p] + y * s->next->linesize[p];
+                const uint8_t *prev2 = parity ? prev : cur;
+                const uint8_t *next2 = parity ? cur  : next;
+
+                for (int x = 0; x < w; x++) {
+                    int c = cur[x + cur_linesize];
+                    int e = cur[x - cur_linesize];
+                    if (FFABS(c - e) > FFABS(prev2[x] - next2[x])) {
+                        dst[x] = temporal_src[x];
+                    } else {
+                        dst[x] = spatial_src[x];
+                    }
+                }
+            } else {
+                memcpy(dst, s->next->data[p] + y * s->next->linesize[p], w);
+            }
+
+            dst += out_linesize;
+            in_src += in_linesize;
+            spatial_src += slinesize;
+            temporal_src += tlinesize;
+        }
+    }
+
+    return 0;
+}
+
+static int process_frame(FFFrameSync *fs)
+{
+    AVFilterContext *ctx = fs->parent;
+    ModDifContext *s = fs->opaque;
+    AVFilterLink *outlink = ctx->outputs[0];
+    AVFrame *out, *input, *spatial, *temporal;
+    int ret;
+
+    if ((ret = ff_framesync_get_frame(&s->fs, 0, &input,    0)) < 0 ||
+        (ret = ff_framesync_get_frame(&s->fs, 1, &spatial,  0)) < 0 ||
+        (ret = ff_framesync_get_frame(&s->fs, 2, &temporal, 0)) < 0)
+        return ret;
+
+    if (s->pts != input->pts) {
+        av_frame_free(&s->prev);
+        s->prev = s->cur;
+        s->cur  = s->next;
+        s->next = av_frame_clone(input);
+        s->pts  = input->pts;
+        s->is_second = 0;
+        if (!s->cur) {
+            s->cur = av_frame_clone(s->next);
+            if (!s->cur)
+                return AVERROR(ENOMEM);
+        }
+    } else {
+        s->is_second = 1;
+    }
+
+    if (ctx->is_disabled) {
+        out = av_frame_clone(input);
+        if (!out)
+            return AVERROR(ENOMEM);
+    } else if (!s->prev) {
+        out = av_frame_clone(spatial);
+        if (!out)
+            return AVERROR(ENOMEM);
+    } else {
+        ThreadData td;
+        int tff;
+
+        if (s->parity == -1) {
+            tff = s->cur->interlaced_frame ?
+                  s->cur->top_field_first : 1;
+        } else {
+            tff = s->parity ^ 1;
+        }
+
+        out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out)
+            return AVERROR(ENOMEM);
+        av_frame_copy_props(out, spatial);
+
+        td.input = input;
+        td.spatial = spatial;
+        td.temporal = temporal;
+        td.output = out;
+        td.parity = tff ^ !s->is_second;
+
+        ff_filter_execute(ctx, moddif_slice, &td, NULL,
+                          FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
+    }
+    out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
+
+    return ff_filter_frame(outlink, out);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    ModDifContext *s = ctx->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    int vsub, hsub, ret;
+
+    s->pts = AV_NOPTS_VALUE;
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
+
+    if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
+        return ret;
+
+    hsub = desc->log2_chroma_w;
+    vsub = desc->log2_chroma_h;
+    s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
+    s->planewidth[1]  = s->planewidth[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
+    s->planewidth[0]  = s->planewidth[3]  = inlink->w;
+
+    s->depth = desc->comp[0].depth;
+
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    ModDifContext *s = ctx->priv;
+    AVFilterLink *input = ctx->inputs[0];
+    AVFilterLink *spatial = ctx->inputs[1];
+    AVFilterLink *temporal = ctx->inputs[2];
+    FFFrameSyncIn *in;
+    int ret;
+
+    if (input->w != spatial->w  || input->h != spatial->h ||
+        input->w != temporal->w || input->h != temporal->h) {
+        av_log(ctx, AV_LOG_ERROR, "First input link %s parameters "
+               "(size %dx%d) do not match the corresponding "
+               "second input link %s parameters (%dx%d) "
+               "and/or third input link %s parameters (size %dx%d)\n",
+               ctx->input_pads[0].name, input->w, input->h,
+               ctx->input_pads[1].name, spatial->w, spatial->h,
+               ctx->input_pads[2].name, temporal->w, temporal->h);
+        return AVERROR(EINVAL);
+    }
+
+    outlink->w = input->w;
+    outlink->h = input->h;
+    outlink->sample_aspect_ratio = input->sample_aspect_ratio;
+    outlink->frame_rate = spatial->frame_rate;
+
+    if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0)
+        return ret;
+
+    in = s->fs.in;
+    in[0].time_base = input->time_base;
+    in[1].time_base = spatial->time_base;
+    in[2].time_base = temporal->time_base;
+    in[0].sync   = 1;
+    in[0].before = EXT_STOP;
+    in[0].after  = EXT_INFINITY;
+    in[1].sync   = 1;
+    in[1].before = EXT_STOP;
+    in[1].after  = EXT_INFINITY;
+    in[2].sync   = 1;
+    in[2].before = EXT_STOP;
+    in[2].after  = EXT_INFINITY;
+    s->fs.opaque   = s;
+    s->fs.on_event = process_frame;
+
+    ret = ff_framesync_configure(&s->fs);
+    outlink->time_base = s->fs.time_base;
+
+    return ret;
+}
+
+static int activate(AVFilterContext *ctx)
+{
+    ModDifContext *s = ctx->priv;
+    return ff_framesync_activate(&s->fs);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    ModDifContext *s = ctx->priv;
+
+    ff_framesync_uninit(&s->fs);
+
+    av_frame_free(&s->prev);
+    av_frame_free(&s->cur );
+    av_frame_free(&s->next);
+}
+
+static const AVFilterPad moddif_inputs[] = {
+    {
+        .name         = "input",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = config_input,
+    },
+    {
+        .name         = "spatial",
+        .type         = AVMEDIA_TYPE_VIDEO,
+    },
+    {
+        .name         = "temporal",
+        .type         = AVMEDIA_TYPE_VIDEO,
+    },
+};
+
+static const AVFilterPad moddif_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_output,
+    },
+};
+
+const AVFilter ff_vf_moddif = {
+    .name          = "moddif",
+    .description   = NULL_IF_CONFIG_SMALL("Apply modular deinterlacing."),
+    .priv_size     = sizeof(ModDifContext),
+    .uninit        = uninit,
+    .activate      = activate,
+    FILTER_INPUTS(moddif_inputs),
+    FILTER_OUTPUTS(moddif_outputs),
+    FILTER_PIXFMTS_ARRAY(pix_fmts),
+    .priv_class    = &moddif_class,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
+    .process_command = ff_filter_process_command,
+};
-- 
2.33.0



More information about the ffmpeg-devel mailing list