From patchwork Sun Nov 7 14:52:10 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: 31324 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a5e:dc01:0:0:0:0:0 with SMTP id b1csp2592582iok; Sun, 7 Nov 2021 06:52:05 -0800 (PST) X-Google-Smtp-Source: ABdhPJy3ypUF2NMxjRbjbfPlF3D5ONKw5nZTVrHzbG53FLAodnrO0fP0gW8fMkkFTPuhamQdJ2OF X-Received: by 2002:a05:6402:2063:: with SMTP id bd3mr87069119edb.81.1636296725134; Sun, 07 Nov 2021 06:52:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1636296725; cv=none; d=google.com; s=arc-20160816; b=0wsYRy06Z3UZKlK+XrX15V/cxRcPSqcBFOygebTpDVzX43J/udRsAfTmaU4DINXpRL 9jMJL3TNs/GQIiCRf5wQ3Pb4GFqqOBseP9tiiCG2n7CDJtK88bq8MPDclOIAsbB7MneH B+ffEMEVebJTzb7+M2EXWK8MsU01S2JERUQS4SNBlNgwVPDRdeaGymki5x/C0ovQt3Xk xK1RePtm9C50kUQ0rglaYiwkICWHF7XN4CMkHARQLHJYuGWutKfvQNu4OecZkmXaCXZR vnlhb5sclsSE08qQOR3IMn/iJDrr8UsyqGvFNC3Iibs969xS4Hql9rvvRBiWma+w+s8i mW1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=+9h3e1WXn24UCX5ySUb5B/F/46Q1w8pJQxzK39fpi98=; b=vySekh+0+zaZ+/rrvKP5Xk/S8QbyxgzUr+kMf6JWULutMbYUR/caLrers7fEOwEHAY NlpYblhL4qVH6hcsitbkUAMXLEOYFR7DB0aR/0VDXAQIZ0n5jyXqThfd2vj86WpvgFBi ffhAjxHRjnfdgSbRURw4o0ShN66h8EQ7SpWlJgrlo9IG5eJHim9Rj3/5nB4umWktwqyb FEBMLxUqz8p8PlZHGOpfRUIGw+awUTjjbH5xhhRmeNUxMs3FEJlmkQJRaxtmbWEiqWFT bNNhzIOaDzG0jccjHo83BAD4XMmhEYdaLW1nz0UN2RHu9a7UyxzOJVbNRBMzWHLKL2ZA 66Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b="I+L1c/v0"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id f29si19697740edc.273.2021.11.07.06.52.04; Sun, 07 Nov 2021 06:52:05 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b="I+L1c/v0"; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3920C68A5D1; Sun, 7 Nov 2021 16:52:01 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DCEED680CB2 for ; Sun, 7 Nov 2021 16:51:54 +0200 (EET) Received: by mail-ed1-f43.google.com with SMTP id r12so52085355edt.6 for ; Sun, 07 Nov 2021 06:51:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=Um+bawZUZppa+UD70qlQhIlkrOywA50CGG8UfkkMk1I=; b=I+L1c/v0LlW3ckqEHE8PFXT7jNRqDxZ3eEr9L4wHqaopKzmEmruRe21Z9UidVWHlKg Ft/V0Hqz8pqLZZNhxUZD/vCUJvY95IDmtqqPPQmY4CsVMKdJZes1RDKyrr2oU5VXLl3/ tIhLxZQ1fdHWPUnYnGYBYj6GyRS8ukJj0RIJaZ5ink2DzRU8O5lJzAw0+5cmwIYK7kS6 todQptjr8ouj+kqLhoLUGcvYmVTGyO3MiI6KAbYK92qSVkaGz5Rq6nKYueRtJub5mm9P vhh03ox2jV3JUucLrdlJEnWzvKiHKk+sMKGjdnjwbz8Dr/FPLyo82YL1usHiae4UHPlX S6vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=Um+bawZUZppa+UD70qlQhIlkrOywA50CGG8UfkkMk1I=; b=NyFUqUTGweDomTPXvBFHnu7vVxq7F7BFAUfo+Rypst39TwqtnYCAFQa6Ko7HTaAGST E/wTlBM0SPHLr6VeECiOgPPf20QerWbjziIXwjthgS0ml6oeMItTNCAUsqneNB29XuaU OKvlcbOdHxyohtzsVxXJXMwLx92mxu+GKPUiipVZyd08RHFKeVwFplNwLUW3JLItrOcZ 4/eykf3bXlNnmSmj+AsMX2lwZWMIe8x34Khf9hHPHI6vSptiaZI0/YApv6pbgEjJEeuv /REEnHPggFKqJzNzYVWJkwXKUKZJXqUvXIP5M8MEnWZQikiu9T6qjAlmjQruuv4vZdUG HGTA== X-Gm-Message-State: AOAM531ey6oOAyfMqFRlkxqDpuEI1CQNCCV5QR/cq3+6u/58Z1lOL29P KsIIGp3WlrTrTFY1p50r93c1MPnsKIs= X-Received: by 2002:a17:907:3f18:: with SMTP id hq24mr10332537ejc.506.1636296714199; Sun, 07 Nov 2021 06:51:54 -0800 (PST) Received: from localhost.localdomain ([212.15.177.0]) by smtp.gmail.com with ESMTPSA id jg36sm7120590ejc.44.2021.11.07.06.51.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Nov 2021 06:51:53 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 7 Nov 2021 15:52:10 +0100 Message-Id: <20211107145210.201942-1-onemda@gmail.com> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH]{WIP} avfilter: add moddif video filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: ne2FTJ1oYxAP Signed-off-by: Paul B Mahol --- Allows combination of output of spatial only deinterlacers and spatio-temporal deinterlacers. Gives overall higher PSNR result. Spatial only deinterlacer output is used in case spatio-temporal one would use simple spatial interpolations. --- libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_moddif.c | 335 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 337 insertions(+) create mode 100644 libavfilter/vf_moddif.c diff --git a/libavfilter/Makefile b/libavfilter/Makefile index aa499696d7..6fa344489f 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -349,6 +349,7 @@ OBJS-$(CONFIG_METADATA_FILTER) += f_metadata.o OBJS-$(CONFIG_MIDEQUALIZER_FILTER) += vf_midequalizer.o framesync.o OBJS-$(CONFIG_MINTERPOLATE_FILTER) += vf_minterpolate.o motion_estimation.o OBJS-$(CONFIG_MIX_FILTER) += vf_mix.o framesync.o +OBJS-$(CONFIG_MODDIF_FILTER) += vf_moddif.o OBJS-$(CONFIG_MONOCHROME_FILTER) += vf_monochrome.o OBJS-$(CONFIG_MORPHO_FILTER) += vf_morpho.o OBJS-$(CONFIG_MPDECIMATE_FILTER) += vf_mpdecimate.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 71087fbf60..88d91db3f1 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -334,6 +334,7 @@ extern const AVFilter ff_vf_metadata; extern const AVFilter ff_vf_midequalizer; extern const AVFilter ff_vf_minterpolate; extern const AVFilter ff_vf_mix; +extern const AVFilter ff_vf_moddif; extern const AVFilter ff_vf_monochrome; extern const AVFilter ff_vf_morpho; extern const AVFilter ff_vf_mpdecimate; diff --git a/libavfilter/vf_moddif.c b/libavfilter/vf_moddif.c new file mode 100644 index 0000000000..90aaa5328d --- /dev/null +++ b/libavfilter/vf_moddif.c @@ -0,0 +1,335 @@ +/* + * Copyright (c) 2016 Paul B Mahol + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/imgutils.h" +#include "libavutil/pixdesc.h" +#include "libavutil/opt.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" +#include "framesync.h" + +#define OFFSET(x) offsetof(ModDifContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM + +typedef struct ThreadData { + AVFrame *input, *spatial, *temporal, *output; + int parity; +} ThreadData; + +typedef struct ModDifContext { + const AVClass *class; + + int linesize[4]; + int planewidth[4], planeheight[4]; + int nb_planes; + int depth; + int parity; + int is_second; + int64_t pts; + AVFrame *prev, *cur, *next; + FFFrameSync fs; +} ModDifContext; + +#define CONST(name, help, val, unit) { name, help, 0, AV_OPT_TYPE_CONST, {.i64=val}, INT_MIN, INT_MAX, FLAGS, unit } + +static const AVOption moddif_options[] = { + { "parity", "specify the assumed picture field parity", OFFSET(parity), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, FLAGS, "parity" }, + CONST("tff", "assume top field first", 0, "parity"), + CONST("bff", "assume bottom field first", 1, "parity"), + CONST("auto", "auto detect parity", -1, "parity"), + { NULL } +}; + +AVFILTER_DEFINE_CLASS(moddif); + +static const enum AVPixelFormat pix_fmts[] = { + AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, + AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, + AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, + AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, + AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, + AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, + AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, + AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, + AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, + AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, + AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, + AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12, + AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, + 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_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16, + AV_PIX_FMT_NONE +}; + +static int moddif_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ModDifContext *s = ctx->priv; + ThreadData *td = arg; + const int parity = td->parity; + + for (int p = 0; p < s->nb_planes; p++) { + const ptrdiff_t in_linesize = td->input->linesize[p]; + const ptrdiff_t tlinesize = td->temporal->linesize[p]; + const ptrdiff_t slinesize = td->spatial->linesize[p]; + const ptrdiff_t out_linesize = td->output->linesize[p]; + const int w = s->planewidth[p]; + const int h = s->planeheight[p]; + const int slice_start = (h * jobnr) / nb_jobs; + const int slice_end = (h * (jobnr+1)) / nb_jobs; + const uint8_t *in_src = td->input->data[p] + slice_start * in_linesize; + const uint8_t *spatial_src = td->spatial->data[p] + slice_start * slinesize; + const uint8_t *temporal_src = td->temporal->data[p] + slice_start * tlinesize; + uint8_t *dst = td->output->data[p] + slice_start * out_linesize; + + for (int y = slice_start; y < slice_end; y++) { + if ((y ^ parity) & 1) { + const ptrdiff_t cur_linesize = y > 0 && y < h - 1 ? s->cur->linesize[p] : 0; + const uint8_t *cur = s->cur->data[p] + y * cur_linesize; + const uint8_t *prev = s->prev->data[p] + y * s->prev->linesize[p]; + const uint8_t *next = s->next->data[p] + y * s->next->linesize[p]; + const uint8_t *prev2 = parity ? prev : cur; + const uint8_t *next2 = parity ? cur : next; + + for (int x = 0; x < w; x++) { + int c = cur[x + cur_linesize]; + int e = cur[x - cur_linesize]; + if (FFABS(c - e) > FFABS(prev2[x] - next2[x])) { + dst[x] = temporal_src[x]; + } else { + dst[x] = spatial_src[x]; + } + } + } else { + memcpy(dst, s->next->data[p] + y * s->next->linesize[p], w); + } + + dst += out_linesize; + in_src += in_linesize; + spatial_src += slinesize; + temporal_src += tlinesize; + } + } + + return 0; +} + +static int process_frame(FFFrameSync *fs) +{ + AVFilterContext *ctx = fs->parent; + ModDifContext *s = fs->opaque; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out, *input, *spatial, *temporal; + int ret; + + if ((ret = ff_framesync_get_frame(&s->fs, 0, &input, 0)) < 0 || + (ret = ff_framesync_get_frame(&s->fs, 1, &spatial, 0)) < 0 || + (ret = ff_framesync_get_frame(&s->fs, 2, &temporal, 0)) < 0) + return ret; + + if (s->pts != input->pts) { + av_frame_free(&s->prev); + s->prev = s->cur; + s->cur = s->next; + s->next = av_frame_clone(input); + s->pts = input->pts; + s->is_second = 0; + if (!s->cur) { + s->cur = av_frame_clone(s->next); + if (!s->cur) + return AVERROR(ENOMEM); + } + } else { + s->is_second = 1; + } + + if (ctx->is_disabled) { + out = av_frame_clone(input); + if (!out) + return AVERROR(ENOMEM); + } else if (!s->prev) { + out = av_frame_clone(spatial); + if (!out) + return AVERROR(ENOMEM); + } else { + ThreadData td; + int tff; + + if (s->parity == -1) { + tff = s->cur->interlaced_frame ? + s->cur->top_field_first : 1; + } else { + tff = s->parity ^ 1; + } + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) + return AVERROR(ENOMEM); + av_frame_copy_props(out, spatial); + + td.input = input; + td.spatial = spatial; + td.temporal = temporal; + td.output = out; + td.parity = tff ^ !s->is_second; + + ff_filter_execute(ctx, moddif_slice, &td, NULL, + FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx))); + } + out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base); + + return ff_filter_frame(outlink, out); +} + +static int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + ModDifContext *s = ctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + int vsub, hsub, ret; + + s->pts = AV_NOPTS_VALUE; + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + + if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0) + return ret; + + hsub = desc->log2_chroma_w; + vsub = desc->log2_chroma_h; + s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub); + s->planeheight[0] = s->planeheight[3] = inlink->h; + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, hsub); + s->planewidth[0] = s->planewidth[3] = inlink->w; + + s->depth = desc->comp[0].depth; + + return 0; +} + +static int config_output(AVFilterLink *outlink) +{ + AVFilterContext *ctx = outlink->src; + ModDifContext *s = ctx->priv; + AVFilterLink *input = ctx->inputs[0]; + AVFilterLink *spatial = ctx->inputs[1]; + AVFilterLink *temporal = ctx->inputs[2]; + FFFrameSyncIn *in; + int ret; + + if (input->w != spatial->w || input->h != spatial->h || + input->w != temporal->w || input->h != temporal->h) { + av_log(ctx, AV_LOG_ERROR, "First input link %s parameters " + "(size %dx%d) do not match the corresponding " + "second input link %s parameters (%dx%d) " + "and/or third input link %s parameters (size %dx%d)\n", + ctx->input_pads[0].name, input->w, input->h, + ctx->input_pads[1].name, spatial->w, spatial->h, + ctx->input_pads[2].name, temporal->w, temporal->h); + return AVERROR(EINVAL); + } + + outlink->w = input->w; + outlink->h = input->h; + outlink->sample_aspect_ratio = input->sample_aspect_ratio; + outlink->frame_rate = spatial->frame_rate; + + if ((ret = ff_framesync_init(&s->fs, ctx, 3)) < 0) + return ret; + + in = s->fs.in; + in[0].time_base = input->time_base; + in[1].time_base = spatial->time_base; + in[2].time_base = temporal->time_base; + in[0].sync = 1; + in[0].before = EXT_STOP; + in[0].after = EXT_INFINITY; + in[1].sync = 1; + in[1].before = EXT_STOP; + in[1].after = EXT_INFINITY; + in[2].sync = 1; + in[2].before = EXT_STOP; + in[2].after = EXT_INFINITY; + s->fs.opaque = s; + s->fs.on_event = process_frame; + + ret = ff_framesync_configure(&s->fs); + outlink->time_base = s->fs.time_base; + + return ret; +} + +static int activate(AVFilterContext *ctx) +{ + ModDifContext *s = ctx->priv; + return ff_framesync_activate(&s->fs); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + ModDifContext *s = ctx->priv; + + ff_framesync_uninit(&s->fs); + + av_frame_free(&s->prev); + av_frame_free(&s->cur ); + av_frame_free(&s->next); +} + +static const AVFilterPad moddif_inputs[] = { + { + .name = "input", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + }, + { + .name = "spatial", + .type = AVMEDIA_TYPE_VIDEO, + }, + { + .name = "temporal", + .type = AVMEDIA_TYPE_VIDEO, + }, +}; + +static const AVFilterPad moddif_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, +}; + +const AVFilter ff_vf_moddif = { + .name = "moddif", + .description = NULL_IF_CONFIG_SMALL("Apply modular deinterlacing."), + .priv_size = sizeof(ModDifContext), + .uninit = uninit, + .activate = activate, + FILTER_INPUTS(moddif_inputs), + FILTER_OUTPUTS(moddif_outputs), + FILTER_PIXFMTS_ARRAY(pix_fmts), + .priv_class = &moddif_class, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL | AVFILTER_FLAG_SLICE_THREADS, + .process_command = ff_filter_process_command, +};