From patchwork Sun Jan 31 20:55:59 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: 25303 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 58BB544A53D for ; Sun, 31 Jan 2021 22:56:16 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1EA7B6881BF; Sun, 31 Jan 2021 22:56:16 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f53.google.com (mail-ed1-f53.google.com [209.85.208.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 566D368818D for ; Sun, 31 Jan 2021 22:56:10 +0200 (EET) Received: by mail-ed1-f53.google.com with SMTP id j13so16628463edp.2 for ; Sun, 31 Jan 2021 12:56:10 -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=qOnVjo5DqQv1wuHsIR2YE7HZ0aGCzBYVpWjgy6ZTni0=; b=IRzMgumG5mrJVsqQnYhYoO6040VeW2f3PRX9Z31YxdYN+hs8ua5zQfJn8cpEEpxCXQ iLb64niYnT0CcbFt8SRGgeb4hcFZwuT8wSarT3k+pY1azWmIX4ZUibAZUo1CKZw3UpXZ VDzwOHY7JDnY4ucTon7cWhsylJOmBDYIIoCe4yLgnjOIBnD88vYpwF5a932341ap9fLS n4qbmKILzR5a/YQKPYFKSBN4Jbs6bFv4k+8FRFeUPrNXoHmUS/IgIU/Gl5RHaTS8n65g OwZBLIABPWF98G5Uohi/DnUwpGipsp5e3T892P5+YE4u3uJ0eRY4oaFQfZiVxqZJyeoZ LkFw== 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=qOnVjo5DqQv1wuHsIR2YE7HZ0aGCzBYVpWjgy6ZTni0=; b=cEixSWgWouTaWXsbL1QMzEtEBIpKvkeSzoPGKjHIDbFcf3V8lkJOgod5w9uf8ySrFh D9rDnpadjdaza71G21duunYV8JFa/2dnt3KUJ/8/SzPM5W0uPaExkfTgwdHTxaWToIfn KGlcDo7UvenryhGP7bhFEjYbyTgpavMVeUOTD5SwE43PNhSlsVPeZCTpwxA8dbw/4GQ4 bcdGAZDqvTrnBfg+b+VH5sAbZ1KW4TdaxIj2qbcV1OA9VoVFGfO2Az7Z8cLqOlUD626c gjJ2pvVwyzjptBz2Kx6zj9EUmY8++7zEoom8KswTsHA8plNPxvILPFnFtKlKbGRTMQ3f d/rw== X-Gm-Message-State: AOAM531w17zsLvimcMhF54pXsw6mBS7kAy4ltfBAdT6yoaDAtiKk8lJg Zylr23OQNIlPhYyRHMOrPe5ZZIJcu50IdA== X-Google-Smtp-Source: ABdhPJx28TzQY9IQMko6izex/3n1Uf8pnECrEm1Jd7AwZiZ0vnjK/kDM+Hrnjvl9iNOVfqeuGtmHCQ== X-Received: by 2002:a05:6402:22b7:: with SMTP id cx23mr15663511edb.313.1612126569834; Sun, 31 Jan 2021 12:56:09 -0800 (PST) Received: from localhost.localdomain ([31.45.254.141]) by smtp.gmail.com with ESMTPSA id j18sm6913292ejv.18.2021.01.31.12.56.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Jan 2021 12:56:09 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 31 Jan 2021 21:55:59 +0100 Message-Id: <20210131205600.7835-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/2] avfilter: add colorcontrast 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 | 30 +++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_colorcontrast.c | 408 +++++++++++++++++++++++++++++++++ 4 files changed, 440 insertions(+) create mode 100644 libavfilter/vf_colorcontrast.c diff --git a/doc/filters.texi b/doc/filters.texi index d2f62972dd..490586c063 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -8026,6 +8026,36 @@ colorbalance=rs=.3 This filter supports the all above options as @ref{commands}. +@section colorcontrast + +Adjust color contrast between RGB components. + +The filter accepts the following options: + +@table @option +@item rc +Set the red-cyan contrast. Defaults is 0.0. Allowed range is from -1.0 to 1.0. + +@item gm +Set the green-magenta contrast. Defaults is 0.0. Allowed range is from -1.0 to 1.0. + +@item by +Set the blue-yellow contrast. Defaults is 0.0. Allowed range is from -1.0 to 1.0. + +@item rcw +@item gmw +@item byw +Set the weight of each @code{rc}, @code{gm}, @code{by} option value. Default value is 0.0. +Allowed range is from 0.0 to 1.0. If all weights are 0.0 filtering is disabled. + +@item pl +Set the amount of preserving lightness. Default value is 0.0. Allowed range is from 0.0 to 1.0. +@end table + +@subsection Commands + +This filter supports the all above options as @ref{commands}. + @section colorchannelmixer Adjust video input frames by re-mixing color channels. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 7bb6695f13..3a4d3d8e31 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -196,6 +196,7 @@ OBJS-$(CONFIG_CIESCOPE_FILTER) += vf_ciescope.o OBJS-$(CONFIG_CODECVIEW_FILTER) += vf_codecview.o qp_table.o OBJS-$(CONFIG_COLORBALANCE_FILTER) += vf_colorbalance.o OBJS-$(CONFIG_COLORCHANNELMIXER_FILTER) += vf_colorchannelmixer.o +OBJS-$(CONFIG_COLORCONTRAST_FILTER) += vf_colorcontrast.o OBJS-$(CONFIG_COLORKEY_FILTER) += vf_colorkey.o OBJS-$(CONFIG_COLORKEY_OPENCL_FILTER) += vf_colorkey_opencl.o opencl.o \ opencl/colorkey.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 9fccc9da70..6d269b48bf 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -186,6 +186,7 @@ extern AVFilter ff_vf_ciescope; extern AVFilter ff_vf_codecview; extern AVFilter ff_vf_colorbalance; extern AVFilter ff_vf_colorchannelmixer; +extern AVFilter ff_vf_colorcontrast; extern AVFilter ff_vf_colorkey; extern AVFilter ff_vf_colorkey_opencl; extern AVFilter ff_vf_colorhold; diff --git a/libavfilter/vf_colorcontrast.c b/libavfilter/vf_colorcontrast.c new file mode 100644 index 0000000000..7206c954bf --- /dev/null +++ b/libavfilter/vf_colorcontrast.c @@ -0,0 +1,408 @@ +/* + * 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 "avfilter.h" +#include "drawutils.h" +#include "formats.h" +#include "internal.h" +#include "video.h" + +#define R 0 +#define G 1 +#define B 2 + +typedef struct ColorContrastContext { + const AVClass *class; + + float rc, gm, by; + float rcw, gmw, byw; + float preserve; + + int step; + int depth; + uint8_t rgba_map[4]; + + int (*do_slice)(AVFilterContext *s, void *arg, + int jobnr, int nb_jobs); +} ColorContrastContext; + +static inline float lerpf(float v0, float v1, float f) +{ + return v0 + (v1 - v0) * f; +} + +#define PROCESS(max) \ + br = (b + r) * 0.5f; \ + gb = (g + b) * 0.5f; \ + rg = (r + g) * 0.5f; \ + \ + gd = g - br; \ + bd = b - rg; \ + rd = r - gb; \ + \ + g0 = g + gd * gm; \ + b0 = b - gd * gm; \ + r0 = r - gd * gm; \ + \ + g1 = g - bd * by; \ + b1 = b + bd * by; \ + r1 = r - bd * by; \ + \ + g2 = g - rd * rc; \ + b2 = b - rd * rc; \ + r2 = r + rd * rc; \ + \ + ng = av_clipf((g0 * gmw + g1 * byw + g2 * rcw) * scale, 0.f, max); \ + nb = av_clipf((b0 * gmw + b1 * byw + b2 * rcw) * scale, 0.f, max); \ + nr = av_clipf((r0 * gmw + r1 * byw + r2 * rcw) * scale, 0.f, max); \ + \ + li = FFMAX3(r, g, b) + FFMIN3(r, g, b); \ + lo = FFMAX3(nr, ng, nb) + FFMIN3(nr, ng, nb) + FLT_EPSILON; \ + lf = li / lo; \ + \ + r = nr * lf; \ + g = ng * lf; \ + b = nb * lf; \ + \ + nr = lerpf(nr, r, preserve); \ + ng = lerpf(ng, g, preserve); \ + nb = lerpf(nb, b, preserve); + +static int colorcontrast_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ColorContrastContext *s = ctx->priv; + AVFrame *frame = arg; + const int width = frame->width; + const int height = frame->height; + const int slice_start = (height * jobnr) / nb_jobs; + const int slice_end = (height * (jobnr + 1)) / nb_jobs; + const int glinesize = frame->linesize[0]; + const int blinesize = frame->linesize[1]; + const int rlinesize = frame->linesize[2]; + uint8_t *gptr = frame->data[0] + slice_start * glinesize; + uint8_t *bptr = frame->data[1] + slice_start * blinesize; + uint8_t *rptr = frame->data[2] + slice_start * rlinesize; + const float preserve = s->preserve; + const float gm = s->gm * 0.5f; + const float by = s->by * 0.5f; + const float rc = s->rc * 0.5f; + const float gmw = s->gmw; + const float byw = s->byw; + const float rcw = s->rcw; + const float sum = gmw + byw + rcw; + const float scale = 1.f / sum; + + for (int y = slice_start; y < slice_end && sum > FLT_EPSILON; y++) { + for (int x = 0; x < width; x++) { + float g = gptr[x]; + float b = bptr[x]; + float r = rptr[x]; + float g0, g1, g2; + float b0, b1, b2; + float r0, r1, r2; + float gd, bd, rd; + float gb, br, rg; + float nr, ng, nb; + float li, lo, lf; + + PROCESS(255.f); + + gptr[x] = av_clip_uint8(ng); + bptr[x] = av_clip_uint8(nb); + rptr[x] = av_clip_uint8(nr); + } + + gptr += glinesize; + bptr += blinesize; + rptr += rlinesize; + } + + return 0; +} + +static int colorcontrast_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ColorContrastContext *s = ctx->priv; + AVFrame *frame = arg; + const int depth = s->depth; + const float max = (1 << depth) - 1; + const int width = frame->width; + const int height = frame->height; + const int slice_start = (height * jobnr) / nb_jobs; + const int slice_end = (height * (jobnr + 1)) / nb_jobs; + const int glinesize = frame->linesize[0] / 2; + const int blinesize = frame->linesize[1] / 2; + const int rlinesize = frame->linesize[2] / 2; + uint16_t *gptr = (uint16_t *)frame->data[0] + slice_start * glinesize; + uint16_t *bptr = (uint16_t *)frame->data[1] + slice_start * blinesize; + uint16_t *rptr = (uint16_t *)frame->data[2] + slice_start * rlinesize; + const float preserve = s->preserve; + const float gm = s->gm * 0.5f; + const float by = s->by * 0.5f; + const float rc = s->rc * 0.5f; + const float gmw = s->gmw; + const float byw = s->byw; + const float rcw = s->rcw; + const float sum = gmw + byw + rcw; + const float scale = 1.f / sum; + + for (int y = slice_start; y < slice_end && sum > FLT_EPSILON; y++) { + for (int x = 0; x < width; x++) { + float g = gptr[x]; + float b = bptr[x]; + float r = rptr[x]; + float g0, g1, g2; + float b0, b1, b2; + float r0, r1, r2; + float gd, bd, rd; + float gb, br, rg; + float nr, ng, nb; + float li, lo, lf; + + PROCESS(max); + + gptr[x] = av_clip_uintp2_c(ng, depth); + bptr[x] = av_clip_uintp2_c(nb, depth); + rptr[x] = av_clip_uintp2_c(nr, depth); + } + + gptr += glinesize; + bptr += blinesize; + rptr += rlinesize; + } + + return 0; +} + +static int colorcontrast_slice8p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ColorContrastContext *s = ctx->priv; + AVFrame *frame = arg; + const int step = s->step; + const int width = frame->width; + const int height = frame->height; + const int slice_start = (height * jobnr) / nb_jobs; + const int slice_end = (height * (jobnr + 1)) / nb_jobs; + const int linesize = frame->linesize[0]; + const uint8_t roffset = s->rgba_map[R]; + const uint8_t goffset = s->rgba_map[G]; + const uint8_t boffset = s->rgba_map[B]; + uint8_t *ptr = frame->data[0] + slice_start * linesize; + const float preserve = s->preserve; + const float gm = s->gm * 0.5f; + const float by = s->by * 0.5f; + const float rc = s->rc * 0.5f; + const float gmw = s->gmw; + const float byw = s->byw; + const float rcw = s->rcw; + const float sum = gmw + byw + rcw; + const float scale = 1.f / sum; + + for (int y = slice_start; y < slice_end && sum > FLT_EPSILON; y++) { + for (int x = 0; x < width; x++) { + float g = ptr[x * step + goffset]; + float b = ptr[x * step + boffset]; + float r = ptr[x * step + roffset]; + float g0, g1, g2; + float b0, b1, b2; + float r0, r1, r2; + float gd, bd, rd; + float gb, br, rg; + float nr, ng, nb; + float li, lo, lf; + + PROCESS(255.f); + + ptr[x * step + goffset] = av_clip_uint8(ng); + ptr[x * step + boffset] = av_clip_uint8(nb); + ptr[x * step + roffset] = av_clip_uint8(nr); + } + + ptr += linesize; + } + + return 0; +} + +static int colorcontrast_slice16p(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ColorContrastContext *s = ctx->priv; + AVFrame *frame = arg; + const int step = s->step; + const int depth = s->depth; + const float max = (1 << depth) - 1; + const int width = frame->width; + const int height = frame->height; + const int slice_start = (height * jobnr) / nb_jobs; + const int slice_end = (height * (jobnr + 1)) / nb_jobs; + const int linesize = frame->linesize[0] / 2; + const uint8_t roffset = s->rgba_map[R]; + const uint8_t goffset = s->rgba_map[G]; + const uint8_t boffset = s->rgba_map[B]; + uint16_t *ptr = (uint16_t *)frame->data[0] + slice_start * linesize; + const float preserve = s->preserve; + const float gm = s->gm * 0.5f; + const float by = s->by * 0.5f; + const float rc = s->rc * 0.5f; + const float gmw = s->gmw; + const float byw = s->byw; + const float rcw = s->rcw; + const float sum = gmw + byw + rcw; + const float scale = 1.f / sum; + + for (int y = slice_start; y < slice_end && sum > FLT_EPSILON; y++) { + for (int x = 0; x < width; x++) { + float g = ptr[x * step + goffset]; + float b = ptr[x * step + boffset]; + float r = ptr[x * step + roffset]; + float g0, g1, g2; + float b0, b1, b2; + float r0, r1, r2; + float gd, bd, rd; + float gb, br, rg; + float nr, ng, nb; + float li, lo, lf; + + PROCESS(max); + + ptr[x * step + goffset] = av_clip_uintp2_c(ng, depth); + ptr[x * step + boffset] = av_clip_uintp2_c(nb, depth); + ptr[x * step + roffset] = av_clip_uintp2_c(nr, depth); + } + + ptr += linesize; + } + + return 0; +} + +static int filter_frame(AVFilterLink *link, AVFrame *frame) +{ + AVFilterContext *ctx = link->dst; + ColorContrastContext *s = ctx->priv; + int res; + + if (res = ctx->internal->execute(ctx, s->do_slice, frame, NULL, + FFMIN(frame->height, ff_filter_get_nb_threads(ctx)))) + return res; + + 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_RGB24, AV_PIX_FMT_BGR24, + AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA, + AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR, + AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR, + AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0, + AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP, + AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, + AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, + AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, + AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48, + AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64, + AV_PIX_FMT_NONE + }; + + AVFilterFormats *formats = NULL; + + formats = ff_make_format_list(pixel_fmts); + if (!formats) + return AVERROR(ENOMEM); + + return ff_set_common_formats(ctx, formats); +} + +static av_cold int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + ColorContrastContext *s = ctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + int planar = desc->flags & AV_PIX_FMT_FLAG_PLANAR; + + s->step = desc->nb_components; + if (inlink->format == AV_PIX_FMT_RGB0 || + inlink->format == AV_PIX_FMT_0RGB || + inlink->format == AV_PIX_FMT_BGR0 || + inlink->format == AV_PIX_FMT_0BGR) + s->step = 4; + + s->depth = desc->comp[0].depth; + s->do_slice = s->depth <= 8 ? colorcontrast_slice8 : colorcontrast_slice16; + if (!planar) + s->do_slice = s->depth <= 8 ? colorcontrast_slice8p : colorcontrast_slice16p; + + ff_fill_rgba_map(s->rgba_map, inlink->format); + + return 0; +} + +static const AVFilterPad colorcontrast_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .needs_writable = 1, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad colorcontrast_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +#define OFFSET(x) offsetof(ColorContrastContext, x) +#define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM + +static const AVOption colorcontrast_options[] = { + { "rc", "set the red-cyan contrast", OFFSET(rc), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF }, + { "gm", "set the green-magenta contrast", OFFSET(gm), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF }, + { "by", "set the blue-yellow contrast", OFFSET(by), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF }, + { "rcw", "set the red-cyan weight", OFFSET(rcw), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, VF }, + { "gmw", "set the green-magenta weight", OFFSET(gmw), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, VF }, + { "byw", "set the blue-yellow weight", OFFSET(byw), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, VF }, + { "pl", "set the amount of preserving lightness", OFFSET(preserve), AV_OPT_TYPE_FLOAT, {.dbl=0}, 0, 1, VF }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(colorcontrast); + +AVFilter ff_vf_colorcontrast = { + .name = "colorcontrast", + .description = NULL_IF_CONFIG_SMALL("Adjust color contrast between RGB components."), + .priv_size = sizeof(ColorContrastContext), + .priv_class = &colorcontrast_class, + .query_formats = query_formats, + .inputs = colorcontrast_inputs, + .outputs = colorcontrast_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, + .process_command = ff_filter_process_command, +};