[FFmpeg-devel] [PATCH 2/2] lavfi/WIP: port IVTC filters from vapoursynth.

Clément Bœsch ubitux at gmail.com
Sun Mar 24 07:40:47 CET 2013


TODO
 - fieldmatch: test properly
 - fieldmatch: make sure modes are working as expected
 - fieldmatch: flushing
 - decimate: document AVOptions
 - decimate: flushing
 - decimate: setfps
 - check matching input sources parameters
 - check memleaks
 - write documentation
 - version bumps
 - Changelog
---
 libavfilter/Makefile        |   2 +
 libavfilter/allfilters.c    |   2 +
 libavfilter/vf_decimate.c   | 359 +++++++++++++++++
 libavfilter/vf_fieldmatch.c | 946 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 1309 insertions(+)
 create mode 100644 libavfilter/vf_decimate.c
 create mode 100644 libavfilter/vf_fieldmatch.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 0767086..3905655 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -107,6 +107,7 @@ OBJS-$(CONFIG_COPY_FILTER)                   += vf_copy.o
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
 OBJS-$(CONFIG_CURVES_FILTER)                 += vf_curves.o
+OBJS-$(CONFIG_DECIMATE_FILTER)               += vf_decimate.o
 OBJS-$(CONFIG_DELOGO_FILTER)                 += vf_delogo.o
 OBJS-$(CONFIG_DESHAKE_FILTER)                += vf_deshake.o
 OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
@@ -114,6 +115,7 @@ OBJS-$(CONFIG_DRAWTEXT_FILTER)               += vf_drawtext.o
 OBJS-$(CONFIG_EDGEDETECT_FILTER)             += vf_edgedetect.o
 OBJS-$(CONFIG_FADE_FILTER)                   += vf_fade.o
 OBJS-$(CONFIG_FIELD_FILTER)                  += vf_field.o
+OBJS-$(CONFIG_FIELDMATCH_FILTER)             += vf_fieldmatch.o
 OBJS-$(CONFIG_FIELDORDER_FILTER)             += vf_fieldorder.o
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
 OBJS-$(CONFIG_FRAMESTEP_FILTER)              += vf_framestep.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 0b2bd1a..23df432 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -103,6 +103,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(CROP,           crop,           vf);
     REGISTER_FILTER(CROPDETECT,     cropdetect,     vf);
     REGISTER_FILTER(CURVES,         curves,         vf);
+    REGISTER_FILTER(DECIMATE,       decimate,       vf);
     REGISTER_FILTER(DELOGO,         delogo,         vf);
     REGISTER_FILTER(DESHAKE,        deshake,        vf);
     REGISTER_FILTER(DRAWBOX,        drawbox,        vf);
@@ -110,6 +111,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(EDGEDETECT,     edgedetect,     vf);
     REGISTER_FILTER(FADE,           fade,           vf);
     REGISTER_FILTER(FIELD,          field,          vf);
+    REGISTER_FILTER(FIELDMATCH,     fieldmatch,     vf);
     REGISTER_FILTER(FIELDORDER,     fieldorder,     vf);
     REGISTER_FILTER(FORMAT,         format,         vf);
     REGISTER_FILTER(FPS,            fps,            vf);
diff --git a/libavfilter/vf_decimate.c b/libavfilter/vf_decimate.c
new file mode 100644
index 0000000..2345746
--- /dev/null
+++ b/libavfilter/vf_decimate.c
@@ -0,0 +1,359 @@
+/*
+ * Copyright (c) 2012 Fredrik Mellbin
+ * Copyright (c) 2013 Clément Bœsch
+ *
+ * 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/opt.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "internal.h"
+
+#define INPUT_MAIN     0
+#define INPUT_CLEANSRC 1
+
+struct qitem {
+    AVFrame *frame;
+    int64_t maxbdiff;
+    int64_t totdiff;
+};
+
+typedef struct {
+    const AVClass *class;
+
+    int cycle;              ///< number of frames to base the analysis on
+    struct qitem *queue;    ///< window of cycle frames and the associated data diff
+    int fid;                ///< current frame id in the queue
+    int filled;             ///< 1 if the queue is filled, 0 otherwise
+    AVFrame *last;          ///< last frame from the previous queue
+
+    int ppsrc;
+    AVFrame **clean_src;
+    int got_frame[2];       ///< frame request flag for each input stream
+
+    int hsub, vsub;         ///< chroma subsampling values
+    int depth;
+    int chroma;
+    double dupthresh_flt;
+    double scthresh_flt;
+    int64_t dupthresh;
+    int64_t scthresh;
+    int blockx;
+    int blocky;
+    int nxblocks;
+    int nyblocks;
+    int bdiffsize;
+    int64_t *bdiffs;
+} DecimateContext;
+
+#define OFFSET(x) offsetof(DecimateContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption decimate_options[] = {
+    { "cycle",     "", OFFSET(cycle), AV_OPT_TYPE_INT, {.i64 = 5}, 2, 25, FLAGS },
+    { "dupthresh", "", OFFSET(dupthresh_flt), AV_OPT_TYPE_FLOAT, {.dbl =  1.1}, 0, 100, FLAGS },
+    { "scthresh",  "", OFFSET(scthresh_flt),  AV_OPT_TYPE_FLOAT, {.dbl = 15.0}, 0, 100, FLAGS },
+    { "blockx",    "", OFFSET(blockx), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS },
+    { "blocky",    "", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64 = 32}, 4, 1<<9, FLAGS },
+    { "ppsrc",     "", OFFSET(ppsrc), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    { "chroma",    "", OFFSET(chroma), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(decimate);
+
+static void calc_diffs(const DecimateContext *dm, struct qitem *q,
+                       const AVFrame *f1, const AVFrame *f2)
+{
+    int64_t *bdiffs = dm->bdiffs;
+    int plane;
+    int x, y, xl;
+    int i, j;
+    int numplanes = dm->chroma ? 3 : 1;
+    int64_t maxdiff = -1;
+
+    memset(bdiffs, 0, dm->bdiffsize * sizeof(*bdiffs));
+
+    for (plane = 0; plane < numplanes; plane++) {
+        const int linesize1 = f1->linesize[plane];
+        const int linesize2 = f2->linesize[plane];
+        const uint8_t *f1p = f1->data[plane];
+        const uint8_t *f2p = f2->data[plane];
+
+        int width    = plane ? f1->width  >> dm->hsub : f1->width;
+        int height   = plane ? f1->height >> dm->vsub : f1->height;
+        int hblockx  = dm->blockx / 2;
+        int hblocky  = dm->blocky / 2;
+        int nxblocks = dm->nxblocks;
+
+        if (plane) {
+            hblockx >>= dm->hsub;
+            hblocky >>= dm->vsub;
+        }
+
+        for (y = 0; y < height; y++) {
+            int ydest = y / hblocky;
+            int xdest = 0;
+
+            if (dm->depth == 8) {
+                for (x = 0; x < width; x += hblockx) {
+                    int acc = 0;
+                    int m = FFMIN(width, x + hblockx);
+                    for (xl = x; xl < m; xl++)
+                        acc += abs(f1p[xl] - f2p[xl]);
+                    bdiffs[ydest * nxblocks + xdest] += acc;
+                    xdest++;
+                }
+            } else {
+                for (x = 0; x < width; x += hblockx) {
+                    int acc = 0;
+                    int m = FFMIN(width, x + hblockx);
+                    for (xl = x; xl < m; xl++)
+                        acc += abs(((const uint16_t *)f1p)[xl] - ((const uint16_t *)f2p)[xl]);
+                    bdiffs[ydest * nxblocks + xdest] += acc;
+                    xdest++;
+                }
+            }
+            f1p += linesize1;
+            f2p += linesize2;
+        }
+    }
+
+    for (i = 0; i < dm->nyblocks - 1; i++) {
+        for (j = 0; j < dm->nxblocks - 1; j++) {
+            int64_t tmp = bdiffs[      i * dm->nxblocks + j    ]
+                        + bdiffs[      i * dm->nxblocks + j + 1]
+                        + bdiffs[(i + 1) * dm->nxblocks + j    ]
+                        + bdiffs[(i + 1) * dm->nxblocks + j + 1];
+            if (tmp > maxdiff)
+                maxdiff = tmp;
+        }
+    }
+
+    q->totdiff = 0;
+    for (i = 0; i < dm->bdiffsize; i++)
+        q->totdiff += bdiffs[i];
+    q->maxbdiff = maxdiff;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    int scpos = -1, duppos = -1;
+    int drop, i, lowest = 0, ret;
+    AVFilterContext *ctx  = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    DecimateContext *dm   = ctx->priv;
+    AVFrame *prv;
+
+    if (inlink == ctx->inputs[INPUT_MAIN]) {
+        dm->queue[dm->fid].frame = in;
+        dm->got_frame[INPUT_MAIN] = 1;
+    } else {
+        dm->clean_src[dm->fid] = in;
+        dm->got_frame[INPUT_CLEANSRC] = 1;
+    }
+    if (!dm->got_frame[INPUT_MAIN] || (dm->ppsrc && !dm->got_frame[INPUT_CLEANSRC]))
+        return 0;
+    dm->got_frame[INPUT_MAIN] = dm->got_frame[INPUT_CLEANSRC] = 0;
+
+    /* update frame metrics */
+    prv = dm->fid ? dm->queue[dm->fid - 1].frame : dm->last;
+    if (!prv)
+        prv = in;
+    calc_diffs(dm, &dm->queue[dm->fid], prv ? prv : in, in);
+    if (++dm->fid != dm->cycle)
+        return 0;
+    av_frame_free(&dm->last);
+    dm->last = av_frame_clone(in);
+    dm->fid = 0;
+
+    /* we have a complete cycle, select the frame to drop */
+    lowest = 0;
+    for (i = 0; i < dm->cycle; i++) {
+        if (dm->queue[i].totdiff > dm->scthresh)
+            scpos = i;
+        if (dm->queue[i].maxbdiff < dm->queue[lowest].maxbdiff)
+            lowest = i;
+    }
+    if (dm->queue[lowest].maxbdiff < dm->dupthresh)
+        duppos = lowest;
+    drop = scpos >= 0 && duppos < 0 ? scpos : lowest;
+
+    /* XXX: debug */
+    for (i = 0; i < dm->cycle; i++) {
+        av_log(ctx, AV_LOG_VERBOSE,"#%d: totdiff=%ld maxbdiff=%ld%s%s%s%s\n",
+               i, dm->queue[i].totdiff, dm->queue[i].maxbdiff,
+               i == scpos  ? " [SC]"     : "",
+               i == duppos ? " [DUP]"    : "",
+               i == lowest ? " [LOWEST]" : "",
+               i == drop   ? " <-- DROP" : "");
+    }
+
+    /* push all frames except the drop */
+    ret = AVERROR_BUG;
+    for (i = 0; i < dm->cycle; i++) {
+        if (i == drop) {
+            if (dm->ppsrc)
+                av_frame_free(&dm->clean_src[i]);
+            av_frame_free(&dm->queue[i].frame);
+        } else {
+            AVFrame *frame = dm->queue[i].frame;
+            if (dm->ppsrc) {
+                av_frame_free(&frame);
+                frame = dm->clean_src[i];
+            }
+            ret = ff_filter_frame(outlink, frame);
+            if (ret < 0)
+                break;
+        }
+    }
+    return ret;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    int i, max_value;
+    AVFilterContext *ctx = inlink->dst;
+    DecimateContext *dm = ctx->priv;
+    const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
+    const int w = inlink->w;
+    const int h = inlink->h;
+
+    dm->hsub      = pix_desc->log2_chroma_w;
+    dm->vsub      = pix_desc->log2_chroma_h;
+    dm->depth     = pix_desc->comp[0].depth_minus1 + 1;
+    max_value     = (1 << dm->depth) - 1;
+    dm->scthresh  = (int64_t)(((int64_t)max_value *          w * h          * dm->scthresh_flt)  / 100);
+    dm->dupthresh = (int64_t)(((int64_t)max_value * dm->blockx * dm->blocky * dm->dupthresh_flt) / 100);
+    dm->nxblocks  = (w + dm->blockx/2 - 1) / (dm->blockx/2);
+    dm->nyblocks  = (h + dm->blocky/2 - 1) / (dm->blocky/2);
+    dm->bdiffsize = dm->nxblocks * dm->nyblocks;
+    dm->bdiffs    = av_malloc(dm->bdiffsize * sizeof(*dm->bdiffs));
+    dm->queue     = av_calloc(dm->cycle, sizeof(*dm->queue));
+
+    if (!dm->bdiffs || !dm->queue)
+        return AVERROR(ENOMEM);
+
+    if (dm->ppsrc) {
+        dm->clean_src = av_calloc(dm->cycle, sizeof(*dm->clean_src));
+        if (!dm->clean_src)
+            return AVERROR(ENOMEM);
+    }
+
+    for (i = 0; i < dm->cycle; i++) {
+        dm->queue[i].maxbdiff = -1;
+        dm->queue[i].totdiff  = -1;
+    }
+
+    //TODO: adjust FPS
+
+    return 0;
+}
+
+static av_cold int decimate_init(AVFilterContext *ctx, const char *args)
+{
+    const DecimateContext *dm = ctx->priv;
+    AVFilterPad pad = {
+        .name         = av_strdup("main"),
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .config_props = config_input,
+    };
+
+    if (!pad.name)
+        return AVERROR(ENOMEM);
+    ff_insert_inpad(ctx, INPUT_MAIN, &pad);
+
+    if (dm->ppsrc) {
+        pad.name = av_strdup("clean_src");
+        pad.config_props = NULL;
+        if (!pad.name)
+            return AVERROR(ENOMEM);
+        ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad);
+    }
+
+    if ((dm->blockx & (dm->blockx - 1)) ||
+        (dm->blocky & (dm->blocky - 1))) {
+        av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static av_cold void decimate_uninit(AVFilterContext *ctx)
+{
+    DecimateContext *dm = ctx->priv;
+    av_frame_free(&dm->last);
+    av_freep(&dm->bdiffs);
+    av_freep(&dm->queue);
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    int ret;
+    AVFilterContext *ctx = outlink->src;
+    DecimateContext *fm = ctx->priv;
+
+    if (!fm->got_frame[INPUT_MAIN] &&
+        (ret = ff_request_frame(ctx->inputs[INPUT_MAIN])) < 0)
+        return ret;
+    if (fm->ppsrc && !fm->got_frame[INPUT_CLEANSRC] &&
+        (ret = ff_request_frame(ctx->inputs[INPUT_CLEANSRC])) < 0)
+        return ret;
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_YUV420P,
+        AV_PIX_FMT_YUV422P,
+        /* TODO: >8bits pix fmt should be supported, add some */
+        AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+// TODO: check that both sources are close enough
+static int config_output(AVFilterLink *outlink) { return 0; }
+
+static const AVFilterPad decimate_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .request_frame = request_frame,
+        .config_props  = config_output,
+    },
+    { NULL }
+};
+static const char *const shorthand[] = { "cycle", "dupthresh", "scthresh", NULL };
+
+AVFilter avfilter_vf_decimate = {
+    .name          = "decimate",
+    .description   = NULL_IF_CONFIG_SMALL("Decimate frames (post field matching filter)."),
+    .init          = decimate_init,
+    .uninit        = decimate_uninit,
+    .priv_size     = sizeof(DecimateContext),
+    .query_formats = query_formats,
+    .outputs       = decimate_outputs,
+    .priv_class    = &decimate_class,
+    .shorthand     = shorthand,
+};
diff --git a/libavfilter/vf_fieldmatch.c b/libavfilter/vf_fieldmatch.c
new file mode 100644
index 0000000..5d7f55e
--- /dev/null
+++ b/libavfilter/vf_fieldmatch.c
@@ -0,0 +1,946 @@
+/*
+ * Copyright (c) 2012 Fredrik Mellbin
+ * Copyright (c) 2013 Clément Bœsch
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * Fieldmatching filter, ported from VFM filter (vaporsynth) by Clément.
+ * Fredrik Mellbin is the author of the VIVTC/VFM filter, which is itself a
+ * light clone of the TIVTC/TFM (avisynth) filter written by Kevin Stone
+ * (tritical), the original author.
+ *
+ * @see http://bengal.missouri.edu/~kes25c/
+ * @see http://www.vapoursynth.com/about/
+ */
+
+#include <inttypes.h>
+
+#include "libavutil/avassert.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/timestamp.h"
+#include "avfilter.h"
+#include "internal.h"
+
+#define INPUT_MAIN     0
+#define INPUT_CLEANSRC 1
+
+enum fieldmatch_parity {
+    FM_PARITY_AUTO   = -1,
+    FM_PARITY_BOTTOM =  0,
+    FM_PARITY_TOP    =  1,
+};
+
+enum matching_mode {
+    MODE_PC,
+    MODE_PC_N,
+    MODE_PC_U,
+    MODE_PC_N_UB,
+    MODE_PCN,
+    MODE_PCN_UB,
+    NB_MODE
+};
+
+enum mic_matching_mode {
+    MICMATCH_NONE,
+    MICMATCH_XXX,
+    NB_MICMATCH
+};
+
+typedef struct {
+    const AVClass *class;
+
+    AVFrame *prv,  *src,  *nxt;     ///< main sliding window of 3 frames
+    AVFrame *prv2, *src2, *nxt2;    ///< sliding window of the optional second stream
+    int64_t frame_count;            ///< output frame counter
+    int got_frame[2];               ///< frame request flag for each input stream
+    int hsub, vsub;                 ///< chroma subsampling values
+
+    /* options */
+    int order;
+    int ppsrc;
+    enum matching_mode mode;
+    int field;
+    int mchroma;
+    int y0, y1;
+    double scthresh;
+    enum mic_matching_mode micmatch;
+    int micout;
+    int cthresh;
+    int chroma;
+    int blockx, blocky;
+    int mi;
+
+    /* misc buffers */
+    uint8_t *map_data[4];
+    int map_linesize[4];
+    uint8_t *cmask_data[4];
+    int cmask_linesize[4];
+    int *c_array;
+    int tpitchy, tpitchuv;
+    uint8_t *tbuffer;
+} FieldMatchContext;
+
+#define OFFSET(x) offsetof(FieldMatchContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption fieldmatch_options[] = {
+    { "order", "specify the assumed field order", OFFSET(order), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "order" },
+        { "auto", "auto detect parity",        0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO},    INT_MIN, INT_MAX, FLAGS, "order" },
+        { "bff",  "assume bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM},  INT_MIN, INT_MAX, FLAGS, "order" },
+        { "tff",  "assume top field first",    0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP},     INT_MIN, INT_MAX, FLAGS, "order" },
+    { "mode", "set the matching mode or strategy to use", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=MODE_PC}, 0, NB_MODE-1, FLAGS, "mode" },
+        { "pc",      "2-way match (p/c)",                                                                    0, AV_OPT_TYPE_CONST, {.i64=MODE_PC},      INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "pc_n",    "2-way match + 3rd match on combed (p/c + u)",                                          0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N},    INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "pc_u",    "2-way match + 3rd match (same order) on combed (p/c + u)",                             0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_U},    INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "pc_n_ub", "2-way match + 3rd match on combed + 4th/5th matches if still combed (p/c + u + u/b)",  0, AV_OPT_TYPE_CONST, {.i64=MODE_PC_N_UB}, INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "pcn",     "3-way match (p/c/n)",                                                                  0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN},     INT_MIN, INT_MAX, FLAGS, "mode" },
+        { "pcn_ub",  "3-way match + 4th/5th matches on combed (p/c/n + u/b)",                                0, AV_OPT_TYPE_CONST, {.i64=MODE_PCN_UB},  INT_MIN, INT_MAX, FLAGS, "mode" },
+    { "ppsrc", "mark main input as postprocessed and activated clean source input stream", OFFSET(ppsrc), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    { "field", "set the field to match from", OFFSET(field), AV_OPT_TYPE_INT, {.i64=FM_PARITY_AUTO}, -1, 1, FLAGS, "field" },
+        { "auto",   "automatic (same value as 'order')",    0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_AUTO},    INT_MIN, INT_MAX, FLAGS, "field" },
+        { "bottom", "bottom field",                         0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_BOTTOM},  INT_MIN, INT_MAX, FLAGS, "field" },
+        { "top",    "top field",                            0, AV_OPT_TYPE_CONST, {.i64=FM_PARITY_TOP},     INT_MIN, INT_MAX, FLAGS, "field" },
+    { "mchroma", "set whether or not chroma is included during the match compararisons", OFFSET(mchroma), AV_OPT_TYPE_INT, {.i64=1}, 0, 1,  FLAGS },
+    { "y0", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    { "y1", "define an exclusion band which excludes the lines between y0 and y1 from the field matching decision", OFFSET(y0), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    { "scthresh", "set scene change detection threshold", OFFSET(scthresh), AV_OPT_TYPE_FLOAT, {.dbl=12}, 0, 100, FLAGS },
+    { "micmatch", "XXX", OFFSET(micmatch), AV_OPT_TYPE_INT, {.i64=1}, MICMATCH_NONE, NB_MICMATCH, FLAGS, "micmatching" },
+        { "none", "disable micmatching", 0, AV_OPT_TYPE_CONST, {.i64=MICMATCH_NONE}, INT_MIN, INT_MAX, FLAGS, "micmatching" },
+        { "none", "disable micmatching", 0, AV_OPT_TYPE_CONST, {.i64=MICMATCH_NONE}, INT_MIN, INT_MAX, FLAGS, "micmatching" },
+        { "none", "disable micmatching", 0, AV_OPT_TYPE_CONST, {.i64=MICMATCH_NONE}, INT_MIN, INT_MAX, FLAGS, "micmatching" },
+    { "micout",   "XXX", OFFSET(micout),   AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS },
+    { "cthresh", "set the area combing threshold used for combed frame detection",       OFFSET(cthresh), AV_OPT_TYPE_INT, {.i64=10}, -1, INT_MAX, FLAGS },
+    { "chroma", "set whether or not chroma is considered in the combed frame decision", OFFSET(chroma), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
+    { "blockx", "set the x-axis size of the window used during combed frame detection", OFFSET(blockx), AV_OPT_TYPE_INT, {.i64=16}, 4, 1<<9, FLAGS },
+    { "blocky", "set the y-axis size of the window used during combed frame detection", OFFSET(blocky), AV_OPT_TYPE_INT, {.i64=16}, 4, 1<<9, FLAGS },
+    { "mi", "set the number of combed pixels inside any of the blocky by blockx size blocks on the frame for the frame to be detected as combed", OFFSET(mi), AV_OPT_TYPE_INT, {.i64=85}, 0, INT_MAX, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(fieldmatch);
+
+static int get_width(const FieldMatchContext *fm, const AVFrame *f, int plane)
+{
+    return plane ? f->width >> fm->hsub : f->width;
+}
+
+static int get_height(const FieldMatchContext *fm, const AVFrame *f, int plane)
+{
+    return plane ? f->height >> fm->vsub : f->height;
+}
+
+static int64_t luma_abs_diff(const AVFrame *f1, const AVFrame *f2)
+{
+    int x, y;
+    const uint8_t *srcp1 = f1->data[0];
+    const uint8_t *srcp2 = f2->data[0];
+    const int src1_linesize = f1->linesize[0];
+    const int src2_linesize = f2->linesize[0];
+    const int width  = f1->width;
+    const int height = f1->height;
+    int64_t acc = 0;
+
+    for (y = 0; y < height; y++) {
+        for (x = 0; x < width; x++)
+            acc += abs(srcp1[x] - srcp2[x]);
+        srcp1 += src1_linesize;
+        srcp2 += src2_linesize;
+    }
+    return acc;
+}
+
+static void fill_buf(uint8_t *data, int w, int h, int linesize, uint8_t v)
+{
+    int y;
+
+    for (y = 0; y < h; y++) {
+        memset(data, v, w);
+        data += linesize;
+    }
+}
+
+static int calc_combed_score(const FieldMatchContext *fm, const AVFrame *src)
+{
+    int x, y, plane, max_v = 0;
+    const int cthresh = fm->cthresh;
+    const int cthresh6 = cthresh*6;
+
+    for (plane = 0; plane < (fm->chroma ? 3 : 1); plane++) {
+        const uint8_t *srcp = src->data[plane];
+        const int src_linesize = src->linesize[plane];
+        const int width  = get_width (fm, src, plane);
+        const int height = get_height(fm, src, plane);
+        uint8_t *cmkp = fm->cmask_data[plane];
+        const int cmk_linesize = fm->cmask_linesize[plane];
+
+        if (cthresh < 0) {
+            fill_buf(cmkp, width, height, cmk_linesize, 0xff);
+            continue;
+        }
+        fill_buf(cmkp, width, height, cmk_linesize, 0);
+
+        /* [1 -3 4 -3 1] vertical filter */
+#define FILTER(xm2, xm1, xp1, xp2) \
+        abs(  4 * srcp[x] \
+             -3 * (srcp[x + (xm1)*src_linesize] + srcp[x + (xp1)*src_linesize]) \
+             +    (srcp[x + (xm2)*src_linesize] + srcp[x + (xp2)*src_linesize])) > cthresh6
+
+        /* first line */
+        for (x = 0; x < width; x++) {
+            const int sfirst = srcp[x] - srcp[x + src_linesize];
+            if (sfirst > cthresh || sfirst < -cthresh) {
+                if (FILTER(2, 1, 1, 2))
+                    cmkp[x] = 0xff;
+            }
+        }
+        srcp += src_linesize;
+        cmkp += cmk_linesize;
+
+        /* second line */
+        for (x = 0; x < width; x++) {
+            const int sfirst  = srcp[x] - srcp[x - src_linesize];
+            const int ssecond = srcp[x] - srcp[x + src_linesize];
+            if ((sfirst >  cthresh && ssecond >  cthresh) ||
+                (sfirst < -cthresh && ssecond < -cthresh)) {
+                if (FILTER(2, -1, 1, 2))
+                    cmkp[x] = 0xff;
+            }
+        }
+        srcp += src_linesize;
+        cmkp += cmk_linesize;
+
+        /* all lines minus first two and last two */
+        for (y = 2; y < height-2; y++) {
+            for (x = 0; x < width; x++) {
+                const int sfirst  = srcp[x] - srcp[x - src_linesize];
+                const int ssecond = srcp[x] - srcp[x + src_linesize];
+                if ((sfirst >  cthresh && ssecond >  cthresh) ||
+                    (sfirst < -cthresh && ssecond < -cthresh)) {
+                    if (FILTER(-2, -1, 1, 2))
+                        cmkp[x] = 0xff;
+                }
+            }
+            srcp += src_linesize;
+            cmkp += cmk_linesize;
+        }
+
+        /* before-last line */
+        for (x = 0; x < width; x++) {
+            const int sfirst  = srcp[x] - srcp[x - src_linesize];
+            const int ssecond = srcp[x] - srcp[x + src_linesize];
+            if ((sfirst >  cthresh && ssecond >  cthresh) ||
+                (sfirst < -cthresh && ssecond < -cthresh)) {
+                if (FILTER(-2, -1, 1, -2))
+                    cmkp[x] = 0xff;
+            }
+        }
+        srcp += src_linesize;
+        cmkp += cmk_linesize;
+
+        /* last line */
+        for (x = 0; x < width; x++) {
+            const int sfirst = srcp[x] - srcp[x - src_linesize];
+            if (sfirst > cthresh || sfirst < -cthresh) {
+                if (FILTER(-2, -1, -1, -2))
+                    cmkp[x] = 0xff;
+            }
+        }
+    }
+
+    if (fm->chroma) {
+        uint8_t *cmkp  = fm->cmask_data[0];
+        uint8_t *cmkpU = fm->cmask_data[1];
+        uint8_t *cmkpV = fm->cmask_data[2];
+        const int width  = src->width  >> fm->hsub;
+        const int height = src->height >> fm->vsub;
+        const int cmk_linesize   = fm->cmask_linesize[0] * 2;
+        const int cmk_linesizeUV = fm->cmask_linesize[2];
+        uint8_t *cmkpp  = cmkp - (cmk_linesize>>1);
+        uint8_t *cmkpn  = cmkp + (cmk_linesize>>1);
+        uint8_t *cmkpnn = cmkp +  cmk_linesize;
+        for (y = 1; y < height - 1; y++) {
+            cmkpp  += cmk_linesize;
+            cmkp   += cmk_linesize;
+            cmkpn  += cmk_linesize;
+            cmkpnn += cmk_linesize;
+            cmkpV  += cmk_linesizeUV;
+            cmkpU  += cmk_linesizeUV;
+            for (x = 1; x < width-1; x++) {
+#define HAS_FF_AROUND(p, lz) (p[x-1 - lz] == 0xff || p[x - lz] == 0xff || p[x+1 - lz] == 0xff || \
+                              p[x-1     ] == 0xff ||                      p[x+1     ] == 0xff || \
+                              p[x-1 + lz] == 0xff || p[x + lz] == 0xff || p[x+1 + lz] == 0xff)
+                if ((cmkpV[x] == 0xff && HAS_FF_AROUND(cmkpV, cmk_linesizeUV)) ||
+                    (cmkpU[x] == 0xff && HAS_FF_AROUND(cmkpU, cmk_linesizeUV))) {
+                    cmkp[x] = cmkp[x + 1] = cmkpn[x] = cmkpn[x + 1] = 0xff;
+                    if (y & 1) cmkpp [x] = cmkpp [x + 1] = 0xff;
+                    else       cmkpnn[x] = cmkpnn[x + 1] = 0xff;
+                }
+            }
+        }
+    }
+
+    {
+        const int blockx = fm->blockx;
+        const int blocky = fm->blocky;
+        const int xhalf = blockx/2;
+        const int yhalf = blocky/2;
+        const int cmk_linesize = fm->cmask_linesize[0];
+        const uint8_t *cmkp    = fm->cmask_data[0] + cmk_linesize;
+        const int width  = src->width;
+        const int height = src->height;
+        const int xblocks = ((width+xhalf)/blockx) + 1;
+        const int xblocks4 = xblocks<<2;
+        const int yblocks = ((height+yhalf)/blocky) + 1;
+        int *c_array = fm->c_array;
+        const int arraysize = (xblocks*yblocks)<<2;
+        int      heighta = (height/(blocky/2))*(blocky/2);
+        const int widtha = (width /(blockx/2))*(blockx/2);
+        if (heighta == height)
+            heighta = height - yhalf;
+        memset(c_array, 0, arraysize * sizeof(*c_array));
+
+#define C_ARRAY_ADD(v) do {                         \
+    const int box1 = (x / blockx) * 4;              \
+    const int box2 = ((x + xhalf) / blockx) * 4;    \
+    c_array[temp1 + box1    ] += v;                 \
+    c_array[temp1 + box2 + 1] += v;                 \
+    c_array[temp2 + box1 + 2] += v;                 \
+    c_array[temp2 + box2 + 3] += v;                 \
+} while (0)
+
+#define VERTICAL_HALF(y_start, y_end) do {                                  \
+    for (y = y_start; y < y_end; y++) {                                     \
+        const int temp1 = (y / blocky) * xblocks4;                          \
+        const int temp2 = ((y + yhalf) / blocky) * xblocks4;                \
+        for (x = 0; x < width; x++)                                         \
+            if (cmkp[x - cmk_linesize] == 0xff &&                           \
+                cmkp[x               ] == 0xff &&                           \
+                cmkp[x + cmk_linesize] == 0xff)                             \
+                C_ARRAY_ADD(1);                                             \
+        cmkp += cmk_linesize;                                               \
+    }                                                                       \
+} while (0)
+
+        VERTICAL_HALF(1, yhalf);
+
+        for (y = yhalf; y < heighta; y += yhalf) {
+            const int temp1 = (y / blocky) * xblocks4;
+            const int temp2 = ((y + yhalf) / blocky) * xblocks4;
+
+            for (x = 0; x < widtha; x += xhalf) {
+                const uint8_t *cmkp_tmp = cmkp + x;
+                int u, v, sum = 0;
+                for (u = 0; u < yhalf; u++) {
+                    for (v = 0; v < xhalf; v++)
+                        if (cmkp_tmp[v - cmk_linesize] == 0xff &&
+                            cmkp_tmp[v               ] == 0xff &&
+                            cmkp_tmp[v + cmk_linesize] == 0xff)
+                            sum++;
+                    cmkp_tmp += cmk_linesize;
+                }
+                if (sum)
+                    C_ARRAY_ADD(sum);
+            }
+
+            for (x = widtha; x < width; x++) {
+                const uint8_t *cmkp_tmp = cmkp + x;
+                int u, sum = 0;
+                for (u = 0; u < yhalf; u++) {
+                    if (cmkp_tmp[-cmk_linesize] == 0xff &&
+                        cmkp_tmp[            0] == 0xff &&
+                        cmkp_tmp[ cmk_linesize] == 0xff)
+                        sum++;
+                    cmkp_tmp += cmk_linesize;
+                }
+                if (sum)
+                    C_ARRAY_ADD(sum);
+            }
+
+            cmkp += cmk_linesize * yhalf;
+        }
+
+        VERTICAL_HALF(heighta, height - 1);
+
+        for (x = 0; x < arraysize; x++)
+            if (c_array[x] > max_v)
+                max_v = c_array[x];
+    }
+    return max_v;
+}
+
+// the secret is that tbuffer is an interlaced, offset subset of all the lines
+static void build_abs_diff_mask(const uint8_t *prvp, int prv_linesize,
+                                const uint8_t *nxtp, int nxt_linesize,
+                                uint8_t *tbuffer,    int tbuf_linesize,
+                                int width, int height)
+{
+    int y, x;
+
+    prvp -= prv_linesize;
+    nxtp -= nxt_linesize;
+    for (y = 0; y < height; y++) {
+        for (x = 0; x < width; x++)
+            tbuffer[x] = abs(prvp[x] - nxtp[x]);
+        prvp += prv_linesize;
+        nxtp += nxt_linesize;
+        tbuffer += tbuf_linesize;
+    }
+}
+
+/**
+ * Build a map over which pixels differ a lot/a little
+ */
+static void build_diff_map(FieldMatchContext *fm,
+                           const uint8_t *prvp, int prv_linesize,
+                           const uint8_t *nxtp, int nxt_linesize,
+                           uint8_t *dstp, int dst_linesize, int height,
+                           int width, int plane)
+{
+    int x, y, u, diff, count;
+    int tpitch = plane ? fm->tpitchuv : fm->tpitchy;
+    const uint8_t *dp = fm->tbuffer + tpitch;
+
+    build_abs_diff_mask(prvp, prv_linesize, nxtp, nxt_linesize,
+                        fm->tbuffer, tpitch, width, height>>1);
+
+    for (y = 2; y < height - 2; y += 2) {
+        for (x = 1; x < width - 1; x++) {
+            diff = dp[x];
+            if (diff > 3) {
+                for (count = 0, u = x-1; u < x+2 && count < 2; u++) {
+                    count += dp[u-tpitch] > 3;
+                    count += dp[u       ] > 3;
+                    count += dp[u+tpitch] > 3;
+                }
+                if (count > 1) {
+                    dstp[x]++;
+                    if (diff > 19) {
+                        int upper = 0, lower = 0;
+                        for (count = 0, u = x-1; u < x+2 && count < 6; u++) {
+                            if (dp[u-tpitch] > 19) { count++; upper = 1; }
+                            if (dp[u       ] > 19)   count++;
+                            if (dp[u+tpitch] > 19) { count++; lower = 1; }
+                        }
+                        if (count > 3) {
+                            if (upper && lower) {
+                                dstp[x] += 2;
+                            } else {
+                                int upper2 = 0, lower2 = 0;
+                                for (u = FFMAX(x-4,0); u < FFMIN(x+5,width); u++) {
+                                    if (y != 2 &&        dp[u-2*tpitch] > 19) upper2 = 1;
+                                    if (                 dp[u-  tpitch] > 19) upper  = 1;
+                                    if (                 dp[u+  tpitch] > 19) lower  = 1;
+                                    if (y != height-4 && dp[u+2*tpitch] > 19) lower2 = 1;
+                                }
+                                if ((upper && (lower || upper2)) ||
+                                    (lower && (upper || lower2)))
+                                    dstp[x] += 2;
+                                else if (count > 5)
+                                    dstp[x] += 4;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        dp += tpitch;
+        dstp += dst_linesize;
+    }
+}
+
+static int compare_fields(FieldMatchContext *fm, int match1, int match2, int field)
+{
+    int plane, ret;
+    uint64_t accumPc = 0, accumNc = 0, accumPm = 0;
+    uint64_t accumNm = 0, accumPml = 0, accumNml = 0;
+    int norm1, norm2, mtn1, mtn2;
+    float c1, c2, mr;
+    const AVFrame *prv = fm->prv;
+    const AVFrame *src = fm->src;
+    const AVFrame *nxt = fm->nxt;
+
+    for (plane = 0; plane < (fm->mchroma ? 3 : 1); plane++) {
+        int x, y, temp1, temp2;
+        uint8_t *mapp    = fm->map_data[plane];
+        int map_linesize = fm->map_linesize[plane];
+        const uint8_t *prvp = prv->data[plane];
+        const uint8_t *srcp = src->data[plane];
+        const uint8_t *nxtp = nxt->data[plane];
+        const int prv_linesize = prv->linesize[plane];
+        const int src_linesize = src->linesize[plane];
+        const int nxt_linesize = nxt->linesize[plane];
+        const int prvf_linesize = prv_linesize << 1;
+        const int srcf_linesize = src_linesize << 1;
+        const int nxtf_linesize = nxt_linesize << 1;
+        const int width  = get_width (fm, src, plane);
+        const int height = get_height(fm, src, plane);
+        const int y0a = fm->y0 >> (plane != 0);
+        const int y1a = fm->y1 >> (plane != 0);
+        const int startx = (plane == 0 ? 8 : 4);
+        const int stopx  = width - startx;
+        const uint8_t *srcpf, *srcf, *srcnf;
+        const uint8_t *prvpf, *prvnf, *nxtpf, *nxtnf;
+
+        fill_buf(mapp, width, height, map_linesize, 0);
+
+        switch (match1) {
+        case 0: srcf = srcp + (3-field)*src_linesize; prvpf = prvp + (2-field)*prv_linesize; mapp = mapp + (2-field)*map_linesize; break;
+        case 1: srcf = srcp + (3-field)*src_linesize; prvpf = srcp + (2-field)*src_linesize; mapp = mapp + (2-field)*map_linesize; break;
+        case 2: srcf = srcp + (3-field)*src_linesize; prvpf = nxtp + (2-field)*nxt_linesize; mapp = mapp + (2-field)*map_linesize; break;
+        case 3: srcf = srcp + (2+field)*src_linesize; prvpf = prvp + (1+field)*prv_linesize; mapp = mapp + (1+field)*map_linesize; break;
+        case 4: srcf = srcp + (2+field)*src_linesize; prvpf = nxtp + (1+field)*nxt_linesize; mapp = mapp + (1+field)*map_linesize; break;
+        default: av_assert0(0);
+        }
+
+        switch (match2) {
+        case 0: nxtpf = prvp + (2-field)*prv_linesize; break;
+        case 1: nxtpf = srcp + (2-field)*src_linesize; break;
+        case 2: nxtpf = nxtp + (2-field)*nxt_linesize; break;
+        case 3: nxtpf = prvp + (1+field)*prv_linesize; break;
+        case 4: nxtpf = nxtp + (1+field)*nxt_linesize; break;
+        default: av_assert0(0);
+        }
+
+        prvnf = prvpf + prvf_linesize;
+        srcpf = srcf  - srcf_linesize;
+        srcnf = srcf  + srcf_linesize;
+        nxtnf = nxtpf + nxtf_linesize;
+        map_linesize <<= 1;
+        if ((match1 >= 3 && field == 1) || (match1 < 3 && field != 1))
+            build_diff_map(fm, prvpf, prvf_linesize, nxtpf, nxtf_linesize,
+                           mapp, map_linesize, height, width, plane);
+        else
+            build_diff_map(fm, prvnf, prvf_linesize, nxtnf, nxtf_linesize,
+                           mapp + map_linesize, map_linesize, height, width, plane);
+
+        for (y = 2; y < height - 2; y += 2) {
+            if (y0a == y1a || y < y0a || y > y1a) {
+                for (x = startx; x < stopx; x++) {
+                    if (mapp[x] > 0 || mapp[x + map_linesize] > 0) {
+                        temp1 = srcpf[x] + (srcf[x] << 2) + srcnf[x];
+
+                        temp2 = abs(3 * (prvpf[x] + prvnf[x]) - temp1);
+                        if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
+                            accumPc += temp2;
+                        if (temp2 > 42) {
+                            if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
+                                accumPm += temp2;
+                            if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
+                                accumPml += temp2;
+                        }
+
+                        temp2 = abs(3 * (nxtpf[x] + nxtnf[x]) - temp1);
+                        if (temp2 > 23 && ((mapp[x]&1) || (mapp[x + map_linesize]&1)))
+                            accumNc += temp2;
+                        if (temp2 > 42) {
+                            if ((mapp[x]&2) || (mapp[x + map_linesize]&2))
+                                accumNm += temp2;
+                            if ((mapp[x]&4) || (mapp[x + map_linesize]&4))
+                                accumNml += temp2;
+                        }
+                    }
+                }
+            }
+            prvpf += prvf_linesize;
+            prvnf += prvf_linesize;
+            srcpf += srcf_linesize;
+            srcf  += srcf_linesize;
+            srcnf += srcf_linesize;
+            nxtpf += nxtf_linesize;
+            nxtnf += nxtf_linesize;
+            mapp  += map_linesize;
+        }
+    }
+    if (accumPm < 500 && accumNm < 500 && (accumPml >= 500 || accumNml >= 500) &&
+        FFMAX(accumPml,accumNml) > 3*FFMIN(accumPml,accumNml)) {
+        accumPm = accumPml;
+        accumNm = accumNml;
+    }
+    norm1 = (int)((accumPc / 6.0f) + 0.5f);
+    norm2 = (int)((accumNc / 6.0f) + 0.5f);
+    mtn1  = (int)((accumPm / 6.0f) + 0.5f);
+    mtn2  = (int)((accumNm / 6.0f) + 0.5f);
+    c1 = ((float)FFMAX(norm1,norm2)) / ((float)FFMAX(FFMIN(norm1,norm2),1));
+    c2 = ((float)FFMAX(mtn1, mtn2))  / ((float)FFMAX(FFMIN(mtn1, mtn2), 1));
+    mr = ((float)FFMAX(mtn1, mtn2))  / ((float)FFMAX(FFMAX(norm1,norm2),1));
+    if (((mtn1 >=  500 || mtn2 >=  500) && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1)) ||
+        ((mtn1 >= 1000 || mtn2 >= 1000) && (mtn1*3 < mtn2*2 || mtn2*3 < mtn1*2)) ||
+        ((mtn1 >= 2000 || mtn2 >= 2000) && (mtn1*5 < mtn2*4 || mtn2*5 < mtn1*4)) ||
+        ((mtn1 >= 4000 || mtn2 >= 4000) && c2 > c1))
+        ret = mtn1 > mtn2 ? match2 : match1;
+    else if (mr > 0.005 && FFMAX(mtn1, mtn2) > 150 && (mtn1*2 < mtn2*1 || mtn2*2 < mtn1*1))
+        ret = mtn1 > mtn2 ? match2 : match1;
+    else
+        ret = norm1 > norm2 ? match2 : match1;
+    return ret;
+}
+
+static void copy_fields(const FieldMatchContext *fm, AVFrame *dst,
+                        const AVFrame *src, int field)
+{
+    int plane;
+
+    for (plane = 0; plane < 4 && src->data[plane]; plane++) {
+        int i;
+        const int row_size = get_width (fm, src, plane);
+        const int height   = get_height(fm, src, plane);
+
+        uint8_t       *dstp = dst->data[plane] + field*dst->linesize[plane];
+        const uint8_t *srcp = src->data[plane] + field*src->linesize[plane];
+        const int dst_linesize2 = dst->linesize[plane] * 2;
+        const int src_linesize2 = src->linesize[plane] * 2;
+
+        for (i = 0; i < height; i += 2) {
+            memcpy(dstp, srcp, row_size);
+            dstp += dst_linesize2;
+            srcp += src_linesize2;
+        }
+    }
+}
+
+static AVFrame *create_weave_frame(AVFilterContext *ctx, int match, int field,
+                                   const AVFrame *prv, AVFrame *src, const AVFrame *nxt)
+{
+    AVFrame *dst;
+    FieldMatchContext *fm = ctx->priv;
+
+    if (match == 1) { // frame is considered progressive
+        dst = av_frame_clone(src);
+    } else {
+        AVFilterLink *outlink = ctx->outputs[0];
+
+        dst = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!dst)
+            return NULL;
+        av_frame_copy_props(dst, src);
+
+        switch (match) {
+        case 0: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, prv,   field); break;
+        case 2: copy_fields(fm, dst, src, 1-field); copy_fields(fm, dst, nxt,   field); break;
+        case 3: copy_fields(fm, dst, src,   field); copy_fields(fm, dst, prv, 1-field); break;
+        case 4: copy_fields(fm, dst, src,   field); copy_fields(fm, dst, nxt, 1-field); break;
+        default: av_assert0(0);
+        }
+    }
+    return dst;
+}
+
+enum {
+    mP, // match to previous field
+    mC, // match to current field
+    mN, // match to next field
+    mB, // match to previous field (matches from opposite parity flag)
+    mU, // match to next field     (matches from the opposite parity flag)
+};
+
+static int checkmm(AVFilterContext *ctx, int *mics, int m1, int m2,
+                   AVFrame **gen_frames, int field)
+{
+    const FieldMatchContext *fm = ctx->priv;
+
+#define LOAD_MIC(mid) do {                                                  \
+    if (mics[mid] < 0) {                                                    \
+        if (!gen_frames[mid])                                               \
+            gen_frames[mid] = create_weave_frame(ctx, mid, field,           \
+                                                 fm->prv, fm->src, fm->nxt);\
+        mics[mid] = calc_combed_score(fm, gen_frames[mid]);                  \
+    }                                                                       \
+} while (0)
+
+    LOAD_MIC(m1);
+    LOAD_MIC(m2);
+
+    if ((mics[m2] * 3 < mics[m1] || (mics[m2] * 2 < mics[m1] && mics[m1] > fm->mi)) &&
+        abs(mics[m2] - mics[m1]) >= 30 && mics[m2] < fm->mi)
+        return m2;
+    else
+        return m1;
+}
+
+static const int fxo0m[] = { 0, 1, 2, 3, 4 };
+static const int fxo1m[] = { 2, 1, 0, 4, 3 };
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx  = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    FieldMatchContext *fm = ctx->priv;
+    int mics[] = { -1, -1, -1, -1, -1 };
+    int order, field, i, match, sc = 0;
+    const int *fxo;
+    AVFrame *gen_frames[] = { NULL, NULL, NULL, NULL, NULL };
+    AVFrame *dst;
+
+#define SLIDING_FRAME_WINDOW(prv, src, nxt) do {                \
+        if (prv != src) /* 2nd loop exception (1st has prv==src and we don't want to loose src) */ \
+            av_frame_free(&prv);                                \
+        prv = src;                                              \
+        src = nxt;                                              \
+        if (in) /* TODO: request_frame() flushing */            \
+            nxt = in;                                           \
+        if (!prv)                                               \
+            prv = src;                                          \
+        if (!prv) /* received only one frame at that point */   \
+            return 0;                                           \
+        av_assert0(prv && src && nxt);                          \
+} while (0)
+
+    if (inlink == ctx->inputs[INPUT_MAIN]) {
+        SLIDING_FRAME_WINDOW(fm->prv, fm->src, fm->nxt);
+        fm->got_frame[INPUT_MAIN] = 1;
+    } else {
+        SLIDING_FRAME_WINDOW(fm->prv2, fm->src2, fm->nxt2);
+        fm->got_frame[INPUT_CLEANSRC] = 1;
+    }
+
+    if (!fm->got_frame[INPUT_MAIN] || (fm->ppsrc && !fm->got_frame[INPUT_CLEANSRC]))
+        return 0;
+
+    //av_log(0,0,"[prv: %p src: %p nxt: %p]\n", fm->prv, fm->src, fm->nxt);
+    //av_log(0,0,"[prv2:%p src2:%p nxt2:%p]\n", fm->prv2, fm->src2, fm->nxt2);
+
+    /* Parity */
+    order = fm->order != FM_PARITY_AUTO ? fm->order : (in->interlaced_frame ? in->top_field_first : 1);
+    field = fm->field != FM_PARITY_AUTO ? fm->field : order;
+    av_assert0(order == 0 || order == 1 || field == 0 || field == 1);
+    fxo = field ^ order ? fxo1m : fxo0m;
+
+    // check if it's a scenechange so micmatch can be used
+    // only relevant for mm mode 1
+    if (fm->micmatch == 1 && fm->prv != fm->src)
+        sc = luma_abs_diff(fm->prv, fm->src) > fm->scthresh;
+
+    if (fm->micout) {
+        /* debug mode: we generate all the fields combinations and their
+         * associated combed score */
+        for (i = 0; i < FF_ARRAY_ELEMS(mics); i++) {
+            gen_frames[i] = create_weave_frame(ctx, i, field, fm->prv, fm->src, fm->nxt);
+            if (!gen_frames[i])
+                return AVERROR(ENOMEM);
+            mics[i] = calc_combed_score(fm, gen_frames[i]);
+        }
+        av_log(ctx, AV_LOG_VERBOSE, "MICS: %d %d %d %d %d\n",
+               mics[0], mics[1], mics[2], mics[3], mics[4]);
+    } else {
+        gen_frames[mC] = av_frame_clone(fm->src);
+        if (!gen_frames[mC])
+            return AVERROR(ENOMEM);
+    }
+
+    // p/c selection
+    match = compare_fields(fm, fxo[mC], fxo[mP], field);
+    // the mode has 3-way p/c/n matches
+    if (fm->mode == MODE_PCN || fm->mode == MODE_PCN_UB)
+        match = compare_fields(fm, match, fxo[mN], field);
+
+    if (fm->micmatch == 2 || (sc && fm->micmatch == 1)) {
+        switch (fm->mode) {
+        /* 2-way p/c matches */
+        case MODE_PC:
+            match = checkmm(ctx, mics, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
+            break;
+        case MODE_PC_N:
+            match = checkmm(ctx, mics, match, fxo[mN], gen_frames, field);
+            break;
+        case MODE_PC_U:
+            match = checkmm(ctx, mics, match, fxo[mU], gen_frames, field);
+            break;
+        case MODE_PC_N_UB:
+            match = checkmm(ctx, mics, match, fxo[mN], gen_frames, field);
+            match = checkmm(ctx, mics, match, fxo[mU], gen_frames, field);
+            match = checkmm(ctx, mics, match, fxo[mB], gen_frames, field);
+            break;
+        /* 3-way p/c/n matches */
+        case MODE_PCN:
+            match = checkmm(ctx, mics, match, match == fxo[mP] ? fxo[mC] : fxo[mP], gen_frames, field);
+            break;
+        case MODE_PCN_UB:
+            match = checkmm(ctx, mics, match, fxo[mU], gen_frames, field);
+            match = checkmm(ctx, mics, match, fxo[mB], gen_frames, field);
+            break;
+        default:
+            av_assert0(0);
+        }
+    }
+
+    if (fm->ppsrc) {
+        /* field matching was based on a filtered/post-processed input, we now
+         * pick the untouched fields from the clean source */
+        dst = create_weave_frame(ctx, match, field, fm->prv2, fm->src2, fm->nxt2);
+    } else {
+        if (!gen_frames[match]) { // XXX: is that possible?
+            dst = create_weave_frame(ctx, match, field, fm->prv, fm->src, fm->nxt);
+        } else {
+            dst = gen_frames[match];
+            gen_frames[match] = NULL;
+        }
+    }
+    if (!dst)
+        return AVERROR(ENOMEM);
+
+    //av_log(0,0,"gen_frames: P:%p C:%p N:%p B:%p U:%p | match=%c\n",
+    //       gen_frames[mP], gen_frames[mC], gen_frames[mN], gen_frames[mB], gen_frames[mU],
+    //       mn[match]);
+
+    for (i = 0; i < FF_ARRAY_ELEMS(gen_frames); i++)
+        av_frame_free(&gen_frames[i]);
+
+    /* mark the frame we are unable to match properly as interlaced so a proper
+     * deinterlacer can take the relay */
+    dst->interlaced_frame = mics[match] >= fm->mi;
+    if (dst->interlaced_frame) {
+        av_log(ctx, AV_LOG_WARNING, "Frame #%"PRId64" at %s is still interlaced\n",
+               fm->frame_count, av_ts2timestr(in->pts, &inlink->time_base));
+        dst->top_field_first = field;
+    }
+    fm->frame_count++;
+
+    fm->got_frame[INPUT_MAIN] = fm->got_frame[INPUT_CLEANSRC] = 0;
+
+    return ff_filter_frame(outlink, dst);
+}
+
+static int request_frame(AVFilterLink *outlink)
+{
+    int ret;
+    AVFilterContext *ctx = outlink->src;
+    FieldMatchContext *fm = ctx->priv;
+
+    if (!fm->got_frame[INPUT_MAIN] &&
+        (ret = ff_request_frame(ctx->inputs[INPUT_MAIN])) < 0)
+        return ret;
+    if (fm->ppsrc && !fm->got_frame[INPUT_CLEANSRC] &&
+        (ret = ff_request_frame(ctx->inputs[INPUT_CLEANSRC])) < 0)
+        return ret;
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_YUV420P,
+        AV_PIX_FMT_YUV422P,
+        AV_PIX_FMT_NONE
+    };
+
+    ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
+    return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    int ret;
+    AVFilterContext *ctx = inlink->dst;
+    FieldMatchContext *fm = ctx->priv;
+    const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
+    const int w = inlink->w;
+    const int h = inlink->h;
+
+    fm->scthresh = (int64_t)((w * h * 255.0 * fm->scthresh) / 100.0);
+
+    if ((ret = av_image_alloc(fm->map_data,   fm->map_linesize,   w, h, inlink->format, 32)) < 0 ||
+        (ret = av_image_alloc(fm->cmask_data, fm->cmask_linesize, w, h, inlink->format, 32)) < 0)
+        return ret;
+
+    fm->hsub = pix_desc->log2_chroma_w;
+    fm->vsub = pix_desc->log2_chroma_h;
+
+    fm->tpitchy  = FFALIGN(w,      16);
+    fm->tpitchuv = FFALIGN(w >> 1, 16);
+
+    fm->tbuffer = av_malloc(h/2 * fm->tpitchy);
+    fm->c_array = av_malloc((((w + fm->blockx/2)/fm->blockx)+1) *
+                            (((h + fm->blocky/2)/fm->blocky)+1) *
+                            4 * sizeof(*fm->c_array));
+    if (!fm->tbuffer || !fm->c_array)
+        return AVERROR(ENOMEM);
+
+    return 0;
+}
+
+static av_cold int fieldmatch_init(AVFilterContext *ctx, const char *args)
+{
+    const FieldMatchContext *fm = ctx->priv;
+    AVFilterPad pad = {
+        .name         = av_strdup("main"),
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .filter_frame = filter_frame,
+        .config_props = config_input,
+    };
+
+    if (!pad.name)
+        return AVERROR(ENOMEM);
+    ff_insert_inpad(ctx, INPUT_MAIN, &pad);
+
+    if (fm->ppsrc) {
+        pad.name = av_strdup("clean_src");
+        pad.config_props = NULL;
+        if (!pad.name)
+            return AVERROR(ENOMEM);
+        ff_insert_inpad(ctx, INPUT_CLEANSRC, &pad);
+    }
+
+    if ((fm->blockx & (fm->blockx - 1)) ||
+        (fm->blocky & (fm->blocky - 1))) {
+        av_log(ctx, AV_LOG_ERROR, "blockx and blocky settings must be power of two\n");
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static av_cold void fieldmatch_uninit(AVFilterContext *ctx)
+{
+    FieldMatchContext *fm = ctx->priv;
+
+    av_freep(&fm->map_data[0]);
+    av_freep(&fm->cmask_data[0]);
+    av_freep(&fm->tbuffer);
+    av_freep(&fm->c_array);
+}
+
+// TODO: check that both sources are close enough
+static int config_output(AVFilterLink *outlink) { return 0; }
+
+static const AVFilterPad fieldmatch_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .request_frame = request_frame,
+        .config_props  = config_output,
+    },
+    { NULL }
+};
+
+static const char *const fieldmatch_shorthand[] = { "order", "mode", "ppsrc", NULL };
+
+AVFilter avfilter_vf_fieldmatch = {
+    .name           = "fieldmatch",
+    .description    = NULL_IF_CONFIG_SMALL("Field matching for inverse telecine"),
+    .query_formats  = query_formats,
+    .priv_size      = sizeof(FieldMatchContext),
+    .init           = fieldmatch_init,
+    .uninit         = fieldmatch_uninit,
+    .inputs         = NULL,
+    .outputs        = fieldmatch_outputs,
+    .priv_class     = &fieldmatch_class,
+    .shorthand      = fieldmatch_shorthand,
+};
-- 
1.8.2



More information about the ffmpeg-devel mailing list