From patchwork Mon Feb 1 16:52:09 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: 25319 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 9119444B112 for ; Mon, 1 Feb 2021 19:20:17 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 677A068029E; Mon, 1 Feb 2021 19:20:17 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BE47668029E for ; Mon, 1 Feb 2021 19:20:11 +0200 (EET) Received: by mail-lj1-f177.google.com with SMTP id f2so20562829ljp.11 for ; Mon, 01 Feb 2021 09:20:11 -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=RNfPPA0J++1upBICfeHbFVClh2AYZZXuFHXkfzufMK4=; b=KvSS/hhltdwdL3gcZw+va+iQ+NsU6Z7jHGib9k4MInYW1dlo9KYlDvcByXi9vfLn47 p3GVviQKPGqcl0+Izn+z97kNOHwjOQXlZthVYQv1qmEgghCrZ8MBLcwSKDvOqhoRdIlo gFRto6+3bMoBmyKZ+Ef56knlsVYFNd1vrPaC679fzExW1NMm9+QfQ60tEAMC/8gVMvvb qUtq3BrazOj6jnGYFAAijV6TTQhGm1Qxgx0gvzQ8cfp6MHZov3IbriSGsqMcLWPTYStw cUoBz9fgSNc22QL89XKWmajwVK1or80afzY7Jlkj/uMLh3IkCZkjabGxkr3bW8XZLUnf nO4A== 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=RNfPPA0J++1upBICfeHbFVClh2AYZZXuFHXkfzufMK4=; b=m1ColuEV+gnceu0YXnvkMnHtrtdzKDq3KCZWFCYYJ7/w1P3g2ywLRe9hRhKU5c11dX s/5Ifyfeseagz63B6nlve/Sih0tE4e5xzxdRSz2TnGXs6MgN4NKi56aNFC+2VNfwcG4a lgPQef7jf7XqCtgGcEi7M6erELcmSIl2i1RFHUldPAPASlynqAgrZbPtwl+/9BXtRi20 EV34jDZecxLrAd7m6zgwuPvdsOKDT29i7olp5cbNalKwPaoDRtTVuvtriE0LcXeCmHhS XJRZFuLsiAbzOXPfpQl0UOVhXHV0IEzDrHYHxmTZPfQ33CMeNzoTgbdSpVL6ijWpAyeA WJYw== X-Gm-Message-State: AOAM530M7ln5TD4JYV7SMXV13bQ3SxTsIcys+s40uV6LQn/0iSk7xIM1 /L6F0/NeH8DbyERX8i2/zHAf7XbIL9fLLg== X-Google-Smtp-Source: ABdhPJzqoms30bg/4rgygcvD/ejN71cc9mJeM2yoPWe/MCD8VGgkKP9JRsDM6LOCWqQVHJ3Q4iyY9g== X-Received: by 2002:a05:6402:4242:: with SMTP id g2mr19978706edb.103.1612198338162; Mon, 01 Feb 2021 08:52:18 -0800 (PST) Received: from localhost.localdomain ([31.45.254.141]) by smtp.gmail.com with ESMTPSA id zk6sm3245252ejb.119.2021.02.01.08.52.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Feb 2021 08:52:17 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Mon, 1 Feb 2021 17:52:09 +0100 Message-Id: <20210201165209.26988-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add colorcorrect 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 | 33 +++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_colorcorrect.c | 218 ++++++++++++++++++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 libavfilter/vf_colorcorrect.c diff --git a/doc/filters.texi b/doc/filters.texi index 490586c063..2d85a414ec 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -8056,6 +8056,39 @@ Set the amount of preserving lightness. Default value is 0.0. Allowed range is f This filter supports the all above options as @ref{commands}. +@section colorcorrect + +Adjust color white balance selectively for blacks and whites. +This filter operates in YUV colorspace. + +The filter accepts the following options: + +@table @option +@item rl +Set the red shadow spot. Allowed range is from -1.0 to 1.0. +Default value is 0. + +@item bl +Set the blue shadow spot. Allowed range is from -1.0 to 1.0. +Default value is 0. + +@item rh +Set the red highlight spot. Allowed range is from -1.0 to 1.0. +Default value is 0. + +@item bh +Set the red highlight spot. Allowed range is from -1.0 to 1.0. +Default value is 0. + +@item saturation +Set the amount of saturation. Allowed range is from -3.0 to 3.0. +Default value is 1. +@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 3a4d3d8e31..68ff8e26d2 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -197,6 +197,7 @@ 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_COLORCORRECT_FILTER) += vf_colorcorrect.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 6d269b48bf..3b4620a5d7 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -187,6 +187,7 @@ extern AVFilter ff_vf_codecview; extern AVFilter ff_vf_colorbalance; extern AVFilter ff_vf_colorchannelmixer; extern AVFilter ff_vf_colorcontrast; +extern AVFilter ff_vf_colorcorrect; extern AVFilter ff_vf_colorkey; extern AVFilter ff_vf_colorkey_opencl; extern AVFilter ff_vf_colorhold; diff --git a/libavfilter/vf_colorcorrect.c b/libavfilter/vf_colorcorrect.c new file mode 100644 index 0000000000..edb48c3ff6 --- /dev/null +++ b/libavfilter/vf_colorcorrect.c @@ -0,0 +1,218 @@ +/* + * 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 "formats.h" +#include "internal.h" +#include "video.h" + +typedef struct ColorCorrectContext { + const AVClass *class; + + float rl, bl; + float rh, bh; + float saturation; + + int depth; + + int (*do_slice)(AVFilterContext *s, void *arg, + int jobnr, int nb_jobs); +} ColorCorrectContext; + +#define PROCESS() \ + ny = y; \ + nu = saturation * (u + y * (bh - bl) + bl); \ + nv = saturation * (v + y * (rh - rl) + rl); + +static int colorcorrect_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ColorCorrectContext *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 ylinesize = frame->linesize[0]; + const int ulinesize = frame->linesize[1]; + const int vlinesize = frame->linesize[2]; + uint8_t *yptr = frame->data[0] + slice_start * ylinesize; + uint8_t *uptr = frame->data[1] + slice_start * ulinesize; + uint8_t *vptr = frame->data[2] + slice_start * vlinesize; + const float saturation = s->saturation; + const float bl = s->bl; + const float rl = s->rl; + const float bh = s->bh; + const float rh = s->rh; + + for (int y = slice_start; y < slice_end; y++) { + for (int x = 0; x < width; x++) { + float y = yptr[x] / 255.f; + float u = uptr[x] / 255.f - .5f; + float v = vptr[x] / 255.f - .5f; + float ny, nu, nv; + + PROCESS(); + + yptr[x] = av_clip_uint8( ny * 255.f); + uptr[x] = av_clip_uint8((nu + 0.5f) * 255.f); + vptr[x] = av_clip_uint8((nv + 0.5f) * 255.f); + } + + yptr += ylinesize; + uptr += ulinesize; + vptr += vlinesize; + } + + return 0; +} + +static int colorcorrect_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ColorCorrectContext *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 ylinesize = frame->linesize[0] / 2; + const int ulinesize = frame->linesize[1] / 2; + const int vlinesize = frame->linesize[2] / 2; + uint16_t *yptr = (uint16_t *)frame->data[0] + slice_start * ylinesize; + uint16_t *uptr = (uint16_t *)frame->data[1] + slice_start * ulinesize; + uint16_t *vptr = (uint16_t *)frame->data[2] + slice_start * vlinesize; + const float saturation = s->saturation; + const float bl = s->bl; + const float rl = s->rl; + const float bh = s->bh; + const float rh = s->rh; + + for (int y = slice_start; y < slice_end; y++) { + for (int x = 0; x < width; x++) { + float y = yptr[x] / max; + float u = uptr[x] / max - 0.5f; + float v = vptr[x] / max - 0.5f; + float ny, nu, nv; + + PROCESS(); + + yptr[x] = av_clip_uintp2_c( ny * max, depth); + uptr[x] = av_clip_uintp2_c((nu + 0.5f) * max, depth); + vptr[x] = av_clip_uintp2_c((nv + 0.5f) * max, depth); + } + + yptr += ylinesize; + uptr += ulinesize; + vptr += vlinesize; + } + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *frame) +{ + AVFilterContext *ctx = inlink->dst; + ColorCorrectContext *s = ctx->priv; + + ctx->internal->execute(ctx, s->do_slice, frame, NULL, + FFMIN(frame->height, ff_filter_get_nb_threads(ctx))); + + 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_YUV444P, AV_PIX_FMT_YUVJ444P, + AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV444P16, + AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA444P9, AV_PIX_FMT_YUVA444P10, AV_PIX_FMT_YUVA444P12, AV_PIX_FMT_YUVA444P16, + 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; + ColorCorrectContext *s = ctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + + s->depth = desc->comp[0].depth; + s->do_slice = s->depth <= 8 ? colorcorrect_slice8 : colorcorrect_slice16; + + return 0; +} + +static const AVFilterPad colorcorrect_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .needs_writable = 1, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad colorcorrect_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +#define OFFSET(x) offsetof(ColorCorrectContext, x) +#define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM + +static const AVOption colorcorrect_options[] = { + { "rl", "set the red shadow spot", OFFSET(rl), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF }, + { "bl", "set the blue shadow spot", OFFSET(bl), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF }, + { "rh", "set the red highlight spot", OFFSET(rh), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF }, + { "bh", "set the blue highlight spot", OFFSET(bh), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF }, + { "saturation", "set the amount of saturation", OFFSET(saturation), AV_OPT_TYPE_FLOAT, {.dbl=1}, -3, 3, VF }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(colorcorrect); + +AVFilter ff_vf_colorcorrect = { + .name = "colorcorrect", + .description = NULL_IF_CONFIG_SMALL("Adjust color white balance selectively for blacks and whites."), + .priv_size = sizeof(ColorCorrectContext), + .priv_class = &colorcorrect_class, + .query_formats = query_formats, + .inputs = colorcorrect_inputs, + .outputs = colorcorrect_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, + .process_command = ff_filter_process_command, +};