diff --git a/doc/filters.texi b/doc/filters.texi
index 01ae540c5c..92f9c53066 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -21152,6 +21152,60 @@ Set the scaling dimension: @code{2} for @code{2xBR}, @code{3} for
 Default is @code{3}.
 @end table
 
+@section xdif
+
+Deinterlace the input video using X Deinterlacing Filter.
+
+Spatial only filter that operates in 8x8 blocks with edge-oriented interpolation.
+It accepts the following options:
+
+@table @option
+@item mode
+The interlacing mode to adopt. It accepts one of the following values:
+
+@table @option
+@item frame
+Output one frame for each frame.
+@item field
+Output one frame for each field.
+@end table
+
+The default value is @code{field}.
+
+@item parity
+The picture field parity assumed for the input interlaced video. It accepts one
+of the following values:
+
+@table @option
+@item ff
+Assume the top field is first.
+@item bff
+Assume the bottom field is first.
+@item auto
+Enable automatic detection of field parity.
+@end table
+
+The default value is @code{auto}.
+If the interlacing is unknown or the decoder does not export this information,
+top field first will be assumed.
+
+@item deint
+Specify which frames to deinterlace. Accepts one of the following
+values:
+
+@table @option
+@item all
+Deinterlace all frames.
+@item interlaced
+Only deinterlace frames marked as interlaced.
+@end table
+
+The default value is @code{all}.
+@end table
+
+@subsection Commands
+This filter supports same @ref{commands} as options.
+
 @section xfade
 
 Apply cross fade from one input video stream to another input video stream.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index ad1046d526..d14023c13d 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -467,6 +467,7 @@ OBJS-$(CONFIG_W3FDIF_FILTER)                 += vf_w3fdif.o
 OBJS-$(CONFIG_WAVEFORM_FILTER)               += vf_waveform.o
 OBJS-$(CONFIG_WEAVE_FILTER)                  += vf_weave.o
 OBJS-$(CONFIG_XBR_FILTER)                    += vf_xbr.o
+OBJS-$(CONFIG_XDIF_FILTER)                   += vf_xdif.o
 OBJS-$(CONFIG_XFADE_FILTER)                  += vf_xfade.o
 OBJS-$(CONFIG_XFADE_OPENCL_FILTER)           += vf_xfade_opencl.o opencl.o opencl/xfade.o
 OBJS-$(CONFIG_XMEDIAN_FILTER)                += vf_xmedian.o framesync.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index ce317dfa1c..215dfef5c5 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -445,6 +445,7 @@ extern AVFilter ff_vf_w3fdif;
 extern AVFilter ff_vf_waveform;
 extern AVFilter ff_vf_weave;
 extern AVFilter ff_vf_xbr;
+extern AVFilter ff_vf_xdif;
 extern AVFilter ff_vf_xfade;
 extern AVFilter ff_vf_xfade_opencl;
 extern AVFilter ff_vf_xmedian;
diff --git a/libavfilter/vf_xdif.c b/libavfilter/vf_xdif.c
new file mode 100644
index 0000000000..5476527f54
--- /dev/null
+++ b/libavfilter/vf_xdif.c
@@ -0,0 +1,261 @@
+/*
+ * Copyright (c) 2000-2011 VLC authors and VideoLAN
+ * Copyright (c) Laurent Aimar <fenrir@videolan.org>
+ * Copyright (c) 2021 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/common.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+#include "xdif.h"
+
+#define OFFSET(x) offsetof(XDIFContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
+#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, 0, 0, FLAGS, unit }
+
+static const AVOption xdif_options[] = {
+    { "mode", "specify the mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, "mode" },
+    CONST("frame", NULL, 0, "mode"),
+    CONST("field", NULL, 1, "mode"),
+    { "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"),
+    { "deint",  "specify which frames to deinterlace", OFFSET(deint), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "deint" },
+    CONST("all",        "deinterlace all frames",                       0, "deint"),
+    CONST("interlaced", "only deinterlace frames marked as interlaced", 1, "deint"),
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(xdif);
+
+typedef struct ThreadData {
+    AVFrame *out, *in;
+} ThreadData;
+
+static inline int64_t ssd(int x)
+{
+    return x * x;
+}
+
+#define DEPTH 8
+#include "xdif_template.c"
+
+#undef DEPTH
+#define DEPTH 16
+#include "xdif_template.c"
+
+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_GBRP, AV_PIX_FMT_GBRAP,
+        AV_PIX_FMT_GRAY8,
+        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_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
+        AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
+        AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16,
+        AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA422P16,
+        AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA420P16,
+        AV_PIX_FMT_GBRAP10,   AV_PIX_FMT_GBRAP12,    AV_PIX_FMT_GBRAP16,
+        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;
+    XDIFContext *s = ctx->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    int depth;
+
+    s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
+    s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
+    s->planewidth[0] = s->planewidth[3] = inlink->w;
+
+    if (inlink->h < 3) {
+        av_log(ctx, AV_LOG_ERROR, "Video of less than 3 lines is not supported\n");
+        return AVERROR(EINVAL);
+    }
+
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
+    depth = desc->comp[0].depth;
+    s->deinterlace_slice = depth <= 8 ? deinterlace_slice_8 : deinterlace_slice_16;
+    s->threshold = (1 << depth) / 4;
+
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AVFilterLink *inlink = ctx->inputs[0];
+
+    outlink->time_base.num = inlink->time_base.num;
+    outlink->time_base.den = inlink->time_base.den * 2;
+    outlink->frame_rate.num = inlink->frame_rate.num * 2;
+    outlink->frame_rate.den = inlink->frame_rate.den;
+
+    return 0;
+}
+
+static int filter(AVFilterContext *ctx, int is_second, AVFrame *in)
+{
+    XDIFContext *s = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    AVFrame *out;
+    ThreadData td;
+
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    if (!out)
+        return AVERROR(ENOMEM);
+    av_frame_copy_props(out, in);
+    out->interlaced_frame = 0;
+    out->pts = s->pts;
+
+    td.out = out; td.in = in;
+    ctx->internal->execute(ctx, s->deinterlace_slice, &td, NULL,
+                           FFMIN(s->planeheight[1] / 8,
+                                 ff_filter_get_nb_threads(ctx)));
+
+    if (s->mode)
+        s->field = !s->field;
+
+    return ff_filter_frame(outlink, out);
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    XDIFContext *s = ctx->priv;
+    int ret;
+
+    if (!s->prev) {
+        s->prev = in;
+        return 0;
+    }
+
+    if ((s->deint && !in->interlaced_frame) || ctx->is_disabled) {
+        s->prev->pts *= 2;
+        ret = ff_filter_frame(ctx->outputs[0], s->prev);
+        s->prev = in;
+        return ret;
+    }
+
+    s->pts = s->prev->pts * 2;
+    ret = filter(ctx, 0, s->prev);
+    if (ret < 0 || s->mode == 0) {
+        av_frame_free(&s->prev);
+        s->prev = in;
+        return ret;
+    }
+
+    s->pts = s->prev->pts + in->pts;
+    ret = filter(ctx, 1, s->prev);
+    av_frame_free(&s->prev);
+    s->prev = in;
+    return ret;
+}
+
+static int request_frame(AVFilterLink *link)
+{
+    AVFilterContext *ctx = link->src;
+    XDIFContext *s = ctx->priv;
+    int ret;
+
+    if (s->eof)
+        return AVERROR_EOF;
+
+    ret = ff_request_frame(ctx->inputs[0]);
+
+    if (ret == AVERROR_EOF) {
+        AVFrame *next = av_frame_clone(s->prev);
+
+        if (!next)
+            return AVERROR(ENOMEM);
+
+        next->pts = s->prev->pts + av_rescale_q(1, av_inv_q(ctx->outputs[0]->frame_rate),
+                                                ctx->outputs[0]->time_base);
+        s->eof = 1;
+        ret = filter_frame(ctx->inputs[0], next);
+    } else if (ret < 0) {
+        return ret;
+    }
+
+    return ret;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    XDIFContext *s = ctx->priv;
+
+    av_frame_free(&s->prev);
+}
+
+static const AVFilterPad xdif_inputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .filter_frame  = filter_frame,
+        .config_props  = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad xdif_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_output,
+        .request_frame = request_frame,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_xdif = {
+    .name          = "xdif",
+    .description   = NULL_IF_CONFIG_SMALL("Apply X deinterlace."),
+    .priv_size     = sizeof(XDIFContext),
+    .priv_class    = &xdif_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = xdif_inputs,
+    .outputs       = xdif_outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS,
+    .process_command = ff_filter_process_command,
+};
diff --git a/libavfilter/xdif.h b/libavfilter/xdif.h
new file mode 100644
index 0000000000..e66d966ef4
--- /dev/null
+++ b/libavfilter/xdif.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2000-2011 VLC authors and VideoLAN
+ * Copyright (c) Laurent Aimar <fenrir@videolan.org>
+ * Copyright (c) 2021 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
+ */
+
+#ifndef AVFILTER_XDIF_H
+#define AVFILTER_XDIF_H
+
+#include "avfilter.h"
+
+typedef struct XDIFContext {
+    const AVClass *class;
+
+    int mode;             ///< 0 is frame, 1 is field
+    int parity;           ///< frame field parity
+    int deint;            ///< which frames to deinterlace
+    int planewidth[4];    ///< width of each plane
+    int planeheight[4];   ///< height of each plane
+    int field;            ///< which field are we on, 0 or 1
+    int eof;
+    int nb_planes;
+    int threshold;
+    int64_t pts;
+
+    AVFrame *prev;
+
+    int (*deinterlace_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
+} XDIFContext;
+
+#endif
diff --git a/libavfilter/xdif_template.c b/libavfilter/xdif_template.c
new file mode 100644
index 0000000000..298a6b08f2
--- /dev/null
+++ b/libavfilter/xdif_template.c
@@ -0,0 +1,289 @@
+/*
+ * Copyright (c) 2000-2011 VLC authors and VideoLAN
+ * Copyright (c) Laurent Aimar <fenrir@videolan.org>
+ * Copyright (c) 2021 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/avassert.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+#undef pixel
+#if DEPTH == 8
+#define pixel uint8_t
+#else
+#define pixel uint16_t
+#endif
+
+#undef fn
+#undef fn2
+#undef fn3
+
+#define fn3(a,b)   a##_##b
+#define fn2(a,b)   fn3(a,b)
+#define fn(a)      fn2(a, DEPTH)
+
+static void fn(XDeintNxNField)(pixel *dst, int dst_linesize,
+                               const pixel *src, int src_linesize,
+                               int width, int height)
+{
+    /* Interlaced */
+    for (int y = 0; y < height; y += 2) {
+        memcpy(dst, src, width * sizeof(pixel));
+        dst += dst_linesize;
+
+        if (y < height - 2) {
+            for (int x = 0; x < width; x++)
+                dst[x] = (src[x] + src[x + 2 * src_linesize] + 1) >> 1;
+        } else {
+            /* Blend last line */
+            for (int x = 0; x < width; x++)
+                dst[x] = (src[x] + src[x - 2 * src_linesize] + 1) >> 1;
+        }
+        dst += 1 * dst_linesize;
+        src += 2 * src_linesize;
+    }
+}
+
+static int fn(XDeintNxNDetect)(const pixel *src, int src_linesize,
+                               int height, int width, int threshold)
+{
+    int fc = 0;
+
+    /* Detect interlacing */
+    for (int y = 0; y < height - 2; y += 2) {
+        int ff = 0, fr = 0;
+        const pixel *s = &src[y * src_linesize];
+
+        for (int x = 0; x < width; x++) {
+            fr += ssd(s[x] - s[1 * src_linesize+x]);
+            ff += ssd(s[x] - s[2 * src_linesize+x]);
+        }
+
+        if (ff < fr || fr > threshold)
+            fc++;
+    }
+
+    return fc >= 1;
+}
+
+static void fn(XDeintNxNFrame)(pixel *dst, int dst_linesize,
+                               const pixel *src, int src_linesize,
+                               int width, int height)
+{
+    /* Progressive */
+    for (int y = 0; y < height; y += 2) {
+        memcpy(dst, src, width * sizeof(pixel));
+        dst += dst_linesize;
+
+        if (y < height - 2) {
+            for (int x = 0; x < width; x++)
+                dst[x] = (src[x] + 2 * src[x + src_linesize] + src[x + 2 * src_linesize] + 2) >> 2;
+        } else {
+            /* Blend last line */
+            for (int x = 0; x < width; x++)
+                dst[x] = (src[x] + src[src_linesize + x] + 1) >> 1;
+        }
+
+        dst += 1 * dst_linesize;
+        src += 2 * src_linesize;
+    }
+}
+
+static void fn(XDeintNxN)(pixel *dst, int dst_linesize, const pixel *src,
+                          int src_linesize, int width, int height, int threshold)
+{
+    if (fn(XDeintNxNDetect)(src, src_linesize, width, height, threshold))
+        fn(XDeintNxNField)(dst, dst_linesize, src, src_linesize, width, height);
+    else
+        fn(XDeintNxNFrame)(dst, dst_linesize, src, src_linesize, width, height);
+}
+
+static void fn(XDeint8x8Merge)(pixel *dst,  int dst_linesize,
+                               const pixel *src1, const pixel *src2,
+                               int src_linesize)
+{
+    /* Progressive */
+    for (int y = 0; y < 8; y += 2) {
+        memcpy(dst, src1, 8 * sizeof(pixel));
+        dst += dst_linesize;
+
+        for (int x = 0; x < 8; x++)
+            dst[x] = (src1[x] + 6 * src2[x] +
+                      src1[x + src_linesize] + 4) >> 3;
+
+        dst += dst_linesize;
+        src1 += src_linesize * 2;
+        src2 += src_linesize * 2;
+    }
+}
+
+static void fn(XDeint8x8Field)(pixel *dst, int dst_linesize,
+                               const pixel *src, int src_linesize)
+{
+    /* Interlaced */
+    for (int y = 0; y < 8; y += 2) {
+        memcpy(dst, src, 8 * sizeof(pixel));
+        dst += dst_linesize;
+
+        for (int x = 0; x < 8; x++) {
+            const pixel *src1 = src;
+            const pixel *src2 = src + 2 * src_linesize;
+            const int64_t c0 = ssd(src1[x-4]-src2[x-2]) + ssd(src1[x-3]-src2[x-1]) +
+                               ssd(src1[x-2]-src2[x+0]) + ssd(src1[x-1]-src2[x+1]) +
+                               ssd(src1[x+0]-src2[x+2]) + ssd(src1[x+1]-src2[x+3]) +
+                               ssd(src1[x+2]-src2[x+4]) + ssd(src1[x+3]-src2[x+5]);
+
+            const int64_t c1 = ssd(src1[x-3]-src2[x-3]) + ssd(src1[x-2]-src2[x-2]) +
+                               ssd(src1[x-1]-src2[x-1]) + ssd(src1[x+0]-src2[x+0]) +
+                               ssd(src1[x+1]-src2[x+1]) + ssd(src1[x+2]-src2[x+2]) +
+                               ssd(src1[x+3]-src2[x+3]) + ssd(src1[x+4]-src2[x+4]);
+
+            const int64_t c2 = ssd(src1[x-2]-src2[x-4]) + ssd(src1[x-1]-src2[x-3]) +
+                               ssd(src1[x+0]-src2[x-2]) + ssd(src1[x+1]-src2[x-1]) +
+                               ssd(src1[x+2]-src2[x+0]) + ssd(src1[x+3]-src2[x+1]) +
+                               ssd(src1[x+4]-src2[x+2]) + ssd(src1[x+5]-src2[x+3]);
+
+            if (c0 < c1 && c1 <= c2)
+                dst[x] = (src1[x-1] + src2[x+1] + 1) >> 1;
+            else if (c2 < c1 && c1 <= c0)
+                dst[x] = (src1[x+1] + src2[x-1] + 1) >> 1;
+            else
+                dst[x] = (src1[x+0] + src2[x+0] + 1) >> 1;
+        }
+
+        dst += 1 * dst_linesize;
+        src += 2 * src_linesize;
+    }
+}
+
+static void fn(XDeint8x8FieldE)(pixel *dst, int dst_linesize,
+                                const pixel *src, int src_linesize)
+{
+    /* Interlaced */
+    for (int y = 0; y < 8; y += 2) {
+        memcpy(dst, src, 8 * sizeof(pixel));
+        dst += dst_linesize;
+        for (int x = 0; x < 8; x++)
+            dst[x] = (src[x] + src[x + 2 * src_linesize] + 1) >> 1;
+
+        dst += 1 * dst_linesize;
+        src += 2 * src_linesize;
+    }
+}
+
+static int fn(XDeint8x8Detect)(const pixel *src, int src_linesize, int threshold)
+{
+    int fc = 0;
+
+    /* Detect interlacing */
+    for (int y = 0; y < 8; y += 2) {
+        int ff = 0, fr = 0;
+
+        for (int x = 0; x < 8; x++) {
+            fr += ssd(src[x] - src[1 * src_linesize + x]) +
+                  ssd(src[src_linesize + x] - src[2 * src_linesize + x]);
+            ff += ssd(src[x] - src[2 * src_linesize + x]) +
+                  ssd(src[src_linesize + x] - src[3 * src_linesize + x]);
+        }
+
+        if (ff < fr || fr > threshold)
+            fc++;
+
+        src += 2 * src_linesize;
+    }
+
+    return fc >= 1;
+}
+
+static void fn(XDeintBand8x8)(pixel *dst, int dst_linesize,
+                              const pixel *src, int src_linesize,
+                              const int mbx, int modx, int threshold)
+{
+    for (int x = 0; x < mbx; x++) {
+        if (fn(XDeint8x8Detect)(src, src_linesize, threshold)) {
+            if (x == 0 || x == mbx - 1)
+                fn(XDeint8x8FieldE)(dst, dst_linesize, src, src_linesize);
+            else
+                fn(XDeint8x8Field)(dst, dst_linesize, src, src_linesize);
+        } else {
+            fn(XDeint8x8Merge)(dst, dst_linesize,
+                               src + 0 * src_linesize, src + 1 * src_linesize,
+                               src_linesize);
+        }
+
+        dst += 8;
+        src += 8;
+    }
+
+    if (modx)
+        fn(XDeintNxN)(dst, dst_linesize, src, src_linesize, modx, 8, threshold);
+}
+
+static int fn(deinterlace_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    XDIFContext *s = ctx->priv;
+    ThreadData *td = arg;
+    AVFrame *out = td->out;
+    AVFrame *in = td->in;
+    const int threshold = s->threshold;
+    const int first = s->field == (s->parity == -1 ? in->top_field_first == 0 : s->parity == 0 ? 0 : 1);
+    const int flip = first ? 1 : -1;
+
+    for (int plane = 0; plane < s->nb_planes; plane++) {
+        const int width = s->planewidth[plane];
+        const int height = s->planeheight[plane];
+        const uint8_t *src_data = in->data[plane] + (flip == -1) * (height - 1) * in->linesize[plane];
+        uint8_t *dst_data = out->data[plane] + (flip == -1) * (height - 1) * out->linesize[plane];
+        const int src_linesize = flip * in->linesize[plane] / sizeof(pixel);
+        const int dst_linesize = flip * out->linesize[plane] / sizeof(pixel);
+        const int start = FFALIGN((height * jobnr) / nb_jobs, 8);
+        const int end = FFMIN(height, FFALIGN((height * (jobnr+1)) / nb_jobs, 8));
+        const int mby = FFMAX((end - start) / 8 - ((jobnr + 1) == nb_jobs), 0);
+        const int mbx = width / 8;
+        const int mody = (end - start) - 8 * mby;
+        const int modx = width - 8 * mbx;
+
+        for (int y = 0; y < mby; y++) {
+            const pixel *src = (const pixel *)src_data + (start + y * 8) * src_linesize;
+            pixel *dst = (pixel *)dst_data + (start + y * 8) * dst_linesize;
+
+            fn(XDeintBand8x8)(dst, dst_linesize, src, src_linesize, mbx, modx, threshold);
+        }
+
+        if (mody) {
+            const pixel *src = (const pixel *)src_data + (start + mby * 8) * src_linesize;
+            pixel *dst = (pixel *)dst_data + (start + mby * 8) * dst_linesize;
+
+            for (int x = 0; x < mbx; x++) {
+                fn(XDeintNxN)(dst, dst_linesize, src, src_linesize, 8, mody, threshold);
+
+                dst += 8;
+                src += 8;
+            }
+
+            if (modx)
+                fn(XDeintNxN)(dst, dst_linesize, src, src_linesize, modx, mody, threshold);
+        }
+    }
+
+    return 0;
+}
