From patchwork Sat Aug 21 12:59:20 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: 29668 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2a4a:0:0:0:0 with SMTP id k10csp501934iov; Sat, 21 Aug 2021 05:59:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysjUYKQpfujJTabqEU3radYwANiTDg9oAg/YTfAC0dTvxSOWghTWMlB+1PcTVrLezM/65h X-Received: by 2002:a05:6402:1385:: with SMTP id b5mr27423709edv.276.1629550779108; Sat, 21 Aug 2021 05:59:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629550779; cv=none; d=google.com; s=arc-20160816; b=Jm9t3XIe/RDu76ROhr8+KG4zl6GKjyyHG7kVZs9VwwGfc+iKGP5YpykwgUjZD0Rr+z wzoaDYgQRI+pXUmcVb+Wu1Pblusfp3syfW60HNa3qb/qUjSeXtNPABuqRYV+/QDOZs5I ZlMtvoXZFTgaOGaaEUKuI4/hWdoUjFK20hUew6IkBy+RCmlCnHyvENYUz9l9geqciBJn fy3mAEzXSdxec4DsRDVeThtuMjrUUmUDN1GFbCy8v4W4XCWkJwS3apRILL3QWMQitqmc GCO4IErf8eKdHNXqw1FqU4XqPb1nwpdUgmCZ43xBEr0r8udNCxGq6pK/YyD8EQTmMLpz rynQ== 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=M4p9/WCwirn4K0Z9QWQwFVkSU30wVC0aWRePWQAm+AI=; b=supQ1JADguJfLKp4T3WScZkYidnDYZZEdsehpc+/WuimRQeormi1jid3JR1n4YlNlB Fszymf6Gl7ekFGih0sHpzsDRs0W+fkAlR3dBmtn6Tii8GtxiafGnY4ZCTccme3YJT1F/ DVV9jX7ZQm9V867/ItuYQ0yLqtShLogaH59klGv2xvOfCypK1dlzH9CnNMUmkia7JI+i KdFAHUX0gki/rnlyyuSfO/gVhQmbVGG5m9TZ2Z5cslfx/yclu/QyTNhAjo9tENA3gEEH 707CtwIU058MpnezOm58a5qT0VZzUVtTE65ZmjKps4Mo5gSSlLXxnZNO6PtYzkNG8YMV jsVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=ueG2iYS4; 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 au25si12481259ejc.642.2021.08.21.05.59.38; Sat, 21 Aug 2021 05:59:39 -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=ueG2iYS4; 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 AAA1D689AEE; Sat, 21 Aug 2021 15:59:35 +0300 (EEST) 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 C80D86880FC for ; Sat, 21 Aug 2021 15:59:28 +0300 (EEST) Received: by mail-ed1-f43.google.com with SMTP id bo19so18157604edb.9 for ; Sat, 21 Aug 2021 05:59:28 -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=RXIHYsBOC/4iZUS6HkEkQYbGGMPb/nhICc58yEb1pyE=; b=ueG2iYS4tUj3WeGiVH8L3gMVke/csNaROueErW/4P51XjD443Ab1seRPfITcwdgy0a +6DWgqvn8DRobvwAx5Tx0nAhrRd8GkZ5udu7sjTrPj98bbCq2pe5jrHwyDvMCm5s+MXL Pl7pYN9cIQ5hcBLlPh4XV0w5JCTordBd1ntrwqgPXXsZdcvfIrGY/rV9kqmuXjMyZMB3 lBIZaFzFVViJSbH+fXP6H0nJrZ/wM/KKSLJK6L4wp+1+ZmGaAhMn9ChYaCWv0GruHQq2 w1DvKuRs+cAF6i1U9Hi3uYNM1NDdZXj4JVZXy0D1Rz0w4n4hYTIkkpVJ9YPURMmmlmrG bjWA== 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=RXIHYsBOC/4iZUS6HkEkQYbGGMPb/nhICc58yEb1pyE=; b=DE2Upfy2VwEAc+Xe1Lfbahvr83VGWEsspL0yY2CedZdp1HazhIXv5l+mcKPU/eG1R8 rOxGws2FJz6d+xTIanoiv7DeCvskzu/CHA9vk7tPFbbTv/ScvixH5qSLTDBUxghdFNGz 6LykbRx1evh3ZEZoNr6WEpQfjF/A81esINrLbFDvzA7WADfQhzwWyYb/POW1s1toiFhd Xhv2R/P3uDHQwmSTwSlz51EIYyVWNz1xzBSNIlPTYag4ydIJ1xbMJ1+pXhjTD94nKdzr DLizLD99GYxYE6k6vczcuqbRf4R1UZE3Rc44/F4MnyDBIqwGrs2dae6fQf0ysj5sRSE9 pcCA== X-Gm-Message-State: AOAM533+BnprmTUoomnbfORfPCbayQq3Po8BZek5tlKAL5L7GLQn97B5 ePQ4FIF6DBsvnPBAcbhKdFuo8ViK4so= X-Received: by 2002:a50:d795:: with SMTP id w21mr27846229edi.112.1629550768146; Sat, 21 Aug 2021 05:59:28 -0700 (PDT) Received: from localhost.localdomain ([95.168.121.40]) by smtp.gmail.com with ESMTPSA id u20sm4322214ejz.87.2021.08.21.05.59.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Aug 2021 05:59:27 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 21 Aug 2021 14:59:20 +0200 Message-Id: <20210821125920.23148-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add hsvkey and hsvhold video filters 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: 0lJr2Q/+YU/V Signed-off-by: Paul B Mahol --- doc/filters.texi | 76 ++++++++ libavfilter/Makefile | 2 + libavfilter/allfilters.c | 2 + libavfilter/vf_hsvkey.c | 380 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 460 insertions(+) create mode 100644 libavfilter/vf_hsvkey.c diff --git a/doc/filters.texi b/doc/filters.texi index 480cab706c..fe7c2dfba8 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -13511,6 +13511,82 @@ If set to 1, force the output to terminate when the shortest input terminates. Default value is 0. @end table +@section hsvhold +Turns a certain HSV range into gray values. + +This filter measures color difference between set HSV color in options +and ones measured in video stream. Depending on options, output +colors can be changed to be gray or not. + +The filter accepts the following options: + +@table @option +@item hue +Set the hue value which will be used if color difference calculation. +Allowed range is from 0 to 360. Default value is 0. + +@item sat +Set the saturation value which will be used if color difference calculation. +Allowed range is from 0 to 1. Default value is 0. + +@item val +Set the value which will be used if color difference calculation. +Allowed range is from 0 to 1. Default value is 0. + +@item similarity +Set similarity percentage with the key color. +Allowed range is from 0 to 1. Default value is 0.01. + +0.00001 matches only the exact key color, while 1.0 matches everything. + +@item blend +Blend percentage. +Allowed range is from 0 to 1. Default value is 0. + +0.0 makes pixels either fully gray, or not gray at all. + +Higher values result in more gray pixels, with a higher gray pixel +the more similar the pixels color is to the key color. +@end table + +@section hsvkey +Turns a certain HSV range into transparency. + +This filter measures color difference between set HSV color in options +and ones measured in video stream. Depending on options, output +colors can be changed to transparent by adding alpha channel. + +The filter accepts the following options: + +@table @option +@item hue +Set the hue value which will be used if color difference calculation. +Allowed range is from 0 to 360. Default value is 0. + +@item sat +Set the saturation value which will be used if color difference calculation. +Allowed range is from 0 to 1. Default value is 0. + +@item val +Set the value which will be used if color difference calculation. +Allowed range is from 0 to 1. Default value is 0. + +@item similarity +Set similarity percentage with the key color. +Allowed range is from 0 to 1. Default value is 0.01. + +0.00001 matches only the exact key color, while 1.0 matches everything. + +@item blend +Blend percentage. +Allowed range is from 0 to 1. Default value is 0. + +0.0 makes pixels either fully transparent, or not transparent at all. + +Higher values result in semi-transparent pixels, with a higher transparency +the more similar the pixels color is to the key color. +@end table + @section hue Modify the hue and/or the saturation of the input. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 102ce7beff..3166e70b58 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -296,6 +296,8 @@ OBJS-$(CONFIG_HISTOGRAM_FILTER) += vf_histogram.o OBJS-$(CONFIG_HQDN3D_FILTER) += vf_hqdn3d.o OBJS-$(CONFIG_HQX_FILTER) += vf_hqx.o OBJS-$(CONFIG_HSTACK_FILTER) += vf_stack.o framesync.o +OBJS-$(CONFIG_HSVHOLD_FILTER) += vf_hsvkey.o +OBJS-$(CONFIG_HSVKEY_FILTER) += vf_hsvkey.o OBJS-$(CONFIG_HUE_FILTER) += vf_hue.o OBJS-$(CONFIG_HWDOWNLOAD_FILTER) += vf_hwdownload.o OBJS-$(CONFIG_HWMAP_FILTER) += vf_hwmap.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 73040d2824..c49c831203 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -281,6 +281,8 @@ extern const AVFilter ff_vf_histogram; extern const AVFilter ff_vf_hqdn3d; extern const AVFilter ff_vf_hqx; extern const AVFilter ff_vf_hstack; +extern const AVFilter ff_vf_hsvhold; +extern const AVFilter ff_vf_hsvkey; extern const AVFilter ff_vf_hue; extern const AVFilter ff_vf_hwdownload; extern const AVFilter ff_vf_hwmap; diff --git a/libavfilter/vf_hsvkey.c b/libavfilter/vf_hsvkey.c new file mode 100644 index 0000000000..ec6af0862d --- /dev/null +++ b/libavfilter/vf_hsvkey.c @@ -0,0 +1,380 @@ +/* + * 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 + +#include "libavutil/opt.h" +#include "libavutil/imgutils.h" +#include "libavutil/intreadwrite.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct HSVKeyContext { + const AVClass *class; + + float hue, hue_opt, sat, val; + float similarity; + float blend; + + float scale; + + float half; + + int depth; + int max; + + int hsub_log2; + int vsub_log2; + + int (*do_slice)(AVFilterContext *ctx, void *arg, + int jobnr, int nb_jobs); +} HSVKeyContext; + +#define SQR(x) ((x)*(x)) + +static int do_hsvkey_pixel(HSVKeyContext *s, int y, int u, int v, + float hue_key, float sat_key, float val_key) +{ + const float similarity = s->similarity; + const float scale = s->scale; + const float blend = s->blend; + const int imax = s->max; + const float max = imax; + const float half = s->half; + const float uf = u - half; + const float vf = v - half; + const float hue = atan2f(uf, vf) + M_PI; + const float sat = sqrtf((uf * uf + vf * vf) / (half * half * 2.f)); + const float val = y * scale; + float diff; + + diff = sqrtf(SQR(sat) * SQR(val) + + SQR(sat_key) * SQR(val_key) - + 2.f * sat * val * sat_key * val_key * cosf(hue_key - hue) + + SQR(val - val_key)); + if (diff < similarity) { + return 0; + } else if (blend > 0.000001f) { + return av_clipf((diff - similarity) / blend, 0.f, 1.f) * max; + } else { + return imax; + } + + return 0; +} + +static int do_hsvkey_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs) +{ + HSVKeyContext *s = avctx->priv; + AVFrame *frame = arg; + const int slice_start = (frame->height * jobnr) / nb_jobs; + const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs; + const int hsub_log2 = s->hsub_log2; + const int vsub_log2 = s->vsub_log2; + const float hue = s->hue; + const float sat = s->sat; + const float val = s->val; + + for (int y = slice_start; y < slice_end; y++) { + for (int x = 0; x < frame->width; x++) { + int Y = frame->data[0][frame->linesize[0] * y + x]; + int u = frame->data[1][frame->linesize[1] * (y >> vsub_log2) + (x >> hsub_log2)]; + int v = frame->data[2][frame->linesize[2] * (y >> vsub_log2) + (x >> hsub_log2)]; + + frame->data[3][frame->linesize[3] * y + x] = do_hsvkey_pixel(s, Y, u, v, hue, sat, val); + } + } + + return 0; +} + +static int do_hsvkey16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs) +{ + HSVKeyContext *s = avctx->priv; + AVFrame *frame = arg; + const int slice_start = (frame->height * jobnr) / nb_jobs; + const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs; + const int hsub_log2 = s->hsub_log2; + const int vsub_log2 = s->vsub_log2; + const float hue = s->hue; + const float sat = s->sat; + const float val = s->val; + + for (int y = slice_start; y < slice_end; ++y) { + for (int x = 0; x < frame->width; ++x) { + uint16_t *dst = (uint16_t *)(frame->data[3] + frame->linesize[3] * y); + int Y = AV_RN16(&frame->data[0][frame->linesize[0] * y + 2 * x]); + int u = AV_RN16(&frame->data[1][frame->linesize[1] * (y >> vsub_log2) + 2 * (x >> hsub_log2)]); + int v = AV_RN16(&frame->data[2][frame->linesize[2] * (y >> vsub_log2) + 2 * (x >> hsub_log2)]); + + dst[x] = do_hsvkey_pixel(s, Y, u, v, hue, sat, val); + } + } + + return 0; +} + +static int do_hsvhold_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs) +{ + HSVKeyContext *s = avctx->priv; + AVFrame *frame = arg; + const int hsub_log2 = s->hsub_log2; + const int vsub_log2 = s->vsub_log2; + const int width = frame->width >> hsub_log2; + const int height = frame->height >> vsub_log2; + const int slice_start = (height * jobnr) / nb_jobs; + const int slice_end = (height * (jobnr + 1)) / nb_jobs; + const float scale = s->scale; + const float hue = s->hue; + const float sat = s->sat; + const float val = s->val; + + for (int y = slice_start; y < slice_end; ++y) { + for (int x = 0; x < width; ++x) { + uint8_t *dstu = frame->data[1] + frame->linesize[1] * y; + uint8_t *dstv = frame->data[2] + frame->linesize[2] * y; + int Y = frame->data[0][frame->linesize[0] * (y << vsub_log2) + (x << hsub_log2)]; + int u = frame->data[1][frame->linesize[1] * y + x]; + int v = frame->data[2][frame->linesize[2] * y + x]; + int t = do_hsvkey_pixel(s, Y, u, v, hue, sat, val); + + if (t > 0) { + float f = 1.f - t * scale; + + dstu[x] = 128 + (u - 128) * f; + dstv[x] = 128 + (v - 128) * f; + } + } + } + + return 0; +} + +static int do_hsvhold16_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs) +{ + HSVKeyContext *s = avctx->priv; + AVFrame *frame = arg; + const int hsub_log2 = s->hsub_log2; + const int vsub_log2 = s->vsub_log2; + const int width = frame->width >> hsub_log2; + const int height = frame->height >> vsub_log2; + const int slice_start = (height * jobnr) / nb_jobs; + const int slice_end = (height * (jobnr + 1)) / nb_jobs; + const float scale = s->scale; + const float half = s->half; + const float hue = s->hue; + const float sat = s->sat; + const float val = s->val; + + for (int y = slice_start; y < slice_end; ++y) { + for (int x = 0; x < width; ++x) { + uint16_t *dstu = (uint16_t *)(frame->data[1] + frame->linesize[1] * y); + uint16_t *dstv = (uint16_t *)(frame->data[2] + frame->linesize[2] * y); + int Y = AV_RN16(&frame->data[0][frame->linesize[0] * (y << vsub_log2) + 2 * (x << hsub_log2)]); + int u = AV_RN16(&frame->data[1][frame->linesize[1] * y + 2 * x]); + int v = AV_RN16(&frame->data[2][frame->linesize[2] * y + 2 * x]); + int t = do_hsvkey_pixel(s, Y, u, v, hue, sat, val); + + if (t > 0) { + float f = 1.f - t * scale; + + dstu[x] = half + (u - half) * f; + dstv[x] = half + (v - half) * f; + } + } + } + + return 0; +} + +static int filter_frame(AVFilterLink *link, AVFrame *frame) +{ + AVFilterContext *avctx = link->dst; + HSVKeyContext *s = avctx->priv; + int res; + + s->hue = M_PI * fmodf(526.f - s->hue_opt, 360.f) / 180.f; + if (res = ff_filter_execute(avctx, s->do_slice, frame, NULL, + FFMIN(frame->height, ff_filter_get_nb_threads(avctx)))) + return res; + + return ff_filter_frame(avctx->outputs[0], frame); +} + +static av_cold int config_output(AVFilterLink *outlink) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format); + AVFilterContext *avctx = outlink->src; + HSVKeyContext *s = avctx->priv; + + s->depth = desc->comp[0].depth; + s->max = (1 << s->depth) - 1; + s->half = 0.5f * s->max; + s->scale = 1.f / s->max; + + if (!strcmp(avctx->filter->name, "hsvkey")) { + s->do_slice = s->depth <= 8 ? do_hsvkey_slice : do_hsvkey16_slice; + } else { + s->do_slice = s->depth <= 8 ? do_hsvhold_slice: do_hsvhold16_slice; + } + + return 0; +} + +static av_cold int query_formats(AVFilterContext *avctx) +{ + static const enum AVPixelFormat pixel_fmts[] = { + AV_PIX_FMT_YUVA420P, + AV_PIX_FMT_YUVA422P, + AV_PIX_FMT_YUVA444P, + 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_NONE + }; + + static const enum AVPixelFormat hold_pixel_fmts[] = { + AV_PIX_FMT_YUV420P, + AV_PIX_FMT_YUV422P, + AV_PIX_FMT_YUV444P, + AV_PIX_FMT_YUVA420P, + AV_PIX_FMT_YUVA422P, + AV_PIX_FMT_YUVA444P, + 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_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12, + AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14, + 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_NONE + }; + const enum AVPixelFormat *pix_fmts; + + pix_fmts = !strcmp(avctx->filter->name, "hsvhold") ? hold_pixel_fmts : pixel_fmts; + + return ff_set_common_formats_from_list(avctx, pix_fmts); +} + +static av_cold int config_input(AVFilterLink *inlink) +{ + AVFilterContext *avctx = inlink->dst; + HSVKeyContext *s = avctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + + s->hsub_log2 = desc->log2_chroma_w; + s->vsub_log2 = desc->log2_chroma_h; + + return 0; +} + +static const AVFilterPad hsvkey_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad hsvkey_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, + { NULL } +}; + +#define OFFSET(x) offsetof(HSVKeyContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM + +static const AVOption hsvkey_options[] = { + { "hue", "set the hue value", OFFSET(hue_opt), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, 360, FLAGS }, + { "sat", "set the saturation value", OFFSET(sat), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, 1, FLAGS }, + { "val", "set the value value", OFFSET(val), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, 1, FLAGS }, + { "similarity", "set the hsvkey similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01}, 0.00001, 1.0, FLAGS }, + { "blend", "set the hsvkey blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(hsvkey); + +const AVFilter ff_vf_hsvkey = { + .name = "hsvkey", + .description = NULL_IF_CONFIG_SMALL("Turns a certain HSV range into transparency. Operates on YUV colors."), + .priv_size = sizeof(HSVKeyContext), + .priv_class = &hsvkey_class, + .query_formats = query_formats, + .inputs = hsvkey_inputs, + .outputs = hsvkey_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, + .process_command = ff_filter_process_command, +}; + +static const AVOption hsvhold_options[] = { + { "hue", "set the hue value", OFFSET(hue_opt), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, 360, FLAGS }, + { "sat", "set the saturation value", OFFSET(sat), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, 1, FLAGS }, + { "val", "set the value value", OFFSET(val), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, 0, 1, FLAGS }, + { "similarity", "set the hsvhold similarity value", OFFSET(similarity), AV_OPT_TYPE_FLOAT, { .dbl = 0.01 }, 0.00001, 1.0, FLAGS }, + { "blend", "set the hsvhold blend value", OFFSET(blend), AV_OPT_TYPE_FLOAT, { .dbl = 0.0 }, 0.0, 1.0, FLAGS }, + { NULL } +}; + +static const AVFilterPad hsvhold_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad hsvhold_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .config_props = config_output, + }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(hsvhold); + +const AVFilter ff_vf_hsvhold = { + .name = "hsvhold", + .description = NULL_IF_CONFIG_SMALL("Turns a certain HSV range into gray."), + .priv_size = sizeof(HSVKeyContext), + .priv_class = &hsvhold_class, + .query_formats = query_formats, + .inputs = hsvhold_inputs, + .outputs = hsvhold_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, + .process_command = ff_filter_process_command, +};