From patchwork Sun Apr 23 18:47:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 3479 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.3.129 with SMTP id 123csp1148295vsd; Sun, 23 Apr 2017 11:53:20 -0700 (PDT) X-Received: by 10.223.136.75 with SMTP id e11mr2947868wre.14.1492973600288; Sun, 23 Apr 2017 11:53:20 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s4si11637088wmf.24.2017.04.23.11.53.19; Sun, 23 Apr 2017 11:53:20 -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; 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=NONE 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 C72F56897E6; Sun, 23 Apr 2017 21:53:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com [74.125.82.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E7442688255 for ; Sun, 23 Apr 2017 21:53:08 +0300 (EEST) Received: by mail-wm0-f67.google.com with SMTP id y10so2367607wmh.0 for ; Sun, 23 Apr 2017 11:53:09 -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=dLJKd7TEVoxUZOi7rqKr5GCN3jy80Rx559ZgGHbjxX0=; b=pMZVQlNodzbJCB+a6DQiB5QaPTsGZpdDNuHxFqf6DVxp/oHgsYiEagHe67HNSch8TL 69I20rPcWHb9nD7kpn5Lg3139/CZWrwKZioYA4yetuEg41DYmJ3RflYHIXMq3/JuHgm6 FH/Wd4XNf7cSV687fyAlV4Qi7K88fQySLUobDu5pO/JQ0kEc+acCEKh5c0FpHuLLbXvC PVIrmq09AjVPO0x77LbCLtjWpjEao5d7lNoCKM6Qje0OA+Clk+9GRxQauRHknLZOuhyB 6cWANUa2nYFKEGnbOkgAkDxdYthOSCwPqZIS2zPo5WTUxWR5E5GM+e6jKJbX5aRcpllH gR2w== 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=dLJKd7TEVoxUZOi7rqKr5GCN3jy80Rx559ZgGHbjxX0=; b=qdTIy+qz4XB3nETdhu8KvjWvohT1cpFA30rIEhthMqKbWC5I5Jn0t8B1oohLOy0H4j kqfUqfvprkDLSTN/rRQNHRgo7JMIX6Z03hG2Fn8HUblzh8wAnMZrcM/K/LGuKmf5FeJt DrjSHf6qlPnuiiwl3Vgg0njDhTUymmcrIZwtZ4GWJvkPVwBYWE5fBVVTkBa4k1k96Fvl TZ9A7LpwYddMuYGdlXs/MLnTkjfS2wc2ZdfAoagpNA5Kdk+J4HubSpAjTADTFDRUO/Ru bwmnBOYVSWuFYCdMePrY9MqhljBv/fNnFNDyQjreApk5i01F2pDw2OxwoFyN5qqMIOol W6vA== X-Gm-Message-State: AN3rC/7Qk2TVKzcZIh4udWT4A+xoJGtazQzksns3TV7WZRfXz3pOjziC 9x6KcPi+1d3I1vV4 X-Received: by 10.28.182.69 with SMTP id g66mr7026801wmf.112.1492973264361; Sun, 23 Apr 2017 11:47:44 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id d17sm9901624wmi.21.2017.04.23.11.47.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 23 Apr 2017 11:47:42 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 23 Apr 2017 20:47:27 +0200 Message-Id: <20170423184727.18942-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH] avfilter: add lumakey 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 | 16 +++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_lumakey.c | 174 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 192 insertions(+) create mode 100644 libavfilter/vf_lumakey.c diff --git a/doc/filters.texi b/doc/filters.texi index 4c733f0..d7ac4cf 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -9278,6 +9278,22 @@ Interpolate values using a tetrahedron. @end table @end table +@section lumakey + +Turn certain luma values into transparency. + +The filter accepts the following options: + +@table @option +@item threshold +Set the luma which will be used as base for transparency. +Default value is @code{0}. + +@item tolerance +Set the range of luma values to be keyed out. +Default value is @code{0}. +@end table + @section lut, lutrgb, lutyuv Compute a look-up table for binding each pixel component input value diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 50c5132..074c690 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -212,6 +212,7 @@ OBJS-$(CONFIG_INTERLEAVE_FILTER) += f_interleave.o OBJS-$(CONFIG_KERNDEINT_FILTER) += vf_kerndeint.o OBJS-$(CONFIG_LENSCORRECTION_FILTER) += vf_lenscorrection.o OBJS-$(CONFIG_LOOP_FILTER) += f_loop.o +OBJS-$(CONFIG_LUMAKEY_FILTER) += vf_lumakey.o OBJS-$(CONFIG_LUT_FILTER) += vf_lut.o OBJS-$(CONFIG_LUT2_FILTER) += vf_lut2.o framesync.o OBJS-$(CONFIG_LUT3D_FILTER) += vf_lut3d.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index f482adb..c69f79e 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -223,6 +223,7 @@ static void register_all(void) REGISTER_FILTER(KERNDEINT, kerndeint, vf); REGISTER_FILTER(LENSCORRECTION, lenscorrection, vf); REGISTER_FILTER(LOOP, loop, vf); + REGISTER_FILTER(LUMAKEY, lumakey, vf); REGISTER_FILTER(LUT, lut, vf); REGISTER_FILTER(LUT2, lut2, vf); REGISTER_FILTER(LUT3D, lut3d, vf); diff --git a/libavfilter/vf_lumakey.c b/libavfilter/vf_lumakey.c new file mode 100644 index 0000000..4ab17a9 --- /dev/null +++ b/libavfilter/vf_lumakey.c @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2017 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/opt.h" +#include "libavutil/imgutils.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct LumakeyContext { + const AVClass *class; + + int threshold; + int tolerance; + + int (*do_lumakey_slice)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs); +} LumakeyContext; + +static int do_lumakey_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + LumakeyContext *s = ctx->priv; + AVFrame *frame = arg; + const int slice_start = (frame->height * jobnr) / nb_jobs; + const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs; + uint8_t *alpha = frame->data[3] + slice_start * frame->linesize[3]; + const uint8_t *luma = frame->data[0] + slice_start * frame->linesize[0]; + const int thr = s->threshold; + const int tol = s->tolerance; + int x, y; + + for (y = slice_start; y < slice_end; ++y) { + for (x = 0; x < frame->width; ++x) { + if (luma[x] >= thr - tol && luma[x] <= thr + tol) { + alpha[x] = 0; + } + } + luma += frame->linesize[0]; + alpha += frame->linesize[3]; + } + + return 0; +} + +static int do_lumakey_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + LumakeyContext *s = ctx->priv; + AVFrame *frame = arg; + const int slice_start = (frame->height * jobnr) / nb_jobs; + const int slice_end = (frame->height * (jobnr + 1)) / nb_jobs; + uint16_t *alpha = (uint16_t *)(frame->data[3] + slice_start * frame->linesize[3]); + const uint16_t *luma = (const uint16_t *)(frame->data[0] + slice_start * frame->linesize[0]); + const int thr = s->threshold; + const int tol = s->tolerance; + int x, y; + + for (y = slice_start; y < slice_end; ++y) { + for (x = 0; x < frame->width; ++x) { + if (luma[x] >= thr - tol && luma[x] <= thr + tol) { + alpha[x] = 0; + } + } + luma += frame->linesize[0] / 2; + alpha += frame->linesize[3] / 2; + } + + return 0; +} + +static int config_input(AVFilterLink *inlink) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + AVFilterContext *ctx = inlink->dst; + LumakeyContext *s = ctx->priv; + int depth; + + depth = desc->comp[0].depth; + if (depth == 8) + s->do_lumakey_slice = do_lumakey_slice8; + else + s->do_lumakey_slice = do_lumakey_slice16; + + return 0; +} + +static int filter_frame(AVFilterLink *link, AVFrame *frame) +{ + AVFilterContext *ctx = link->dst; + LumakeyContext *s = ctx->priv; + int ret; + + if (ret = av_frame_make_writable(frame)) + return ret; + + if (ret = ctx->internal->execute(ctx, s->do_lumakey_slice, frame, NULL, FFMIN(frame->height, ff_filter_get_nb_threads(ctx)))) + return ret; + + return ff_filter_frame(ctx->outputs[0], frame); +} + +static av_cold int query_formats(AVFilterContext *ctx) +{ + static const enum AVPixelFormat pixel_fmts[] = { + AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA420P, + AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA420P9, + AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA420P10, + AV_PIX_FMT_YUVA444P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA420P16, + AV_PIX_FMT_NONE + }; + AVFilterFormats *formats; + + formats = ff_make_format_list(pixel_fmts); + if (!formats) + return AVERROR(ENOMEM); + + return ff_set_common_formats(ctx, formats); +} + +static const AVFilterPad lumakey_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad lumakey_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +#define OFFSET(x) offsetof(LumakeyContext, x) +#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption lumakey_options[] = { + { "threshold", "set the threshold value", OFFSET(threshold), AV_OPT_TYPE_INT, {.i64=0}, 0, UINT16_MAX, FLAGS }, + { "tolerance", "set the tolerance value", OFFSET(tolerance), AV_OPT_TYPE_INT, {.i64=1}, 0, UINT16_MAX, FLAGS }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(lumakey); + +AVFilter ff_vf_lumakey = { + .name = "lumakey", + .description = NULL_IF_CONFIG_SMALL("Turns a certain luma into transparency."), + .priv_size = sizeof(LumakeyContext), + .priv_class = &lumakey_class, + .query_formats = query_formats, + .inputs = lumakey_inputs, + .outputs = lumakey_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, +};