From patchwork Sun Feb 7 18:51:29 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: 25484 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 511B2447C4F for ; Sun, 7 Feb 2021 20:51:46 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 267036881D4; Sun, 7 Feb 2021 20:51:46 +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 520D56881D4 for ; Sun, 7 Feb 2021 20:51:39 +0200 (EET) Received: by mail-ed1-f53.google.com with SMTP id s11so15660744edd.5 for ; Sun, 07 Feb 2021 10:51:39 -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=JOq+XbShXguOFtcqZwkMrzi1KYfRMs0gYBHboa9FDsM=; b=OlCb4NgXWvFhq0tWPTocyjCUcFvJRv3of/ogo7REcrxIrUqLQuwRk4fRIrgSA44RwK 0sADDrgA5A9ok/AK+cqrvrp5TS6ysqm+FqlhboKa99K7HsmfW/55+cKSWTFnyToPIRs1 imHGYzZUBBqXyCRKGl08sEuoJfOcCdhFvLZ9KyBrMBA4WVR/DWGTUaLUpq8P7vkXF7cP nzmZlxCIAt1h93Y4fSaw6dtromCaWH31K9/NkNuPhpXXWvB35swxa9n2kx/F/XdxSUVO 9B/uqPI9Biy8Ohurt6YUuQmXfn/e5Q24y08qgmfrIvUXIQcoF3e4ZL1G3hH430NmznEK VNFw== 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=JOq+XbShXguOFtcqZwkMrzi1KYfRMs0gYBHboa9FDsM=; b=j1xczF8NocnKM3DxB1T9DWMxGpptvet292sZ9XgIo2xx0zILuIMvvgCqsk5DGPvvoE SdWsywmQk7+Uh7J4LIj6VDT0HtLBHrohpBEbkwGbwenr+tsJrCg5zgx+IkMrNTDTBKLc D8kgMC1mtlKUPZadRwM59bo0B+Ym/8B7FSXoGZQwz4Pq8biutycJGLs3tGDDArwBTlUV HL0xRQTv15qpAZ/c4zmc29AyXAQ54ntaCrn6hkVm76iBtEby+apcECrjn6MliwqTDpV+ 8vcuvU4Bb2OeVnOTKtZlaD8F3iNkTL3K5O1s5ZhctDhlP52LkT078x6kwxj6uimTXbJA 40HQ== X-Gm-Message-State: AOAM530+5JmGp+Y7u15MlFuxiwVTW/a7HYVwyqiP/7GtqxHfwWQp0GRM Cg77o9Cag/kFnuKQIVsUF6gCkGEPiTUMCQ== X-Google-Smtp-Source: ABdhPJxHFGCXft5R5Teq+Ug7vG2yUAiCtoaxmJ05D2n/DCbLVoUYuuMyS/nMF/rWXyO5jTwRgQWP9w== X-Received: by 2002:a50:d51d:: with SMTP id u29mr6667082edi.167.1612723898650; Sun, 07 Feb 2021 10:51:38 -0800 (PST) Received: from localhost.localdomain ([212.15.167.195]) by smtp.gmail.com with ESMTPSA id z65sm7878827ede.80.2021.02.07.10.51.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 07 Feb 2021 10:51:38 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sun, 7 Feb 2021 19:51:29 +0100 Message-Id: <20210207185129.14265-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add exposure video 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 | 19 +++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_exposure.c | 144 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 libavfilter/vf_exposure.c diff --git a/doc/filters.texi b/doc/filters.texi index fd42df579c..d199ed1357 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -11361,6 +11361,25 @@ Six-point interpolation. @subsection Commands This filter supports same @ref{commands} as options. +@section exposure +Adjust exposure of the video stream. + +The filter accepts the following options: + +@table @option +@item exposure +Set the exposure correction in EV. Allowed range is from -3.0 to 3.0 EV +Default value is 0 EV. + +@item black +Set the black level correction. Allowed range is from -1.0 to 1.0. +Default value is 0. +@end table + +@subsection Commands + +This filter supports same @ref{commands} as options. + @section extractplanes Extract color channel components from input video stream into diff --git a/libavfilter/Makefile b/libavfilter/Makefile index d403e2b390..5a611e7a9e 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -259,6 +259,7 @@ OBJS-$(CONFIG_EROSION_FILTER) += vf_neighbor.o OBJS-$(CONFIG_EROSION_OPENCL_FILTER) += vf_neighbor_opencl.o opencl.o \ opencl/neighbor.o OBJS-$(CONFIG_ESTDIF_FILTER) += vf_estdif.o +OBJS-$(CONFIG_EXPOSURE_FILTER) += vf_exposure.o OBJS-$(CONFIG_EXTRACTPLANES_FILTER) += vf_extractplanes.o OBJS-$(CONFIG_FADE_FILTER) += vf_fade.o OBJS-$(CONFIG_FFTDNOIZ_FILTER) += vf_fftdnoiz.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 8ffb2cd82e..31546c268d 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -244,6 +244,7 @@ extern AVFilter ff_vf_eq; extern AVFilter ff_vf_erosion; extern AVFilter ff_vf_erosion_opencl; extern AVFilter ff_vf_estdif; +extern AVFilter ff_vf_exposure; extern AVFilter ff_vf_extractplanes; extern AVFilter ff_vf_fade; extern AVFilter ff_vf_fftdnoiz; diff --git a/libavfilter/vf_exposure.c b/libavfilter/vf_exposure.c new file mode 100644 index 0000000000..b7ad075630 --- /dev/null +++ b/libavfilter/vf_exposure.c @@ -0,0 +1,144 @@ +/* + * 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 ExposureContext { + const AVClass *class; + + float exposure; + float black; + + float scale; + int (*do_slice)(AVFilterContext *s, void *arg, + int jobnr, int nb_jobs); +} ExposureContext; + +static int exposure_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ExposureContext *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 float black = s->black; + const float scale = s->scale; + + for (int p = 0; p < 3; p++) { + const int linesize = frame->linesize[p] / 4; + float *ptr = (float *)frame->data[p] + slice_start * linesize; + for (int y = slice_start; y < slice_end; y++) { + for (int x = 0; x < width; x++) + ptr[x] = (ptr[x] - black) * scale; + + ptr += linesize; + } + } + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *frame) +{ + AVFilterContext *ctx = inlink->dst; + ExposureContext *s = ctx->priv; + + s->scale = 1.f / (exp2f(-s->exposure) - s->black); + 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_GBRPF32, AV_PIX_FMT_GBRAPF32, + 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; + ExposureContext *s = ctx->priv; + + s->do_slice = exposure_slice; + + return 0; +} + +static const AVFilterPad exposure_inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .needs_writable = 1, + .filter_frame = filter_frame, + .config_props = config_input, + }, + { NULL } +}; + +static const AVFilterPad exposure_outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +#define OFFSET(x) offsetof(ExposureContext, x) +#define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM + +static const AVOption exposure_options[] = { + { "exposure", "set the exposure correction", OFFSET(exposure), AV_OPT_TYPE_FLOAT, {.dbl=0}, -3, 3, VF }, + { "black", "set the black level correction", OFFSET(black), AV_OPT_TYPE_FLOAT, {.dbl=0}, -1, 1, VF }, + { NULL } +}; + +AVFILTER_DEFINE_CLASS(exposure); + +AVFilter ff_vf_exposure = { + .name = "exposure", + .description = NULL_IF_CONFIG_SMALL("Adjust exposure of the video stream."), + .priv_size = sizeof(ExposureContext), + .priv_class = &exposure_class, + .query_formats = query_formats, + .inputs = exposure_inputs, + .outputs = exposure_outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, + .process_command = ff_filter_process_command, +};