From patchwork Sun Aug 1 10:47: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: 29159 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:6c0f:0:0:0:0:0 with SMTP id a15csp717799ioh; Sun, 1 Aug 2021 03:54:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcjfHFnsQ1QNW/E6zQ7d9W6k2pjqbOvq04O9wJ5bCrzzZjCl83zkw36/mdHnrz02Orm4G2 X-Received: by 2002:a17:906:4b18:: with SMTP id y24mr10547004eju.42.1627815266411; Sun, 01 Aug 2021 03:54:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627815266; cv=none; d=google.com; s=arc-20160816; b=b+4UyBr8KR2sD+DD/tgTdGQFYgMKCMZtnFnypUQk2rYBkP8BEwtMCUVpcjbOj8EGbe i9Joeq/ptLs8XKxWlzSBUAiEhGcrIktd47BxDMOCIp1uSeSPrB45wl3x1sDI5eDL5Gu/ SJNNI6aL1KUGdU/fxihCNkvzPgnhMKIJq2qB7n6AByK1QolwLT9Q04huzFluwrO1ZnpW +QhHA8uiaWW0su0DSBEAOuL2BTl0NHEjRxQWVo3sh+Y+P+daoHElWkvgsPJOVLfWyUv8 eN7Urj6SZpsZeiOtWEYo82y7gDyjDQln16wpQUZkeziQUn8VDsK2VBjmoNo0is96VL65 RqGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to; bh=uk/lDqEZdNiIXM8KAfWqaBuLYwpbRYtVjnYPMUSXC7I=; b=ubDPWLwc8SX/WMV4Cj/o9TMjfVqMP54eOPNafK1NXZBM5TDw/vuUnVd7DQ23K05LWf 4ydxp3bDErb5u906VXBUcq4VLPFPA//rZ/E05TQNj8x3VfuPlyiHPaM+qh4IqCblbAfi cgGd41b5A6Qw+XpKYE8oltaK9JTdWNwlspnNjEExyrjpaN3NhD9MJ+QjRnOZl59SSFbD KSzrfWprXEp6941cPPVzSxqxO0I9uxFwqa/JXR2gDtXQ8nvwz8cDBDQI+PmNuc6q0AHJ tAk0iy68HsgM57joMFn+9sNvVXY0C0dRGW9Fj5tUQW140F91PcAqEMVY48Vwus+a/d90 yV8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=KWOkcO4+; 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 y18si7406246edl.427.2021.08.01.03.54.25; Sun, 01 Aug 2021 03:54:26 -0700 (PDT) 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=20161025 header.b=KWOkcO4+; 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 E5FB668A44A; Sun, 1 Aug 2021 13:54:21 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6ED3E689CFE for ; Sun, 1 Aug 2021 13:54:15 +0300 (EEST) Received: by mail-wm1-f52.google.com with SMTP id b128so8667749wmb.4 for ; Sun, 01 Aug 2021 03:54:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=qWubWJBBlAu4ZnaqmqOS0KPi6Kq0kE9PPIQe11Eiffo=; b=KWOkcO4+EbhIEv6X7sP0VqzM3ITcXtPQC7z7FGR8R6PdsX5ZGAze7lAV7n6jOqf2Xx B7n1gn+UasKdbhGoDj4LKlJVF5AO+8a8YigMXge2X9ZcbuUOZoFCdIW44el+3PMBYP7p 5fDAcg6b2RF6nQovWdfmpKdPIIHAnUm2zF17sXvTdXy2FhJFTOqOMNgDd4J6LwamT1xS sdky1NA5+mY1bCdPbAk1al3FldB2T78cT+nIRZajlYJM+fSOC/7h4ACF25qTTWqC9SD8 3rORASB+tIZhod2Gly789evn0nDicMYSgulV7b1taIjQfirIw4amlB+RZquIoz7PKe5O EQCA== 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=qWubWJBBlAu4ZnaqmqOS0KPi6Kq0kE9PPIQe11Eiffo=; b=Fwq996pk/wO9W5Zts5ZZt5cSgE/LJaC60S6PeMbCvx01gBDqTrozQz8d7VEkNhXAZU EwVfbvMB8VEo+EqV4ndbG2YzQGo7v9wjzQq/ZJOz6j6pMpyBVXM9XDPKT9qds8CzJiGZ qfZtXyiOGatbDY0J5lMYD4CHl1NPnTM/OJbc1LTq7s6yXO0t0YQ9NYxgee2OJT3hxpES LqbGDISnt3OstRVUxv6BJYc0F8A3fFYp9AnVtCglohX+JcaYXsUBy5rjJHA6XqZc4vgg UCsJss1h/sQ490E0cdV+M9MMScHmitMcxFVMR7RlGMDccFoQBcX8thJ0m5dC0Js1O8D6 nwMw== X-Gm-Message-State: AOAM533CQrlF8sYmE7lPa/ImI6unLXp/leNA2nTmKJiLgLTTJCAUtNSU MY8WK2EyZmoZvBSdAO8XUiGNtOFHRnQ= X-Received: by 2002:a1c:f30a:: with SMTP id q10mr11746347wmq.138.1627814839074; Sun, 01 Aug 2021 03:47:19 -0700 (PDT) Received: from localhost.localdomain ([95.168.121.63]) by smtp.gmail.com with ESMTPSA id s2sm6903731wmh.46.2021.08.01.03.47.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 01 Aug 2021 03:47:18 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 1 Aug 2021 12:47:10 +0200 Message-Id: <20210801104710.28807-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add iir 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: myYNiCggjhkC Signed-off-by: Paul B Mahol --- doc/filters.texi | 28 +++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_iir.c | 524 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 554 insertions(+) create mode 100644 libavfilter/vf_iir.c diff --git a/doc/filters.texi b/doc/filters.texi index 66c0f87e47..635179edb9 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -13735,6 +13735,34 @@ further computations. This allows inserting the idet filter as a low computation method to clean up the interlaced flag @end table +@section iir +Apply 2D Infinite Impulse Response filter to input video stream. + +The filter accepts the following options: + +@table @option +@item hn +@item vn +Set horizontal and vertical numerator coefficients of transfer function. + +@item hd +@item vd +Set horizontal and vertical denominator coefficients of transfer function. + +@item planes +Set which planes to filter. Default is all. Allowed range is from 0 to 15. +@end table + +@subsection Examples + +@itemize +@item +Apply lowpass variant: +@example +iir=hn=0.001268 0.002536 0.001268:hd=1.050338 -1.994927 0.949662:vn=0.001268 0.002536 0.001268:vd=1.050338 -1.994927 0.949662 +@end example +@end itemize + @section il Deinterleave or interleave fields. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 49c0c8342b..772971521c 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -303,6 +303,7 @@ 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_IIR_FILTER) += vf_iir.o OBJS-$(CONFIG_IL_FILTER) += vf_il.o OBJS-$(CONFIG_INFLATE_FILTER) += vf_neighbor.o OBJS-$(CONFIG_INTERLACE_FILTER) += vf_tinterlace.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index ae74f9c891..e0276eb98c 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -288,6 +288,7 @@ extern const AVFilter ff_vf_hwupload_cuda; extern const AVFilter ff_vf_hysteresis; extern const AVFilter ff_vf_identity; extern const AVFilter ff_vf_idet; +extern const AVFilter ff_vf_iir; extern const AVFilter ff_vf_il; extern const AVFilter ff_vf_inflate; extern const AVFilter ff_vf_interlace; diff --git a/libavfilter/vf_iir.c b/libavfilter/vf_iir.c new file mode 100644 index 0000000000..8b9dfcc4b4 --- /dev/null +++ b/libavfilter/vf_iir.c @@ -0,0 +1,524 @@ +/* + * 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 + */ + +#include "libavutil/imgutils.h" +#include "libavutil/opt.h" +#include "libavutil/pixdesc.h" +#include "libavutil/avstring.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct IIRContext { + const AVClass *class; + + char *h_num_str; + char *h_den_str; + char *v_num_str; + char *v_den_str; + + int h_nb_num; + int h_nb_den; + int v_nb_num; + int v_nb_den; + + float *hnum; + float *hden; + float *vnum; + float *vden; + + float **honum; + float **hoden; + float **vonum; + float **voden; + + int depth; + int flt; + int planes; + int nb_planes; + int nb_threads; + + float *in, *out; + + int linesize[4]; + int planewidth[4]; + int planeheight[4]; +} IIRContext; + +#define OFFSET(x) offsetof(IIRContext, x) +#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM + +static const AVOption iir_options[] = { + { "hn", "set horizontal numerator", OFFSET(h_num_str), AV_OPT_TYPE_STRING, {.str="1"}, 0, 0, FLAGS }, + { "hd", "set horizotnal denominator", OFFSET(h_den_str), AV_OPT_TYPE_STRING, {.str="1"}, 0, 0, FLAGS }, + { "vn", "set vertical numerator", OFFSET(v_num_str), AV_OPT_TYPE_STRING, {.str="1"}, 0, 0, FLAGS }, + { "vd", "set vertical denominator", OFFSET(v_den_str), AV_OPT_TYPE_STRING, {.str="1"}, 0, 0, FLAGS }, + { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(iir); + +typedef struct ThreadData { + int height; + int width; + + float *dst; + float *src; + int dst_linesize; + int src_linesize; +} ThreadData; + +static void horiz_slice(float *dst, float *src, + int dst_linesize, int src_linesize, + int nb_num, int nb_den, + const float *num, const float *den, + float *onum, float *oden, + int width, int height) +{ + for (int y = 0; y < height; y++) { + for (int i = 0; i < nb_num; i++) + onum[i] = src[0]; + for (int i = 0; i < nb_den; i++) + oden[i] = src[0]; + + for (int x = 0; x < width; x++) { + float sample = 0.f; + + memmove(&onum[1], &onum[0], (nb_num - 1) * sizeof(*onum)); + memmove(&oden[1], &oden[0], (nb_den - 1) * sizeof(*oden)); + onum[0] = src[x]; + for (int i = 0; i < nb_num; i++) + sample += num[i] * onum[i]; + + for (int i = 1; i < nb_den; i++) + sample -= den[i] * oden[i]; + + dst[x] = oden[0] = sample; + } + + dst += dst_linesize; + src += src_linesize; + } +} + +static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + IIRContext *s = ctx->priv; + ThreadData *td = arg; + const int height = td->height; + const int width = td->width; + const int slice_start = (height * jobnr ) / nb_jobs; + const int slice_end = (height * (jobnr+1)) / nb_jobs; + + horiz_slice(td->dst + td->dst_linesize * slice_start, + td->src + td->src_linesize * slice_start, + td->dst_linesize, td->src_linesize, + s->h_nb_num, s->h_nb_den, + s->hnum, s->hden, s->honum[jobnr], s->hoden[jobnr], + width, slice_end - slice_start); + + return 0; +} + +static void do_vertical_columns(float *pdst, float *psrc, + int dst_linesize, int src_linesize, + int nb_num, int nb_den, + const float *num, const float *den, + float *onum, float *oden, + int width, int height) +{ + for (int x = 0; x < width; x++) { + float *src = psrc; + float *dst = pdst; + + for (int i = 0; i < nb_num; i++) + onum[i] = src[x]; + for (int i = 0; i < nb_den; i++) + oden[i] = src[x]; + + for (int y = 0; y < height; y++) { + float sample = 0.f; + + memmove(&onum[1], &onum[0], (nb_num - 1) * sizeof(*onum)); + memmove(&oden[1], &oden[0], (nb_den - 1) * sizeof(*oden)); + onum[0] = src[x]; + for (int i = 0; i < nb_num; i++) + sample += num[i] * onum[i]; + + for (int i = 1; i < nb_den; i++) + sample -= den[i] * oden[i]; + + dst[x] = oden[0] = sample; + + dst += dst_linesize; + src += src_linesize; + } + } +} + +static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + IIRContext *s = ctx->priv; + ThreadData *td = arg; + const int height = td->height; + const int width = td->width; + const int slice_start = (width * jobnr ) / nb_jobs; + const int slice_end = (width * (jobnr+1)) / nb_jobs; + + do_vertical_columns(td->dst + slice_start, + td->src + slice_start, + td->dst_linesize, td->src_linesize, + s->v_nb_num, s->v_nb_den, + s->vnum, s->vden, s->vonum[jobnr], s->voden[jobnr], + slice_end - slice_start, height); + + return 0; +} + +static void iir2d(AVFilterContext *ctx, float *out, float *in, + int out_linesize, int in_linesize, + int width, int height) +{ + IIRContext *s = ctx->priv; + ThreadData td; + + td.width = width; + td.height = height; + td.dst = out; + td.src = in; + td.dst_linesize = out_linesize; + td.src_linesize = in_linesize; + ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, s->nb_threads)); + td.src = out; + td.src_linesize = out_linesize; + ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, s->nb_threads)); +} + +static int query_formats(AVFilterContext *ctx) +{ + 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_GBRPF32, AV_PIX_FMT_GBRAPF32, + AV_PIX_FMT_GRAYF32, + AV_PIX_FMT_NONE + }; + + return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); +} + +static void count_coefficients(char *item_str, int *nb_items) +{ + char *p; + + if (!item_str) + return; + + *nb_items = 1; + for (p = item_str; *p; p++) { + if (*p == ' ') + (*nb_items)++; + } +} + +static int read_coefficients(AVFilterContext *ctx, char *item_str, int nb_items, float *dst) +{ + char *p, *arg, *old_str, *saveptr = NULL; + int i; + + p = old_str = av_strdup(item_str); + if (!p) + return AVERROR(ENOMEM); + + for (i = 0; i < nb_items; i++) { + if (!(arg = av_strtok(p, " ", &saveptr))) + break; + + p = NULL; + if (av_sscanf(arg, "%f", &dst[i]) != 1) { + av_log(ctx, AV_LOG_ERROR, "Invalid coefficients supplied: %s\n", arg); + av_freep(&old_str); + return AVERROR(EINVAL); + } + } + + av_freep(&old_str); + + return 0; +} + +static void normalize_coefficients(float *num, int nb_num, float *den, int nb_den) +{ + float div = den[0]; + + for (int i = 0; i < nb_num; i++) + num[i] /= div; + + for (int i = 0; i < nb_den; i++) + den[i] /= div; +} + +static int config_input(AVFilterLink *inlink) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + AVFilterContext *ctx = inlink->dst; + IIRContext *s = ctx->priv; + int ret; + + if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0) + return ret; + + s->depth = desc->comp[0].depth; + s->flt = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT); + + s->in = av_malloc_array(FFALIGN(inlink->w, 64), FFALIGN(inlink->h, 64) * sizeof(*s->in)); + s->out = av_malloc_array(FFALIGN(inlink->w, 64), FFALIGN(inlink->h, 64) * sizeof(*s->out)); + if (!s->in || !s->out) + return AVERROR(ENOMEM); + + s->nb_threads = ff_filter_get_nb_threads(ctx); + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->planewidth[0] = s->planewidth[3] = inlink->w; + s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + s->planeheight[0] = s->planeheight[3] = inlink->h; + + s->nb_planes = av_pix_fmt_count_planes(inlink->format); + + count_coefficients(s->h_num_str, &s->h_nb_num); + count_coefficients(s->h_den_str, &s->h_nb_den); + count_coefficients(s->v_num_str, &s->v_nb_num); + count_coefficients(s->v_den_str, &s->v_nb_den); + + if (!s->h_nb_num || !s->h_nb_den || + !s->v_nb_num || !s->v_nb_den) + return AVERROR(EINVAL); + + s->hnum = av_calloc(s->h_nb_num, sizeof(*s->hnum)); + s->hden = av_calloc(s->h_nb_den, sizeof(*s->hden)); + s->honum = av_calloc(s->nb_threads, sizeof(*s->honum)); + s->hoden = av_calloc(s->nb_threads, sizeof(*s->hoden)); + s->vnum = av_calloc(s->h_nb_num, sizeof(*s->vnum)); + s->vden = av_calloc(s->h_nb_den, sizeof(*s->vden)); + s->vonum = av_calloc(s->nb_threads, sizeof(*s->vonum)); + s->voden = av_calloc(s->nb_threads, sizeof(*s->voden)); + if (!s->hnum || !s->hden || !s->honum || !s->hoden || + !s->vnum || !s->vden || !s->vonum || !s->voden) + return AVERROR(ENOMEM); + + for (int i = 0; i < s->nb_threads; i++) { + s->honum[i] = av_calloc(s->h_nb_num, sizeof(**s->honum)); + s->hoden[i] = av_calloc(s->h_nb_den, sizeof(**s->hoden)); + s->vonum[i] = av_calloc(s->v_nb_num, sizeof(**s->vonum)); + s->voden[i] = av_calloc(s->v_nb_den, sizeof(**s->voden)); + if (!s->honum[i] || !s->hoden[i] || + !s->vonum[i] || !s->voden[i]) + return AVERROR(ENOMEM); + } + + ret = read_coefficients(ctx, s->h_num_str, s->h_nb_num, s->hnum); + if (ret < 0) + return ret; + ret = read_coefficients(ctx, s->h_den_str, s->h_nb_den, s->hden); + if (ret < 0) + return ret; + ret = read_coefficients(ctx, s->v_num_str, s->v_nb_num, s->vnum); + if (ret < 0) + return ret; + ret = read_coefficients(ctx, s->v_den_str, s->v_nb_den, s->vden); + if (ret < 0) + return ret; + + normalize_coefficients(s->hnum, s->h_nb_num, s->hden, s->h_nb_den); + normalize_coefficients(s->vnum, s->v_nb_num, s->vden, s->v_nb_den); + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *in) +{ + AVFilterContext *ctx = inlink->dst; + IIRContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + AVFrame *out; + int plane; + + out = ff_get_video_buffer(outlink, outlink->w, outlink->h); + if (!out) { + av_frame_free(&in); + return AVERROR(ENOMEM); + } + av_frame_copy_props(out, in); + + for (plane = 0; plane < s->nb_planes; plane++) { + const int height = s->planeheight[plane]; + const int width = s->planewidth[plane]; + const uint8_t *src = in->data[plane]; + const uint16_t *src16 = (const uint16_t *)in->data[plane]; + uint8_t *dst = out->data[plane]; + uint16_t *dst16 = (uint16_t *)out->data[plane]; + int fin_linesize; + int fout_linesize; + float *fout, *fin; + + if (!(s->planes & (1 << plane))) { + av_image_copy_plane(out->data[plane], out->linesize[plane], + in->data[plane], in->linesize[plane], + s->linesize[plane], height); + continue; + } + + if (s->flt) { + fin = (float *)in->data[plane]; + fout = (float *)out->data[plane]; + fin_linesize = in->linesize[plane] / 4; + fout_linesize = out->linesize[plane] / 4; + } else { + float *bptr = s->in; + + if (s->depth == 8) { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + bptr[x] = src[x]; + } + bptr += width; + src += in->linesize[plane]; + } + } else { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + bptr[x] = src16[x]; + } + bptr += width; + src16 += in->linesize[plane] / 2; + } + } + + fin = s->in; + fout = s->out; + fin_linesize = width; + fout_linesize = width; + } + + iir2d(ctx, fout, fin, fout_linesize, fin_linesize, + s->planewidth[plane], s->planeheight[plane]); + + if (!s->flt) { + float *bptr = s->out; + + if (s->depth == 8) { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + dst[x] = bptr[x]; + } + bptr += width; + dst += out->linesize[plane]; + } + } else { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + dst16[x] = bptr[x]; + } + bptr += width; + dst16 += out->linesize[plane] / 2; + } + } + } + } + + av_frame_free(&in); + return ff_filter_frame(outlink, out); +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + IIRContext *s = ctx->priv; + + av_freep(&s->in); + av_freep(&s->out); + + av_freep(&s->hnum); + av_freep(&s->hden); + av_freep(&s->vnum); + av_freep(&s->vden); + + for (int i = 0; i < s->nb_threads; i++) { + if (s->honum) + av_freep(&s->honum[i]); + if (s->hoden) + av_freep(&s->hoden[i]); + if (s->vonum) + av_freep(&s->vonum[i]); + if (s->voden) + av_freep(&s->voden[i]); + } + + av_freep(&s->honum); + av_freep(&s->hoden); + av_freep(&s->vonum); + av_freep(&s->voden); +} + +static const AVFilterPad iir_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_input, + .filter_frame = filter_frame, + }, + { NULL } +}; + +static const AVFilterPad iir_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +const AVFilter ff_vf_iir = { + .name = "iir", + .description = NULL_IF_CONFIG_SMALL("Apply 2D Infinite Impulse Response filter with supplied coefficients."), + .priv_size = sizeof(IIRContext), + .priv_class = &iir_class, + .uninit = uninit, + .query_formats = query_formats, + .inputs = iir_inputs, + .outputs = iir_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, +};