[FFmpeg-devel] [PATCH] add phqm filter and img_hash
From: Christopher Kennedy 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 --- 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 : - 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 00..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 +#include +#include +#include +#include + +#include + +#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 =
[FFmpeg-devel] [PATCH] add phqm filter and img_hash
From: Christopher Kennedy 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" \ -y -f null /dev/null Signed-off-by: Christopher Kennedy --- Changelog| 1 + configure| 2 + libavfilter/Makefile | 2 + libavfilter/allfilters.c | 1 + libavfilter/img_hash.cpp | 99 libavfilter/img_hash.h | 46 ++ libavfilter/vf_phqm.c| 334 +++ 7 files changed, 485 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 : - 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..645e232b3e 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -325,6 +325,7 @@ OBJS-$(CONFIG_PERMS_FILTER) += f_perms.o OBJS-$(CONFIG_PERSPECTIVE_FILTER)+= vf_perspective.o OBJS-$(CONFIG_PHASE_FILTER) += vf_phase.o OBJS-$(CONFIG_PHOTOSENSITIVITY_FILTER) += vf_photosensitivity.o +OBJS-$(CONFIG_PHQM_FILTER) += vf_phqm.o img_hash.o OBJS-$(CONFIG_PIXDESCTEST_FILTER)+= vf_pixdesctest.o OBJS-$(CONFIG_PIXSCOPE_FILTER) += vf_datascope.o OBJS-$(CONFIG_PP_FILTER) += vf_pp.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..f0fcaad235 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -309,6 +309,7 @@ extern AVFilter ff_vf_perms; extern AVFilter ff_vf_perspective; extern AVFilter ff_vf_phase; extern AVFilter ff_vf_photosensitivity; +extern AVFilter ff_vf_phqm; extern AVFilter ff_vf_pixdesctest; extern AVFilter ff_vf_pixscope; extern AVFilter ff_vf_pp; diff --git a/libavfilter/img_hash.cpp b/libavfilter/img_hash.cpp new file mode 100644 index 00..35dca0ca6a --- /dev/null +++ b/libavfilter/img_hash.cpp @@ -0,0 +1,99 @@ +/* + * 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 +#include +#include +#include +#include + +#include + +#include "img_hash.h" +#include "libavutil/pixdesc.h" +extern "C" { +#include "avfilter.h" +} + +// convert from avframe to iplimage format +static int fill_iplimage_from_frame(IplImage *img, const AVFrame *frame, enum AVPixelFormat pixfmt) +{ +IplImage *tmpimg; +int depth = IPL_DEPTH_8U, channels_nb; + +switch (pixfmt) { +case AV_PIX_FMT_GRAY8: channels_nb = 1; break; +case AV_PIX_FMT_BGRA: channels_nb = 4; break; +case AV_PIX_FMT_BGR24: channels_nb = 3; break; +case AV_PIX_FMT_YUV420P:channels_nb = 3; break; +default: return -1; +} + +tmpimg = cvCreateImageHeader((CvSize){frame->width, frame->height}, depth, channels_nb); +*img
[FFmpeg-devel] [PATCH] add phqm filter and img_hash
From: Christopher Kennedy 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" \ -y -f null /dev/null Signed-off-by: Christopher Kennedy --- Changelog| 1 + configure| 2 + libavfilter/Makefile | 2 + libavfilter/allfilters.c | 1 + libavfilter/img_hash.cpp | 98 libavfilter/img_hash.h | 46 ++ libavfilter/vf_phqm.c| 334 +++ 7 files changed, 484 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 : - 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..645e232b3e 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -325,6 +325,7 @@ OBJS-$(CONFIG_PERMS_FILTER) += f_perms.o OBJS-$(CONFIG_PERSPECTIVE_FILTER)+= vf_perspective.o OBJS-$(CONFIG_PHASE_FILTER) += vf_phase.o OBJS-$(CONFIG_PHOTOSENSITIVITY_FILTER) += vf_photosensitivity.o +OBJS-$(CONFIG_PHQM_FILTER) += vf_phqm.o img_hash.o OBJS-$(CONFIG_PIXDESCTEST_FILTER)+= vf_pixdesctest.o OBJS-$(CONFIG_PIXSCOPE_FILTER) += vf_datascope.o OBJS-$(CONFIG_PP_FILTER) += vf_pp.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..f0fcaad235 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -309,6 +309,7 @@ extern AVFilter ff_vf_perms; extern AVFilter ff_vf_perspective; extern AVFilter ff_vf_phase; extern AVFilter ff_vf_photosensitivity; +extern AVFilter ff_vf_phqm; extern AVFilter ff_vf_pixdesctest; extern AVFilter ff_vf_pixscope; extern AVFilter ff_vf_pp; diff --git a/libavfilter/img_hash.cpp b/libavfilter/img_hash.cpp new file mode 100644 index 00..4d5843da22 --- /dev/null +++ b/libavfilter/img_hash.cpp @@ -0,0 +1,98 @@ +/* + * 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 +#include +#include +#include +#include + +#include + +#include "img_hash.h" +#include "libavutil/pixdesc.h" +extern "C" { +#include "avfilter.h" +} + +// convert from avframe to iplimage format +static int fill_iplimage_from_frame(IplImage *img, const AVFrame *frame, enum AVPixelFormat pixfmt) +{ +IplImage *tmpimg; +int depth = IPL_DEPTH_8U, channels_nb; + +switch (pixfmt) { +case AV_PIX_FMT_GRAY8: channels_nb = 1; break; +case AV_PIX_FMT_BGRA: channels_nb = 4; break; +case AV_PIX_FMT_BGR24: channels_nb = 3; break; +default: return -1; +} + +tmpimg = cvCreateImageHeader((CvSize){frame->width, frame->height}, depth, channels_nb); +*img = *tmpimg; +img->imageData = img->imageDataOrigin = (cha