From patchwork Sat Oct 20 15:02:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 10726 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 D2E56448423 for ; Sat, 20 Oct 2018 18:02:41 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3202C68A702; Sat, 20 Oct 2018 18:02:23 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-ed1-f67.google.com (mail-ed1-f67.google.com [209.85.208.67]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 72D0368A62B for ; Sat, 20 Oct 2018 18:02:16 +0300 (EEST) Received: by mail-ed1-f67.google.com with SMTP id c1-v6so34002260ede.5 for ; Sat, 20 Oct 2018 08:02:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=mHe8xo2FhbmHm27HF1SjlIIwRiQExIzEGGd5YOjcOo0=; b=bO2/kvdFcNDhQSg6uYPEcuTaciM88u1GyZH2j6XCy8p+9++nq0T1sUWJ18M5QkZLYL Pv6Hp2JJJrQaPZoxKYVGQfmBzHQbWfubiKxXeSGBvz1ahK4yZdG2lhHENsWqS0aElOWR zIG+ExbXKTY+FEsM+ymL9Oiay3z1+gv8muhkcG23cAXylco38QwXB0n9Tqt7CBkEta1A Mpm6B88W8BKUQ4P3qQel4RQMZbJ04RSYv6M8zM49ReeqM6C3Lul0xG+F+MbxQ9zbGBhy 2bak1zaTyM+6KujixeayDsRhhRXEPSBIWw9qN0npy2DkhKl90YB9LuIRXLIltWiEydNH h5dQ== 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:in-reply-to :references; bh=mHe8xo2FhbmHm27HF1SjlIIwRiQExIzEGGd5YOjcOo0=; b=Ibl5ukZWj6dedubmLYxR7qSgjmlOBZ+I/KMV6NrDHEhMLRRVtjsP/zGQPlS56w2lOM UzPEr5TgbOU0mEP+UIHOJV+4F0GbJehKs0Zc/kfLH3EJOvcHwg/EnvQkLQXWoDi6wYBS LSTjWKb2lHzaJqAXcIqM1IuBWvs6x6dVVeU984K6TukTGMzGmGCre/FJhEbMchvOam11 SirfOghIOzyB1eEPT8ZdLqgcX5eO8AlQVt/XXLzySg4YkN1avNpDD8ntAPrT51EjEYzz fDIevmmEhgvFSGUNFwIg+xucBnfub/tasDiGB9aLbIy8vaIFzr2e4Jdgz7kFm2KMsTME HExg== X-Gm-Message-State: ABuFfoh9Xy4iY1Ktz6jLBeupXn7pu291/yc55fhBfkq7ZIdPF5CrkUqe bh2+Qj9SF4uajbEZm1g84/k1lFRSOQA= X-Google-Smtp-Source: ACcGV62h+yRFAiCZrbYCDf4oX3WdUPnEOOdrgX2lIMpzdjeMj13iEeU5S6TZgdTejWRyaC54i+axXA== X-Received: by 2002:aa7:d804:: with SMTP id v4-v6mr8824044edq.255.1540047762457; Sat, 20 Oct 2018 08:02:42 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id z7-v6sm9518915edd.71.2018.10.20.08.02.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Oct 2018 08:02:41 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Oct 2018 17:02:31 +0200 Message-Id: <20181020150231.6534-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181019192758.9239-1-onemda@gmail.com> References: <20181019192758.9239-1-onemda@gmail.com> Subject: [FFmpeg-devel] [PATCH] avfilter: add vibrance 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 | 29 +++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_vibrance.c | 240 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 271 insertions(+) create mode 100644 libavfilter/vf_vibrance.c diff --git a/doc/filters.texi b/doc/filters.texi index f314718a56..d49e30b168 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -17274,6 +17274,35 @@ and ones with constant delta pts. If there was frames with variable delta, than it will also show min and max delta encountered. +@section vibrance + +Boost or cut saturation. + +The filter accepts the following options: +@table @option +@item intensity +Set strength of boost if positive value or cut if negative value. Default is 0. +Allowed range is from -2 to 2. + +@item rbal +Set the red balance. Default is 1. Allowed range is from -10 to 10. + +@item gbal +Set the green balance. Default is 1. Allowed range is from -10 to 10. + +@item bbal +Set the blue balance. Default is 1. Allowed range is from -10 to 10. + +@item rlum +Set the red luma coefficient. + +@item glum +Set the green luma coefficient. + +@item blum +Set the blue luma coefficient. +@end table + @anchor{vignette} @section vignette diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 46c6023bcc..38fe649078 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -393,6 +393,7 @@ OBJS-$(CONFIG_VAGUEDENOISER_FILTER) += vf_vaguedenoiser.o OBJS-$(CONFIG_VECTORSCOPE_FILTER) += vf_vectorscope.o OBJS-$(CONFIG_VFLIP_FILTER) += vf_vflip.o OBJS-$(CONFIG_VFRDET_FILTER) += vf_vfrdet.o +OBJS-$(CONFIG_VIBRANCE_FILTER) += vf_vibrance.o OBJS-$(CONFIG_VIDSTABDETECT_FILTER) += vidstabutils.o vf_vidstabdetect.o OBJS-$(CONFIG_VIDSTABTRANSFORM_FILTER) += vidstabutils.o vf_vidstabtransform.o OBJS-$(CONFIG_VIGNETTE_FILTER) += vf_vignette.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 536765581b..2289efbb5b 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -374,6 +374,7 @@ extern AVFilter ff_vf_vaguedenoiser; extern AVFilter ff_vf_vectorscope; extern AVFilter ff_vf_vflip; extern AVFilter ff_vf_vfrdet; +extern AVFilter ff_vf_vibrance; extern AVFilter ff_vf_vidstabdetect; extern AVFilter ff_vf_vidstabtransform; extern AVFilter ff_vf_vignette; diff --git a/libavfilter/vf_vibrance.c b/libavfilter/vf_vibrance.c new file mode 100644 index 0000000000..d167ab8e2f --- /dev/null +++ b/libavfilter/vf_vibrance.c @@ -0,0 +1,240 @@ +/* + * Copyright (c) 2018 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 VibranceContext { + const AVClass *class; + + float intensity; + float balance[3]; + float lcoeffs[3]; + + int depth; + + int (*do_slice)(AVFilterContext *s, void *arg, + int jobnr, int nb_jobs); +} VibranceContext; + +static inline float lerpf(float v0, float v1, float f) +{ + return v0 + (v1 - v0) * f; +} + +static int vibrance_slice8(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs) +{ + VibranceContext *s = avctx->priv; + AVFrame *frame = arg; + const int width = frame->width; + const int height = frame->height; + const float gc = s->lcoeffs[0]; + const float bc = s->lcoeffs[1]; + const float rc = s->lcoeffs[2]; + const float intensity = s->intensity; + const float gintensity = intensity * s->balance[0]; + const float bintensity = intensity * s->balance[1]; + const float rintensity = intensity * s->balance[2]; + 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; + + for (int y = slice_start; y < slice_end; y++) { + for (int x = 0; x < width; x++) { + float g = gptr[x] / 255.f; + float b = bptr[x] / 255.f; + float r = rptr[x] / 255.f; + float max_color = FFMAX3(r, g, b); + float min_color = FFMIN3(r, g, b); + float color_saturation = max_color - min_color; + float luma = g * gc + r * rc + b * bc; + const float cg = 1.f + gintensity * (1.f - FFSIGN(gintensity) * color_saturation); + const float cb = 1.f + bintensity * (1.f - FFSIGN(bintensity) * color_saturation); + const float cr = 1.f + rintensity * (1.f - FFSIGN(rintensity) * color_saturation); + + g = lerpf(luma, g, cg); + b = lerpf(luma, b, cb); + r = lerpf(luma, r, cr); + + gptr[x] = av_clip_uint8(g * 255.f); + bptr[x] = av_clip_uint8(b * 255.f); + rptr[x] = av_clip_uint8(r * 255.f); + } + + gptr += glinesize; + bptr += blinesize; + rptr += rlinesize; + } + + return 0; +} + +static int vibrance_slice16(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs) +{ + VibranceContext *s = avctx->priv; + AVFrame *frame = arg; + const int depth = s->depth; + const float max = (1 << depth) - 1; + const float gc = s->lcoeffs[0]; + const float bc = s->lcoeffs[1]; + const float rc = s->lcoeffs[2]; + const int width = frame->width; + const int height = frame->height; + const float intensity = s->intensity; + const float gintensity = intensity * s->balance[0]; + const float bintensity = intensity * s->balance[1]; + const float rintensity = intensity * s->balance[2]; + 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; + + for (int y = slice_start; y < slice_end; y++) { + for (int x = 0; x < width; x++) { + float g = gptr[x] / max; + float b = bptr[x] / max; + float r = rptr[x] / max; + float max_color = FFMAX3(r, g, b); + float min_color = FFMIN3(r, g, b); + float color_saturation = max_color - min_color; + float luma = g * gc + r * rc + b * bc; + const float cg = 1.f + gintensity * (1.f - FFSIGN(gintensity) * color_saturation); + const float cb = 1.f + bintensity * (1.f - FFSIGN(bintensity) * color_saturation); + const float cr = 1.f + rintensity * (1.f - FFSIGN(rintensity) * color_saturation); + + g = lerpf(luma, g, cg); + b = lerpf(luma, b, cb); + r = lerpf(luma, r, cr); + + gptr[x] = av_clip_uintp2_c(g * max, depth); + bptr[x] = av_clip_uintp2_c(b * max, depth); + rptr[x] = av_clip_uintp2_c(r * max, depth); + } + + gptr += glinesize; + bptr += blinesize; + rptr += rlinesize; + } + + return 0; +} + +static int filter_frame(AVFilterLink *link, AVFrame *frame) +{ + AVFilterContext *avctx = link->dst; + VibranceContext *s = avctx->priv; + int res; + + if (res = avctx->internal->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 query_formats(AVFilterContext *avctx) +{ + static const enum AVPixelFormat pixel_fmts[] = { + 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_NONE + }; + + AVFilterFormats *formats = NULL; + + formats = ff_make_format_list(pixel_fmts); + if (!formats) + return AVERROR(ENOMEM); + + return ff_set_common_formats(avctx, formats); +} + +static av_cold int config_input(AVFilterLink *inlink) +{ + AVFilterContext *avctx = inlink->dst; + VibranceContext *s = avctx->priv; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); + + s->depth = desc->comp[0].depth; + s->do_slice = s->depth <= 8 ? vibrance_slice8 : vibrance_slice16; + + return 0; +} + +static const AVFilterPad vibrance_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .needs_writable = 1, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad vibrance_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +#define OFFSET(x) offsetof(VibranceContext, x) +#define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption vibrance_options[] = { + { "intensity", "set the intensity value", OFFSET(intensity), AV_OPT_TYPE_FLOAT, {.dbl=0}, -2, 2, VF }, + { "rbal", "set the red balance value", OFFSET(balance[2]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF }, + { "gbal", "set the green balance value", OFFSET(balance[0]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF }, + { "bbal", "set the blue balance value", OFFSET(balance[1]), AV_OPT_TYPE_FLOAT, {.dbl=1}, -10, 10, VF }, + { "rlum", "set the red luma coefficient", OFFSET(lcoeffs[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.072186}, 0, 1, VF }, + { "glum", "set the green luma coefficient", OFFSET(lcoeffs[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.715158}, 0, 1, VF }, + { "blum", "set the blue luma coefficient", OFFSET(lcoeffs[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.212656}, 0, 1, VF }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(vibrance); + +AVFilter ff_vf_vibrance = { + .name = "vibrance", + .description = NULL_IF_CONFIG_SMALL("Boost or cut saturation."), + .priv_size = sizeof(VibranceContext), + .priv_class = &vibrance_class, + .query_formats = query_formats, + .inputs = vibrance_inputs, + .outputs = vibrance_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, +};