[FFmpeg-devel] [PATCH] add phqm filter and img_hash

ckennedy at ellation.com ckennedy at ellation.com
Fri Oct 25 03:23:48 EEST 2019


From: Christopher Kennedy <ckennedy at ellation.com>

this adds a phqm filter and OpenCV img_hash based resource usable
by the phqm and future filters using image hash functionality
from OpenCV.

C++ to C handling so that full OpenCV functionality and API can
be used instead of the C versions (which are incomplete and
don't always exist).

Example command line:

ffmpeg -i encode.mp4 -i reference.mp4 \
           -filter_complex "[0:v][1:v]phqm=stats_file=out.log" \
           -an -codec:v rawvideo -y -f null /dev/null

Signed-off-by: Christopher Kennedy <ckennedy at ellation.com>
---
 Changelog                |   1 +
 configure                |   2 +
 libavfilter/Makefile     |   2 +
 libavfilter/allfilters.c |   1 +
 libavfilter/img_hash.cpp | 104 ++++++++++++
 libavfilter/img_hash.h   |  46 +++++
 libavfilter/vf_phqm.c    | 356 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 512 insertions(+)
 create mode 100644 libavfilter/img_hash.cpp
 create mode 100644 libavfilter/img_hash.h
 create mode 100644 libavfilter/vf_phqm.c

diff --git a/Changelog b/Changelog
index 316589e336..4a22f77d37 100644
--- a/Changelog
+++ b/Changelog
@@ -17,6 +17,7 @@ version <next>:
 - anlms filter
 - arnndn filter
 - bilateral filter
+- phqm perceptual hash filter using OpenCV img_lib
 
 
 version 4.2:
diff --git a/configure b/configure
index 8413826f9e..e231d359bb 100755
--- a/configure
+++ b/configure
@@ -3497,6 +3497,8 @@ nlmeans_opencl_filter_deps="opencl"
 nnedi_filter_deps="gpl"
 ocr_filter_deps="libtesseract"
 ocv_filter_deps="libopencv"
+phqm_filter_deps="libopencv"
+phqm_filter_extralibs="-lstdc++ -lopencv_img_hash"
 openclsrc_filter_deps="opencl"
 overlay_opencl_filter_deps="opencl"
 overlay_qsv_filter_deps="libmfx"
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 63d2fba861..3000e5d490 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -312,6 +312,7 @@ OBJS-$(CONFIG_NORMALIZE_FILTER)              += vf_normalize.o
 OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
 OBJS-$(CONFIG_OCR_FILTER)                    += vf_ocr.o
 OBJS-$(CONFIG_OCV_FILTER)                    += vf_libopencv.o
+OBJS-$(CONFIG_PHQM_FILTER)               += vf_phqm.o img_hash.o
 OBJS-$(CONFIG_OSCILLOSCOPE_FILTER)           += vf_datascope.o
 OBJS-$(CONFIG_OVERLAY_FILTER)                += vf_overlay.o framesync.o
 OBJS-$(CONFIG_OVERLAY_OPENCL_FILTER)         += vf_overlay_opencl.o opencl.o \
@@ -498,6 +499,7 @@ OBJS-$(CONFIG_SHARED)                        += log2_tab.o
 SKIPHEADERS-$(CONFIG_QSVVPP)                 += qsvvpp.h
 SKIPHEADERS-$(CONFIG_OPENCL)                 += opencl.h
 SKIPHEADERS-$(CONFIG_VAAPI)                  += vaapi_vpp.h
+SKIPHEADERS-$(CONFIG_LIBOPENCV)              += img_hash.h
 
 TOOLS     = graph2dot
 TESTPROGS = drawutils filtfmts formats integral
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index e4186f93db..cd94a0c727 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -297,6 +297,7 @@ extern AVFilter ff_vf_normalize;
 extern AVFilter ff_vf_null;
 extern AVFilter ff_vf_ocr;
 extern AVFilter ff_vf_ocv;
+extern AVFilter ff_vf_phqm;
 extern AVFilter ff_vf_oscilloscope;
 extern AVFilter ff_vf_overlay;
 extern AVFilter ff_vf_overlay_opencl;
diff --git a/libavfilter/img_hash.cpp b/libavfilter/img_hash.cpp
new file mode 100644
index 0000000000..e957a65ab0
--- /dev/null
+++ b/libavfilter/img_hash.cpp
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2019 Christopher Kennedy
+ *
+ * OpenCV img_hash
+ *
+ * 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 <opencv2/core.hpp>
+#include <opencv2/core/ocl.hpp>
+#include <opencv2/highgui.hpp>
+#include <opencv2/img_hash.hpp>
+#include <opencv2/imgproc.hpp>
+
+#include <iostream>
+
+#include "img_hash.h"
+#include "libavutil/pixdesc.h"
+extern "C" {
+#include "avfilter.h"
+}
+
+using namespace cv;
+using namespace cv::img_hash;
+using namespace std;
+
+// convert from avframe to iplimage format
+static void fill_iplimage_from_frame(IplImage *img, const AVFrame *frame, enum AVPixelFormat pixfmt)
+{
+    IplImage *tmpimg;
+    int depth, channels_nb;
+
+    if      (pixfmt == AV_PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U;  channels_nb = 1; }
+    else if (pixfmt == AV_PIX_FMT_BGRA)  { depth = IPL_DEPTH_8U;  channels_nb = 4; }
+    else if (pixfmt == AV_PIX_FMT_BGR24) { depth = IPL_DEPTH_8U;  channels_nb = 3; }
+    else if (pixfmt == AV_PIX_FMT_YUV420P) { depth = IPL_DEPTH_8U;  channels_nb = 3; }
+    else return;
+
+    tmpimg = cvCreateImageHeader((CvSize){frame->width, frame->height}, depth, channels_nb);
+    *img = *tmpimg;
+    img->imageData = img->imageDataOrigin = (char *) frame->data[0];
+    img->dataOrder = IPL_DATA_ORDER_PIXEL;
+    img->origin    = IPL_ORIGIN_TL;
+    img->widthStep = frame->linesize[0];
+}
+
+// Get the score of two Video Frames by comparing the perceptual hashes and deriving a hamming distance
+// showing how similar they are or different. lower the score is better for most algorithms
+extern "C" double getScore(const AVFrame *frame1, const AVFrame *frame2, enum AVPixelFormat pixfmt, int hash_type) {
+    cv::Ptr<cv::img_hash::ImgHashBase> algo;
+    IplImage ipl1, ipl2;
+    cv::Mat h1;
+    cv::Mat h2;
+    cv::Mat m1;
+    cv::Mat m2;
+
+    // Take FFmpeg video frame and convert into an IplImage for OpenCV
+    fill_iplimage_from_frame(&ipl1, frame1, pixfmt);
+    fill_iplimage_from_frame(&ipl2, frame2, pixfmt);
+
+    // Convert an IplImage to an Mat Image for OpenCV (newer format)
+    m1 = cv::cvarrToMat(&ipl1);
+    m2 = cv::cvarrToMat(&ipl2);
+
+    // substantiate the hash type algorithm
+    if (hash_type == COLORMOMENT) {
+        algo = cv::img_hash::ColorMomentHash::create();
+    } else if (hash_type == AVERAGE) {
+        algo = cv::img_hash::AverageHash::create();
+    } else if (hash_type == BLOCKMEAN1) {
+        //BlockMeanHash support mode 0 and mode 1, they associate to
+        //    //mode 1 and mode 2 of PHash library
+        algo = cv::img_hash::BlockMeanHash::create(0);
+    } else if (hash_type == BLOCKMEAN2) {
+        algo = cv::img_hash::BlockMeanHash::create(1);
+    } else if (hash_type == MARRHILDRETH) {
+        algo = cv::img_hash::MarrHildrethHash::create();
+    } else if (hash_type == RADIALVARIANCE) {
+        algo = cv::img_hash::RadialVarianceHash::create();
+    } else { // Default to PHash
+        algo = cv::img_hash::PHash::create();
+    }
+
+    // Compute the hash
+    algo->compute(m1, h1);
+    algo->compute(m2, h2);
+
+    // Compare the hashes and return the hamming distance
+    return algo->compare(h1, h2);
+}
diff --git a/libavfilter/img_hash.h b/libavfilter/img_hash.h
new file mode 100644
index 0000000000..40494536bd
--- /dev/null
+++ b/libavfilter/img_hash.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2019 Christopher Kennedy
+ *
+ * PHQM Perceptual Hash Quality Metric
+ *
+ * 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_IMG_HASH_H
+#define AVFILTER_IMG_HASH_H
+
+#include "avfilter.h"
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+#define PHASH 0
+#define COLORMOMENT 1
+#define AVERAGE 2
+#define MARRHILDRETH 3
+#define RADIALVARIANCE 4
+#define BLOCKMEAN1 5
+#define BLOCKMEAN2 6
+
+double getScore(const AVFrame *frame1, const AVFrame *frame2, enum AVPixelFormat pixfmt, int hash_type);
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
diff --git a/libavfilter/vf_phqm.c b/libavfilter/vf_phqm.c
new file mode 100644
index 0000000000..4dc76e996a
--- /dev/null
+++ b/libavfilter/vf_phqm.c
@@ -0,0 +1,356 @@
+/*
+ * Copyright (c) 2019 Christopher Kennedy
+ *
+ * PHQM Perceptual Hash Quality Metric
+ *
+ * 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
+ * PHQM: Caculate the Image Hash Hamming Difference between two input videos.
+ */
+
+#include "libavutil/avstring.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "drawutils.h"
+#include "formats.h"
+#include "framesync.h"
+#include "internal.h"
+#include "video.h"
+
+#include "img_hash.h"
+#include "scene_sad.h"
+
+typedef struct PHQMContext {
+    const AVClass *class;
+    FFFrameSync fs;
+    double shd, hd, min_hd, max_hd, smin_hd, smax_hd;
+    uint64_t nb_shd;
+    uint64_t nb_frames;
+    FILE *stats_file;
+    char *stats_file_str;
+    int stats_version;
+    char *hash_type;
+    int hash_type_i;
+    int stats_header_written;
+    ff_scene_sad_fn sad;            ///< Sum of the absolute difference function (scene detect only)
+    double prev_mafd;               ///< previous MAFD                           (scene detect only)
+    AVFrame *prev_picref;           ///< previous frame                          (scene detect only)
+    double scd_thresh;
+    double scene_score;
+} PHQMContext;
+
+#define OFFSET(x) offsetof(PHQMContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption phqm_options[] = {
+    {"stats_file", "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
+    {"f",          "Set file where to store per-frame difference information", OFFSET(stats_file_str), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS },
+    {"stats_version", "Set the format version for the stats file.",               OFFSET(stats_version),  AV_OPT_TYPE_INT,    {.i64=1},    1, 2, FLAGS },
+    {"scd_thresh", "Scene Change Detection Threshold. 0.5 default, 0.0-1.0",    OFFSET(scd_thresh),  AV_OPT_TYPE_DOUBLE,    {.dbl=0.5},    0, 1, FLAGS },
+    { "hash_type", "options: phash, colormoment, average", OFFSET(hash_type), AV_OPT_TYPE_STRING, {.str = "phash"}, .flags = FLAGS },
+    { NULL }
+};
+
+FRAMESYNC_DEFINE_CLASS(phqm, PHQMContext, fs);
+
+static void set_meta(AVDictionary **metadata, const char *key, char comp, float d)
+{
+    char value[128];
+    snprintf(value, sizeof(value), "%0.2f", d);
+    if (comp) {
+        char key2[128];
+        snprintf(key2, sizeof(key2), "%s%c", key, comp);
+        av_dict_set(metadata, key2, value, 0);
+    } else {
+        av_dict_set(metadata, key, value, 0);
+    }
+}
+
+static double get_scene_score(AVFilterContext *ctx, AVFrame *frame)
+{
+    double ret = 0;
+    PHQMContext *s = ctx->priv;
+    AVFrame *prev_picref = s->prev_picref;
+
+    if (prev_picref &&
+        frame->height == prev_picref->height &&
+        frame->width  == prev_picref->width) {
+        uint64_t sad;
+        double mafd, diff;
+
+        s->sad(prev_picref->data[0], prev_picref->linesize[0], frame->data[0], frame->linesize[0], frame->width * 3, frame->height, &sad);
+        emms_c();
+        mafd = (double)sad / (frame->width * 3 * frame->height);
+        diff = fabs(mafd - s->prev_mafd);
+        ret  = av_clipf(FFMIN(mafd, diff) / 100., 0, 1);
+        s->prev_mafd = mafd;
+        av_frame_free(&prev_picref);
+    }
+    s->prev_picref = av_frame_clone(frame);
+    return ret;
+}
+
+static int do_phqm(FFFrameSync *fs)
+{
+    AVFilterContext *ctx = fs->parent;
+    PHQMContext *s = ctx->priv;
+    AVFrame *master, *ref;
+    double comp_mse[4], mse = 0, hd = 0;
+    int ret, j, c;
+    double hd_limit = 1000000;
+    AVDictionary **metadata;
+
+    ret = ff_framesync_dualinput_get(fs, &master, &ref);
+    if (ret < 0)
+        return ret;
+    if (!ref)
+        return ff_filter_frame(ctx->outputs[0], master);
+    metadata = &master->metadata;
+
+
+    s->nb_frames++;
+
+    /* scene change detection score */
+    s->scene_score = get_scene_score(ctx, ref);
+    if (s->scene_score >= s->scd_thresh && s->nb_shd >= 48) {
+        av_log(s, AV_LOG_WARNING, "ImgHashScene: n:%"PRId64"-%"PRId64" hd_avg:%0.3lf hd_min:%0.3lf hd_max:%0.3lf scd:%0.2lf\n",
+               (s->nb_frames - s->nb_shd), s->nb_frames - 1, (s->shd / s->nb_shd), s->smin_hd, s->smax_hd, s->scene_score);
+        s->shd = 0;
+        s->nb_shd = 0;
+        s->smin_hd = 0;
+        s->smax_hd = 0;
+    }
+
+    /* limit the highest value so we cut off at perceptual difference match */
+    if (s->hash_type_i == PHASH || s->hash_type_i == AVERAGE)
+        hd_limit = 5;
+    else if (s->hash_type_i == MARRHILDRETH)
+        hd_limit = 30;
+    else if (s->hash_type_i == RADIALVARIANCE)
+        hd_limit = 0.9;
+    else if (s->hash_type_i == BLOCKMEAN1)
+        hd_limit = 12;
+    else if (s->hash_type_i == BLOCKMEAN2)
+        hd_limit = 48;
+    else if (s->hash_type_i == COLORMOMENT)
+        hd_limit = 8;
+
+    /* get ref / enc perceptual hashes and calc hamming distance difference value */
+    hd = getScore(ref, master, ref->format, s->hash_type_i);
+    s->hd += FFMIN(hd, hd_limit);
+    set_meta(metadata, "lavfi.phqm.phqm", 0, hd);
+
+    /* scene hamming distance avg */
+    s->shd += FFMIN(hd, hd_limit);
+    s->nb_shd++;
+    av_log(s, AV_LOG_DEBUG, "ImgHashFrame: hd:%0.3lf scd:%0.2lf\n", hd, s->scene_score);
+
+    s->min_hd = FFMIN(s->min_hd, hd);
+    s->max_hd = FFMAX(s->max_hd, hd);
+    s->smin_hd = FFMIN(s->smin_hd, hd);
+    s->smax_hd = FFMAX(s->smax_hd, hd);
+
+    if (s->stats_file) {
+        if (s->stats_version == 2 && !s->stats_header_written) {
+            fprintf(s->stats_file, "phqm_log_version:2 fields:n");
+            fprintf(s->stats_file, ",phqm");
+            fprintf(s->stats_file, "\n");
+            s->stats_header_written = 1;
+        }
+        fprintf(s->stats_file,
+                "n:%"PRId64" phqm:%0.3f phqm_min:%0.3f phqm_max:%0.3f sad:%0.2f ",
+                s->nb_frames, hd, s->min_hd, s->max_hd, s->scene_score);
+        fprintf(s->stats_file, "\n");
+    }
+
+    return ff_filter_frame(ctx->outputs[0], master);
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    PHQMContext *s = ctx->priv;
+
+    if (s->stats_file_str) {
+        if (!strcmp(s->stats_file_str, "-")) {
+            s->stats_file = stdout;
+        } else {
+            s->stats_file = fopen(s->stats_file_str, "w");
+            if (!s->stats_file) {
+                int err = AVERROR(errno);
+                char buf[128];
+                av_strerror(err, buf, sizeof(buf));
+                av_log(ctx, AV_LOG_ERROR, "Could not open stats file %s: %s\n",
+                       s->stats_file_str, buf);
+                return err;
+            }
+        }
+    }
+
+    if (s->hash_type) {
+        if (!strcmp(s->hash_type, "phash"))
+            s->hash_type_i = PHASH;
+        else if (!strcmp(s->hash_type, "colormoment")) {
+            s->hash_type_i = COLORMOMENT;
+        } else if (!strcmp(s->hash_type, "average"))
+            s->hash_type_i = AVERAGE;
+        else if (!strcmp(s->hash_type, "marrhildreth"))
+            s->hash_type_i = MARRHILDRETH;
+        else if (!strcmp(s->hash_type, "radialvariance"))
+            s->hash_type_i = RADIALVARIANCE;
+        else if (!strcmp(s->hash_type, "blockmean1"))
+            s->hash_type_i = BLOCKMEAN1;
+        else if (!strcmp(s->hash_type, "blockmean2"))
+            s->hash_type_i = BLOCKMEAN2;
+        else {
+            av_log(s, AV_LOG_ERROR, "Bad hash_type given %s\n", s->hash_type);
+            return AVERROR(EINVAL);
+        }
+    }
+
+    s->sad = ff_scene_sad_get_fn(8);
+    if (!s->sad)
+        return AVERROR(EINVAL);
+
+    s->fs.on_event = do_phqm;
+    return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pix_fmts[] = {
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
+#define PF_NOALPHA(suf) AV_PIX_FMT_YUV420##suf,  AV_PIX_FMT_YUV422##suf,  AV_PIX_FMT_YUV444##suf
+#define PF_ALPHA(suf)   AV_PIX_FMT_YUVA420##suf, AV_PIX_FMT_YUVA422##suf, AV_PIX_FMT_YUVA444##suf
+#define PF(suf)         PF_NOALPHA(suf), PF_ALPHA(suf)
+        PF(P), PF(P9), PF(P10), PF_NOALPHA(P12), PF_NOALPHA(P14), PF(P16),
+        AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
+        AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
+        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_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_ref(AVFilterLink *inlink)
+{
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    AVFilterContext *ctx  = inlink->dst;
+    PHQMContext *s = ctx->priv;
+    double average_max;
+    unsigned sum;
+    int j;
+
+    if (ctx->inputs[0]->w != ctx->inputs[1]->w ||
+        ctx->inputs[0]->h != ctx->inputs[1]->h) {
+        av_log(ctx, AV_LOG_ERROR, "Width and height of input videos must be same.\n");
+        return AVERROR(EINVAL);
+    }
+    if (ctx->inputs[0]->format != ctx->inputs[1]->format) {
+        av_log(ctx, AV_LOG_ERROR, "Inputs must be of same pixel format.\n");
+        return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
+static int config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    PHQMContext *s = ctx->priv;
+    AVFilterLink *mainlink = ctx->inputs[0];
+    int ret;
+
+    ret = ff_framesync_init_dualinput(&s->fs, ctx);
+    if (ret < 0)
+        return ret;
+    outlink->w = mainlink->w;
+    outlink->h = mainlink->h;
+    outlink->time_base = mainlink->time_base;
+    outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
+    outlink->frame_rate = mainlink->frame_rate;
+    if ((ret = ff_framesync_configure(&s->fs)) < 0)
+        return ret;
+
+    return 0;
+}
+
+static int activate(AVFilterContext *ctx)
+{
+    PHQMContext *s = ctx->priv;
+    return ff_framesync_activate(&s->fs);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    PHQMContext *s = ctx->priv;
+
+    if (s->nb_frames > 0)
+        av_log(ctx, AV_LOG_WARNING, "PHQM average:%f min:%f max:%f\n",
+               s->hd / s->nb_frames, s->min_hd, s->max_hd);
+
+    ff_framesync_uninit(&s->fs);
+
+    if (s->stats_file && s->stats_file != stdout)
+        fclose(s->stats_file);
+    av_frame_free(&s->prev_picref);
+}
+
+static const AVFilterPad phqm_inputs[] = {
+    {
+        .name         = "main",
+        .type         = AVMEDIA_TYPE_VIDEO,
+    },{
+        .name         = "reference",
+        .type         = AVMEDIA_TYPE_VIDEO,
+        .config_props = config_input_ref,
+    },
+    { NULL }
+};
+
+static const AVFilterPad phqm_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_output,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_phqm= {
+    .name          = "phqm",
+    .description   = NULL_IF_CONFIG_SMALL("PHQM: Calculate the Perceptual Hash Hamming Difference between two video streams."),
+    .preinit       = phqm_framesync_preinit,
+    .init          = init,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .activate      = activate,
+    .priv_size     = sizeof(PHQMContext),
+    .priv_class    = &phqm_class,
+    .inputs        = phqm_inputs,
+    .outputs       = phqm_outputs,
+};
-- 
2.20.1



More information about the ffmpeg-devel mailing list