From patchwork Fri Oct 19 19:27:58 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: 10719 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 6136D447AF8 for ; Fri, 19 Oct 2018 22:28:12 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B197168A792; Fri, 19 Oct 2018 22:27:53 +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 DD1C468A76F for ; Fri, 19 Oct 2018 22:27:47 +0300 (EEST) Received: by mail-ed1-f67.google.com with SMTP id g32-v6so32383812edg.13 for ; Fri, 19 Oct 2018 12:28:14 -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=Lz/LQY9ofi2yep8fnRddJiso5RfhqmPvtOc8v7ov5k4=; b=Q9e9Iwa80+lxYPUILUBxQeODyj0kf1g34ncRYGd852nCyKeY3xnN7639r8RRkcULfl fdY6ezG4rkcv4Dby/yBO7TQ4cLpibP8uGiflet+78kd7rFhUOIRpCFMFvoHJvu7T6A4E r16La3/xjUnFE4CgCU2kxC7akJsd9BXPzluUjqoy2vueoKWawR+LEoRl4WXuUpaO7ya0 J5JqUABtj1LlJ1oVwciGt3uRxqD9ppX4ZvJ8HK96d2rNQI/hHRfKSGhDzB9eHMjmpw+n wD27j2cTVZKX2/rPO/UCgCb1IwDU8l1HEmlsdtlOWef4sdSapcm6rLiiSE9JT7sei+3U yU0w== 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=Lz/LQY9ofi2yep8fnRddJiso5RfhqmPvtOc8v7ov5k4=; b=dmZNoWZfskRJgDHiqnMmTNi9ObB0aaXI9U+1ndlxInXQAdpBxzE+m9sQ602TkNDD1G k0dtK0XNIhYkMPKEVzeffvT3DkcOoNi7D7kcP4OFezNOXNlKnPqW8CYEo5YyfNSRDWGV GoUZGkbRsPAjqbeFQXGpIT9M1hWirR6QoPZz+zL7UiNZcbFJxlbdf19IoNh5W14mXJ0y sQzmSM2ayIMkztmYpdJV0IeXGglCNejcCJnUjEbHySbnJE01kFBrAnm9mkglEE3GYCDQ NTi1mkjYMFhJ250rladUpf91sfDYoDlMDRQ2xXjyF8biWwHctr78SraxF6L3I/7TfL80 1Y4g== X-Gm-Message-State: ABuFfojPJ1qjyTqrkkQIYp3sggw3wzolfVovjgcgGJC3VdL3A71rUH+q g4H9iZS29lSp+OAE2upZJWPWqIjVw1E= X-Google-Smtp-Source: ACcGV61AEiAjSER6lIatjrbpxo6HVtmJV6c+3atmfivJ2nMg+Hr3VxPDgBdlwEjgRUb57rdU5Ts6gg== X-Received: by 2002:aa7:cdc9:: with SMTP id h9-v6mr7256611edw.133.1539977293314; Fri, 19 Oct 2018 12:28:13 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id v30-v6sm11338268edc.21.2018.10.19.12.28.11 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Oct 2018 12:28:12 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Fri, 19 Oct 2018 21:27:58 +0200 Message-Id: <20181019192758.9239-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 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 --- libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_vibrance.c | 169 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 171 insertions(+) create mode 100644 libavfilter/vf_vibrance.c 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..df07b914cb --- /dev/null +++ b/libavfilter/vf_vibrance.c @@ -0,0 +1,169 @@ +/* + * 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; + + int depth; + int planewidth[4]; + int planeheight[4]; + + int (*do_slice)(AVFilterContext *s, void *arg, + int jobnr, int nb_jobs); +} VibranceContext; + +static int vibrance_slice(AVFilterContext *avctx, void *arg, int jobnr, int nb_jobs) +{ + VibranceContext *s = avctx->priv; + AVFrame *frame = arg; + const float intensity = s->intensity; + const float abs_intensity = fabsf(intensity); + const float one_intensity = 1.f - abs_intensity; + const int slice_start = (s->planeheight[1] * jobnr) / nb_jobs; + const int slice_end = (s->planeheight[1] * (jobnr + 1)) / nb_jobs; + const int ulinesize = frame->linesize[1]; + const int vlinesize = frame->linesize[2]; + uint8_t *uptr = frame->data[1] + slice_start * ulinesize; + uint8_t *vptr = frame->data[2] + slice_start * vlinesize; + + for (int y = slice_start; y < slice_end; y++) { + for (int x = 0; x < s->planewidth[1]; x++) { + float saturation, target_saturation, hue; + int u = uptr[x] - 128; + int v = vptr[x] - 128; + + saturation = hypotf(u, v) / 180.f; + if (intensity < 0) { + target_saturation = saturation * saturation; + } else { + target_saturation = sqrtf(saturation); + } + saturation = abs_intensity * target_saturation + one_intensity * saturation; + hue = atan2f(v, u); + + uptr[x] = av_clip_uint8(floorf(128 + 180.f * saturation * cosf(hue))); + vptr[x] = av_clip_uint8(floorf(128 + 180.f * saturation * sinf(hue))); + } + uptr += ulinesize; + vptr += vlinesize; + } + + 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_YUV420P, + AV_PIX_FMT_YUV422P, + AV_PIX_FMT_YUV444P, + AV_PIX_FMT_YUVA420P, + AV_PIX_FMT_YUVA422P, + AV_PIX_FMT_YUVA444P, + 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->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); + s->planeheight[0] = s->planeheight[3] = inlink->h; + s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); + s->planewidth[0] = s->planewidth[3] = inlink->w; + + s->depth = desc->comp[0].depth; + s->do_slice = vibrance_slice; + + 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 FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM + +static const AVOption vibrance_options[] = { + { "intensity", "set the vibrance intensity value", OFFSET(intensity), AV_OPT_TYPE_FLOAT, { .dbl = 0}, -1.0, 1.0, FLAGS }, + { 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, +};