From patchwork Wed Mar 3 18:43:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 26077 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 06B1A44BE73 for ; Wed, 3 Mar 2021 20:43:35 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CEBF368A865; Wed, 3 Mar 2021 20:43:34 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7EB3068A700 for ; Wed, 3 Mar 2021 20:43:28 +0200 (EET) Received: by mail-ej1-f43.google.com with SMTP id do6so44272718ejc.3 for ; Wed, 03 Mar 2021 10:43:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=dGA+gy3CN82eAA0VSVKiJUGe8+ah7reluSB4xs6SuCU=; b=gfh62Fmu8YuDfsMXz70EV/od6fD7gMuSNSDvmegvlyRyIB1oUpz4WmCCPPaem4/IEL emyjW89JzaFLzb152n2AgLZKXf9ai+6FcVpRuRWqcy4fN37Dx3aorlVEC9U1b+eTeK4G +8GWUKTiM1OiXQzjsjrL9xlSQeQl1LMWdUFlcdZg6qeum87UX44hmF4Hblvqyt3m/ljB SPK8n1/exFsy8SJ1n68DQb96cWvZBpadI9meqkb70xa3HpJqB+9VKaDYkWiXIbdvifbR eCz+7ASlJNgEhBeWvyzOM2yToohSXIm1fYVg5fCR8pQIAqlwiV/9iMdRCwpB/6kBfBjc eeoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=dGA+gy3CN82eAA0VSVKiJUGe8+ah7reluSB4xs6SuCU=; b=EoKOoskNnCdBQeC8Y9u7eAC1ElKKOLsrxc5I70Ita7xj1/qJL1ILIn3CooP3d9hWNs m0cr0iHkXdq+DI1z4RfTAvss5kohIEdJ0YMmlvPjfitR7PRrlAIZ6Pt0Xgd3bd2zmXqz E1oofUsSp8AJ1NEAzHYr9SK20n44Tt3CoocD9zwiVJd2AncvC9jRO0xBwzc2AWqfcxz5 dAd/2eAqX2eELPe1aMydbzsOARG4pqm8JVdEYi7c+AMJIAajR0jg9cyvJd/3WfresgH0 V86D+RaCeM2kPVBZO6UIzhD1guKJP2vabBDKN2yqRfyNgQ9hOGkh+YNts7eYQIoa8wyP lQzg== X-Gm-Message-State: AOAM530HXYGtB/EQY7rKra+94UTK3UBvGzhuOyy9p5P7gPseEj5BEfmO 07VK4mfO3arnk0nD8Bsc49Xju0mRq2g= X-Google-Smtp-Source: ABdhPJyXI+/vHeXI4uiIrXa+e7BJ9yieR/OI7rd9DVdgkEDkDnV/Zx/AFpXxBfBL1uoI+kM8I+zPWQ== X-Received: by 2002:a17:906:3a13:: with SMTP id z19mr183159eje.317.1614797007953; Wed, 03 Mar 2021 10:43:27 -0800 (PST) Received: from localhost.localdomain ([94.250.185.123]) by smtp.gmail.com with ESMTPSA id v13sm24298271ede.5.2021.03.03.10.43.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Mar 2021 10:43:27 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Wed, 3 Mar 2021 19:43:16 +0100 Message-Id: <20210303184317.8809-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/2] avfilter: add identity video filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- doc/filters.texi | 22 +++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_identity.c | 372 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 396 insertions(+) create mode 100644 libavfilter/vf_identity.c diff --git a/doc/filters.texi b/doc/filters.texi index 426cb158da..38698b6542 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -13382,6 +13382,28 @@ By default value is 0. The @code{hysteresis} filter also supports the @ref{framesync} options. +@section identity + +Obtain the identity score between two input videos. + +This filter takes two input videos. + +Both input videos must have the same resolution and pixel format for +this filter to work correctly. Also it assumes that both inputs +have the same number of frames, which are compared one by one. + +The obtained per component, average, min and max identity score is printed through +the logging system. + +The filter stores the calculated identity scores of each frame in frame metadata. + +In the below example the input file @file{main.mpg} being processed is compared +with the reference file @file{ref.mpg}. + +@example +ffmpeg -i main.mpg -i ref.mpg -lavfi identity -f null - +@end example + @section idet Detect video interlacing type. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index fba103ff80..346ed6f15c 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -299,6 +299,7 @@ OBJS-$(CONFIG_HWMAP_FILTER) += vf_hwmap.o OBJS-$(CONFIG_HWUPLOAD_CUDA_FILTER) += vf_hwupload_cuda.o OBJS-$(CONFIG_HWUPLOAD_FILTER) += vf_hwupload.o OBJS-$(CONFIG_HYSTERESIS_FILTER) += vf_hysteresis.o framesync.o +OBJS-$(CONFIG_IDENTITY_FILTER) += vf_identity.o OBJS-$(CONFIG_IDET_FILTER) += vf_idet.o OBJS-$(CONFIG_IL_FILTER) += vf_il.o OBJS-$(CONFIG_INFLATE_FILTER) += vf_neighbor.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index d2d543f917..4bd8eabd28 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -283,6 +283,7 @@ extern AVFilter ff_vf_hwmap; extern AVFilter ff_vf_hwupload; extern AVFilter ff_vf_hwupload_cuda; extern AVFilter ff_vf_hysteresis; +extern AVFilter ff_vf_identity; extern AVFilter ff_vf_idet; extern AVFilter ff_vf_il; extern AVFilter ff_vf_inflate; diff --git a/libavfilter/vf_identity.c b/libavfilter/vf_identity.c new file mode 100644 index 0000000000..02dda392b3 --- /dev/null +++ b/libavfilter/vf_identity.c @@ -0,0 +1,372 @@ +/* + * 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 + */ + +/** + * @file + * Caculate the Identity 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" + +typedef struct IdentityContext { + const AVClass *class; + FFFrameSync fs; + double score, min_score, max_score, score_comp[4]; + uint64_t nb_frames; + int is_rgb; + uint8_t rgba_map[4]; + char comps[4]; + int nb_components; + int nb_threads; + int planewidth[4]; + int planeheight[4]; + uint64_t **scores; + unsigned (*filter_line)(const uint8_t *buf, const uint8_t *ref, int w); +} IdentityContext; + +#define OFFSET(x) offsetof(IdentityContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption identity_options[] = { + { NULL } +}; + +FRAMESYNC_DEFINE_CLASS(identity, IdentityContext, fs); + +static unsigned identity_line_8bit(const uint8_t *main_line, const uint8_t *ref_line, int outw) +{ + unsigned score = 0; + + for (int j = 0; j < outw; j++) + score += main_line[j] == ref_line[j]; + + return score; +} + +static unsigned identity_line_16bit(const uint8_t *mmain_line, const uint8_t *rref_line, int outw) +{ + const uint16_t *main_line = (const uint16_t *)mmain_line; + const uint16_t *ref_line = (const uint16_t *)rref_line; + unsigned score = 0; + + for (int j = 0; j < outw; j++) + score += main_line[j] == ref_line[j]; + + return score; +} + +typedef struct ThreadData { + const uint8_t *main_data[4]; + const uint8_t *ref_data[4]; + int main_linesize[4]; + int ref_linesize[4]; + int planewidth[4]; + int planeheight[4]; + uint64_t **score; + int nb_components; +} ThreadData; + +static +int compute_images_identity(AVFilterContext *ctx, void *arg, + int jobnr, int nb_jobs) +{ + IdentityContext *s = ctx->priv; + ThreadData *td = arg; + uint64_t *score = td->score[jobnr]; + + for (int c = 0; c < td->nb_components; c++) { + const int outw = td->planewidth[c]; + const int outh = td->planeheight[c]; + const int slice_start = (outh * jobnr) / nb_jobs; + const int slice_end = (outh * (jobnr+1)) / nb_jobs; + const int ref_linesize = td->ref_linesize[c]; + const int main_linesize = td->main_linesize[c]; + const uint8_t *main_line = td->main_data[c] + main_linesize * slice_start; + const uint8_t *ref_line = td->ref_data[c] + ref_linesize * slice_start; + uint64_t m = 0; + + for (int i = slice_start; i < slice_end; i++) { + m += s->filter_line(main_line, ref_line, outw); + ref_line += ref_linesize; + main_line += main_linesize; + } + score[c] = m; + } + + return 0; +} + +static void set_meta(AVDictionary **metadata, const char *key, char comp, float d) +{ + char value[128]; + snprintf(value, sizeof(value), "%f", 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 int do_identity(FFFrameSync *fs) +{ + AVFilterContext *ctx = fs->parent; + IdentityContext *s = ctx->priv; + AVFrame *master, *ref; + double comp_identity[4], score = 0.; + uint64_t comp_sum[4] = { 0 }; + AVDictionary **metadata; + ThreadData td; + int ret; + + ret = ff_framesync_dualinput_get(fs, &master, &ref); + if (ret < 0) + return ret; + if (ctx->is_disabled || !ref) + return ff_filter_frame(ctx->outputs[0], master); + metadata = &master->metadata; + + td.nb_components = s->nb_components; + td.score = s->scores; + for (int c = 0; c < s->nb_components; c++) { + td.main_data[c] = master->data[c]; + td.ref_data[c] = ref->data[c]; + td.main_linesize[c] = master->linesize[c]; + td.ref_linesize[c] = ref->linesize[c]; + td.planewidth[c] = s->planewidth[c]; + td.planeheight[c] = s->planeheight[c]; + } + + ctx->internal->execute(ctx, compute_images_identity, &td, NULL, FFMIN(s->planeheight[1], s->nb_threads)); + + for (int j = 0; j < s->nb_threads; j++) { + for (int c = 0; c < s->nb_components; c++) + comp_sum[c] += s->scores[j][c]; + } + + for (int c = 0; c < s->nb_components; c++) + comp_identity[c] = comp_sum[c] / ((double)s->planewidth[c] * s->planeheight[c]); + + for (int c = 0; c < s->nb_components; c++) + score += comp_identity[c]; + score /= s->nb_components; + + s->min_score = FFMIN(s->min_score, score); + s->max_score = FFMAX(s->max_score, score); + + s->score += score; + + for (int j = 0; j < s->nb_components; j++) + s->score_comp[j] += comp_identity[j]; + s->nb_frames++; + + for (int j = 0; j < s->nb_components; j++) { + int c = s->is_rgb ? s->rgba_map[j] : j; + set_meta(metadata, "lavfi.identity.identity.", s->comps[j], comp_identity[c]); + } + set_meta(metadata, "lavfi.identity.identity_avg", 0, score); + + return ff_filter_frame(ctx->outputs[0], master); +} + +static av_cold int init(AVFilterContext *ctx) +{ + IdentityContext *s = ctx->priv; + + s->fs.on_event = do_identity; + + 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; + IdentityContext *s = ctx->priv; + + s->nb_threads = ff_filter_get_nb_threads(ctx); + s->nb_components = desc->nb_components; + 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); + } + + s->is_rgb = ff_fill_rgba_map(s->rgba_map, inlink->format) >= 0; + s->comps[0] = s->is_rgb ? 'R' : 'Y' ; + s->comps[1] = s->is_rgb ? 'G' : 'U' ; + s->comps[2] = s->is_rgb ? 'B' : 'V' ; + s->comps[3] = 'A'; + + 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; + + s->scores = av_calloc(s->nb_threads, sizeof(*s->scores)); + if (!s->scores) + return AVERROR(ENOMEM); + + for (int t = 0; t < s->nb_threads && s->scores; t++) { + s->scores[t] = av_calloc(s->nb_components, sizeof(*s->scores[0])); + if (!s->scores[t]) + return AVERROR(ENOMEM); + } + + s->min_score = +INFINITY; + s->max_score = -INFINITY; + + s->filter_line = desc->comp[0].depth > 8 ? identity_line_16bit : identity_line_8bit; + + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + IdentityContext *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; + + outlink->time_base = s->fs.time_base; + + if (av_cmp_q(mainlink->time_base, outlink->time_base) || + av_cmp_q(ctx->inputs[1]->time_base, outlink->time_base)) + av_log(ctx, AV_LOG_WARNING, "not matching timebases found between first input: %d/%d and second input %d/%d, results may be incorrect!\n", + mainlink->time_base.num, mainlink->time_base.den, + ctx->inputs[1]->time_base.num, ctx->inputs[1]->time_base.den); + + return 0; +} + +static int activate(AVFilterContext *ctx) +{ + IdentityContext *s = ctx->priv; + return ff_framesync_activate(&s->fs); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + IdentityContext *s = ctx->priv; + + if (s->nb_frames > 0) { + char buf[256]; + + buf[0] = 0; + for (int j = 0; j < s->nb_components; j++) { + int c = s->is_rgb ? s->rgba_map[j] : j; + av_strlcatf(buf, sizeof(buf), " %c:%f", s->comps[j], s->score_comp[c] / s->nb_frames); + } + + av_log(ctx, AV_LOG_INFO, "Identity%s average:%f min:%f max:%f\n", + buf, + s->score / s->nb_frames, + s->min_score, + s->max_score); + } + + ff_framesync_uninit(&s->fs); + for (int t = 0; t < s->nb_threads && s->scores; t++) + av_freep(&s->scores[t]); + av_freep(&s->scores); +} + +static const AVFilterPad identity_inputs[] = { + { + .name = "main", + .type = AVMEDIA_TYPE_VIDEO, + },{ + .name = "reference", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input_ref, + }, + { NULL } +}; + +static const AVFilterPad identity_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, + { NULL } +}; + +AVFilter ff_vf_identity = { + .name = "identity", + .description = NULL_IF_CONFIG_SMALL("Calculate the Identity between two video streams."), + .preinit = identity_framesync_preinit, + .init = init, + .uninit = uninit, + .query_formats = query_formats, + .activate = activate, + .priv_size = sizeof(IdentityContext), + .priv_class = &identity_class, + .inputs = identity_inputs, + .outputs = identity_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, +}; From patchwork Wed Mar 3 18:43:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 26078 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 24B2E44BE73 for ; Wed, 3 Mar 2021 20:43:38 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0A7C868A8FA; Wed, 3 Mar 2021 20:43:38 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ej1-f48.google.com (mail-ej1-f48.google.com [209.85.218.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 758EE68A700 for ; Wed, 3 Mar 2021 20:43:29 +0200 (EET) Received: by mail-ej1-f48.google.com with SMTP id ci14so25194362ejc.7 for ; Wed, 03 Mar 2021 10:43:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=JGJ9ALH4rUZts6OoYYCIGQpfeoEzoJLXcaAuf9zQroc=; b=fwKvhhhectQv/CDtJ7kzJ3VP42QO1vfw67moDPAXw1pmlRU3i3PM8slp4ygURtbldC SkvH1ALugWOBuL2ATm3Tw/f70ZWYw4yekS5v2sv4DB/u0Vz4AUCKu8qagfdlPfq87Vjy qm2BXyA8JwrHA6SR4okZVjmikTzMWrEr71TMQaymvRMWr30OikfaLDufIuXkO9VABsbi 7jr27ohHX5e5S229RrLQy2dZBEyR5iGXVy7gEhSPQhlF+PacufhK+fpaCxo7UXbq6lAQ S5wy+mFMMFT06Ua8VugrbCPPJOuBpYhl4PI3RTHGtCXdWvZHY3q5jGAW8nQyhgUPSQYt fkhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=JGJ9ALH4rUZts6OoYYCIGQpfeoEzoJLXcaAuf9zQroc=; b=ja1eo2+cTGpFlrG9LaVGiwk2QqrZjMX36TeKPFgSzxAphf45sKxJ9wd4lFE1Zo3h2R 7hCj78XE4iQgSns5iTdP/tYJtwWMmEuFDvMjBu/qCQacUMoIk30grxmgobWldVwEkivc 1s8HHPgFoFWJbNcNjPpeDKo8BkDYYZ2ljURh8Yrbn7fWjBPwZjCqWYSzIt+s/RWSHE3G p6r8ps4HZoDjsFPI1ijknl8DVcmS1eBbyITtSLUGJSf/68IHzBOsSVrsjiXeC/zqqsh7 d/cjidxjme9PHjwjEG+qum9m8GkEZmYOI52f91WbmhlG7w0+xlb0u+TBORok8kUIb1VU w9wA== X-Gm-Message-State: AOAM533/HMCUO7kq5k/oQwMDFrwfkhS6IWMmEcqL6tCaKftOD3QTnjNB gpQUc0M5U/GgbgfLYAg43ed2FM1TByQ= X-Google-Smtp-Source: ABdhPJwpA+mXSfK7Y/Nue9Rwhm3Xjq3hlWpyeXIZMBqwyfE2isTgjSKCK85CqBxg19F5Bq49oUDobg== X-Received: by 2002:a17:906:380b:: with SMTP id v11mr199241ejc.183.1614797008864; Wed, 03 Mar 2021 10:43:28 -0800 (PST) Received: from localhost.localdomain ([94.250.185.123]) by smtp.gmail.com with ESMTPSA id v13sm24298271ede.5.2021.03.03.10.43.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Mar 2021 10:43:28 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Wed, 3 Mar 2021 19:43:17 +0100 Message-Id: <20210303184317.8809-2-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210303184317.8809-1-onemda@gmail.com> References: <20210303184317.8809-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH 2/2] avfilter: add msad video filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- doc/filters.texi | 21 +++++++ libavfilter/allfilters.c | 1 + libavfilter/vf_identity.c | 115 ++++++++++++++++++++++++++++++++------ 3 files changed, 120 insertions(+), 17 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index 38698b6542..eb0a0b6a0b 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -14903,6 +14903,27 @@ Default value for @option{hi} is 64*12, default value for @option{lo} is 64*5, and default value for @option{frac} is 0.33. @end table +@section msad + +Obtain the MSAD (Mean Sum of Absolute Differences) between two input videos. + +This filter takes two input videos. + +Both input videos must have the same resolution and pixel format for +this filter to work correctly. Also it assumes that both inputs +have the same number of frames, which are compared one by one. + +The obtained per component, average, min and max MSAD is printed through +the logging system. + +The filter stores the calculated MSAD of each frame in frame metadata. + +In the below example the input file @file{main.mpg} being processed is compared +with the reference file @file{ref.mpg}. + +@example +ffmpeg -i main.mpg -i ref.mpg -lavfi msad -f null - +@end example @section negate diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 4bd8eabd28..0d02c2b748 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -320,6 +320,7 @@ extern AVFilter ff_vf_minterpolate; extern AVFilter ff_vf_mix; extern AVFilter ff_vf_monochrome; extern AVFilter ff_vf_mpdecimate; +extern AVFilter ff_vf_msad; extern AVFilter ff_vf_negate; extern AVFilter ff_vf_nlmeans; extern AVFilter ff_vf_nlmeans_opencl; diff --git a/libavfilter/vf_identity.c b/libavfilter/vf_identity.c index 02dda392b3..60d02e328b 100644 --- a/libavfilter/vf_identity.c +++ b/libavfilter/vf_identity.c @@ -32,6 +32,7 @@ #include "framesync.h" #include "internal.h" #include "video.h" +#include "scene_sad.h" typedef struct IdentityContext { const AVClass *class; @@ -39,7 +40,9 @@ typedef struct IdentityContext { double score, min_score, max_score, score_comp[4]; uint64_t nb_frames; int is_rgb; + int is_msad; uint8_t rgba_map[4]; + int max[4]; char comps[4]; int nb_components; int nb_threads; @@ -47,17 +50,14 @@ typedef struct IdentityContext { int planeheight[4]; uint64_t **scores; unsigned (*filter_line)(const uint8_t *buf, const uint8_t *ref, int w); + int (*filter_slice)(AVFilterContext *ctx, void *arg, + int jobnr, int nb_jobs); + ff_scene_sad_fn sad; } IdentityContext; #define OFFSET(x) offsetof(IdentityContext, x) #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM -static const AVOption identity_options[] = { - { NULL } -}; - -FRAMESYNC_DEFINE_CLASS(identity, IdentityContext, fs); - static unsigned identity_line_8bit(const uint8_t *main_line, const uint8_t *ref_line, int outw) { unsigned score = 0; @@ -91,6 +91,34 @@ typedef struct ThreadData { int nb_components; } ThreadData; +static +int compute_images_msad(AVFilterContext *ctx, void *arg, + int jobnr, int nb_jobs) +{ + IdentityContext *s = ctx->priv; + ThreadData *td = arg; + uint64_t *score = td->score[jobnr]; + + for (int c = 0; c < td->nb_components; c++) { + const int outw = td->planewidth[c]; + const int outh = td->planeheight[c]; + const int slice_start = (outh * jobnr) / nb_jobs; + const int slice_end = (outh * (jobnr+1)) / nb_jobs; + const int ref_linesize = td->ref_linesize[c]; + const int main_linesize = td->main_linesize[c]; + const uint8_t *main_line = td->main_data[c] + main_linesize * slice_start; + const uint8_t *ref_line = td->ref_data[c] + ref_linesize * slice_start; + uint64_t m = 0; + + s->sad(main_line, main_linesize, ref_line, ref_linesize, + outw, slice_end - slice_start, &m); + + score[c] = m; + } + + return 0; +} + static int compute_images_identity(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) @@ -121,16 +149,21 @@ int compute_images_identity(AVFilterContext *ctx, void *arg, return 0; } -static void set_meta(AVDictionary **metadata, const char *key, char comp, float d) +static void set_meta(AVFilterContext *ctx, + AVDictionary **metadata, const char *key, char comp, float d) { char value[128]; snprintf(value, sizeof(value), "%f", d); if (comp) { char key2[128]; - snprintf(key2, sizeof(key2), "%s%c", key, comp); + snprintf(key2, sizeof(key2), "lavfi.%s.%s%s%c", + ctx->filter->name, ctx->filter->name, key, comp); av_dict_set(metadata, key2, value, 0); } else { - av_dict_set(metadata, key, value, 0); + char key2[128]; + snprintf(key2, sizeof(key2), "lavfi.%s.%s%s", + ctx->filter->name, ctx->filter->name, key); + av_dict_set(metadata, key2, value, 0); } } @@ -139,7 +172,7 @@ static int do_identity(FFFrameSync *fs) AVFilterContext *ctx = fs->parent; IdentityContext *s = ctx->priv; AVFrame *master, *ref; - double comp_identity[4], score = 0.; + double comp_score[4], score = 0.; uint64_t comp_sum[4] = { 0 }; AVDictionary **metadata; ThreadData td; @@ -163,7 +196,7 @@ static int do_identity(FFFrameSync *fs) td.planeheight[c] = s->planeheight[c]; } - ctx->internal->execute(ctx, compute_images_identity, &td, NULL, FFMIN(s->planeheight[1], s->nb_threads)); + ctx->internal->execute(ctx, s->filter_slice, &td, NULL, FFMIN(s->planeheight[1], s->nb_threads)); for (int j = 0; j < s->nb_threads; j++) { for (int c = 0; c < s->nb_components; c++) @@ -171,10 +204,13 @@ static int do_identity(FFFrameSync *fs) } for (int c = 0; c < s->nb_components; c++) - comp_identity[c] = comp_sum[c] / ((double)s->planewidth[c] * s->planeheight[c]); + comp_score[c] = comp_sum[c] / ((double)s->planewidth[c] * s->planeheight[c]); + + for (int c = 0; c < s->nb_components && s->is_msad; c++) + comp_score[c] /= (double)s->max[c]; for (int c = 0; c < s->nb_components; c++) - score += comp_identity[c]; + score += comp_score[c]; score /= s->nb_components; s->min_score = FFMIN(s->min_score, score); @@ -183,14 +219,14 @@ static int do_identity(FFFrameSync *fs) s->score += score; for (int j = 0; j < s->nb_components; j++) - s->score_comp[j] += comp_identity[j]; + s->score_comp[j] += comp_score[j]; s->nb_frames++; for (int j = 0; j < s->nb_components; j++) { int c = s->is_rgb ? s->rgba_map[j] : j; - set_meta(metadata, "lavfi.identity.identity.", s->comps[j], comp_identity[c]); + set_meta(ctx, metadata, ".", s->comps[j], comp_score[c]); } - set_meta(metadata, "lavfi.identity.identity_avg", 0, score); + set_meta(ctx, metadata, "_avg", 0, score); return ff_filter_frame(ctx->outputs[0], master); } @@ -269,8 +305,19 @@ static int config_input_ref(AVFilterLink *inlink) s->min_score = +INFINITY; s->max_score = -INFINITY; + s->max[0] = (1 << desc->comp[0].depth) - 1; + s->max[1] = (1 << desc->comp[1].depth) - 1; + s->max[2] = (1 << desc->comp[2].depth) - 1; + s->max[3] = (1 << desc->comp[3].depth) - 1; + + s->is_msad = !strcmp(ctx->filter->name, "msad"); + s->filter_slice = !s->is_msad ? compute_images_identity : compute_images_msad; s->filter_line = desc->comp[0].depth > 8 ? identity_line_16bit : identity_line_8bit; + s->sad = ff_scene_sad_get_fn(desc->comp[0].depth <= 8 ? 8 : 16); + if (!s->sad) + return AVERROR(EINVAL); + return 0; } @@ -322,7 +369,8 @@ static av_cold void uninit(AVFilterContext *ctx) av_strlcatf(buf, sizeof(buf), " %c:%f", s->comps[j], s->score_comp[c] / s->nb_frames); } - av_log(ctx, AV_LOG_INFO, "Identity%s average:%f min:%f max:%f\n", + av_log(ctx, AV_LOG_INFO, "%s%s average:%f min:%f max:%f\n", + ctx->filter->name, buf, s->score / s->nb_frames, s->min_score, @@ -356,6 +404,15 @@ static const AVFilterPad identity_outputs[] = { { NULL } }; +static const AVOption options[] = { + { NULL } +}; + +#if CONFIG_IDENTITY_FILTER + +#define identity_options options +FRAMESYNC_DEFINE_CLASS(identity, IdentityContext, fs); + AVFilter ff_vf_identity = { .name = "identity", .description = NULL_IF_CONFIG_SMALL("Calculate the Identity between two video streams."), @@ -370,3 +427,27 @@ AVFilter ff_vf_identity = { .outputs = identity_outputs, .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, }; + +#endif /* CONFIG_IDENTITY_FILTER */ + +#if CONFIG_MSAD_FILTER + +#define msad_options options +FRAMESYNC_DEFINE_CLASS(msad, IdentityContext, fs); + +AVFilter ff_vf_msad = { + .name = "msad", + .description = NULL_IF_CONFIG_SMALL("Calculate the MSAD between two video streams."), + .preinit = msad_framesync_preinit, + .init = init, + .uninit = uninit, + .query_formats = query_formats, + .activate = activate, + .priv_size = sizeof(IdentityContext), + .priv_class = &msad_class, + .inputs = identity_inputs, + .outputs = identity_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, +}; + +#endif /* CONFIG_MSAD_FILTER */