From patchwork Tue Jan 26 11:16:31 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: 25193 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 0098444BAF8 for ; Tue, 26 Jan 2021 13:49:22 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CBD8F689993; Tue, 26 Jan 2021 13:49:21 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 99DB76804F1 for ; Tue, 26 Jan 2021 13:49:15 +0200 (EET) Received: by mail-wr1-f54.google.com with SMTP id 7so16128380wrz.0 for ; Tue, 26 Jan 2021 03:49:15 -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=RsTw/hu1hJMqbSbR5PFhVePnLxJEAOeuQP/+ORlp3pQ=; b=rFuBKAFEu7ts99eB+x2UslODWEErENOTUv9+uZQfStO5aaybgJnRBB8CEVIQqTwD7t h5KhYzxMK1SSTe97Zlu6wyOCBTMzcjCyQXF2lVNhR8HDfENQ1LDGgEVhQoqch8rorsLW qf9MjbX47RPtnAo3wthxSiwuQAEyB6RRf5BPIRoMOYfTCNIzKNOCgPgzWQMDFhm7Wp+i dCnxvPGIllU+cR+RsHLKaO00kOnRIlGWUhRCj9y5CtFkUs1EfZPMTTTnmBALHVA7EgaS 1d67GT1AL7uP5zAEItKSHKITro9JDX83FiAwxhc23MN4afWjR2JOXiIkRkZJ2CfkSrfW stYw== 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=RsTw/hu1hJMqbSbR5PFhVePnLxJEAOeuQP/+ORlp3pQ=; b=oI7h7+dm5Ji1Rk9VSYfblHVexYammcaSUmtInSbpbZaT5YXGRmGrgmA/15TVWb1Ena rGYNHX80SVV1OM+MrndJOQgjoZczbTpGBECUA/SRFpjFiYg3PJqsBODlG0TcHboJRCGV Iq4WMC47WRvnb0VP3rMy5qP7WTthYByMeXxMHzT3cI6kuw2+AEgN4XhmOTXR0y46FTkC /RaUBJMcqAAVcftIeAIITUpENxivkIRyY0CHlMCiKKZEZg6A2aUuhoxCOgnrU86qOtFb CYMQrHS8WLAQsCDxHmVxntHwgmKZdYQnq2atuhsDHaLpu17GS7pDY291KsULkO4hk8FO 6Q6Q== X-Gm-Message-State: AOAM530WKmbYA6ukT6LX9npY27Wk+NXncM0xEO2z+02PEEChQ5Tmyf8o /lLDmoDawVrAZeYgB9RI+P7tuybS0Zo= X-Google-Smtp-Source: ABdhPJxZNVF129X8YPpvZfjR8NXNPxzI0c3te5AYPISjjvCsed2mBUWohtbTcMss3wCiaaevyDBeCg== X-Received: by 2002:a17:906:7798:: with SMTP id s24mr3169457ejm.19.1611659800345; Tue, 26 Jan 2021 03:16:40 -0800 (PST) Received: from localhost.localdomain ([31.45.254.141]) by smtp.gmail.com with ESMTPSA id w16sm12773446edv.4.2021.01.26.03.16.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Jan 2021 03:16:39 -0800 (PST) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Tue, 26 Jan 2021 12:16:31 +0100 Message-Id: <20210126111631.4906-1-onemda@gmail.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH] avfilter: add colortemperature 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 | 22 +++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/vf_colortemperature.c | 280 ++++++++++++++++++++++++++++++ 4 files changed, 304 insertions(+) create mode 100644 libavfilter/vf_colortemperature.c diff --git a/doc/filters.texi b/doc/filters.texi index 991f8d62c0..2a994e110b 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -8549,6 +8549,28 @@ For example to convert the input to SMPTE-240M, use the command: colorspace=smpte240m @end example +@section colortemperature +Adjust color temperature in video to simulate variations in ambient color temperature. + +The filter accepts the following options: + +@table @option +@item temperature +Set the temperature in Kelvins. Allowed range is from 1000 to 40000. +Default value is 6500 K. + +@item mix +Set mixing with filtered output. Allowed range is from 0 to 1. +Default value is 1. + +@item pl +Set the amount of preserving lightness. Allowed range is from 0 to 1. +Default value is 0. +@end table + +@subsection Commands +This filter supports same @ref{commands} as options. + @section convolution Apply convolution of 3x3, 5x5, 7x7 or horizontal/vertical up to 49 elements. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index fbb4e29bd0..e1c364fbc6 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -203,6 +203,7 @@ OBJS-$(CONFIG_COLORHOLD_FILTER) += vf_colorkey.o OBJS-$(CONFIG_COLORLEVELS_FILTER) += vf_colorlevels.o OBJS-$(CONFIG_COLORMATRIX_FILTER) += vf_colormatrix.o OBJS-$(CONFIG_COLORSPACE_FILTER) += vf_colorspace.o colorspace.o colorspacedsp.o +OBJS-$(CONFIG_COLORTEMPERATURE_FILTER) += vf_colortemperature.o OBJS-$(CONFIG_CONVOLUTION_FILTER) += vf_convolution.o OBJS-$(CONFIG_CONVOLUTION_OPENCL_FILTER) += vf_convolution_opencl.o opencl.o \ opencl/convolution.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index d9d36554c4..1c1cc5b276 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -192,6 +192,7 @@ extern AVFilter ff_vf_colorhold; extern AVFilter ff_vf_colorlevels; extern AVFilter ff_vf_colormatrix; extern AVFilter ff_vf_colorspace; +extern AVFilter ff_vf_colortemperature; extern AVFilter ff_vf_convolution; extern AVFilter ff_vf_convolution_opencl; extern AVFilter ff_vf_convolve; diff --git a/libavfilter/vf_colortemperature.c b/libavfilter/vf_colortemperature.c new file mode 100644 index 0000000000..11ab1d0cce --- /dev/null +++ b/libavfilter/vf_colortemperature.c @@ -0,0 +1,280 @@ +/* + * 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 ColorTemperatureContext { + const AVClass *class; + + float temperature; + float mix; + float preserve; + + float color[3]; + + int depth; + + int (*do_slice)(AVFilterContext *s, void *arg, + int jobnr, int nb_jobs); +} ColorTemperatureContext; + +static float saturate(float input) +{ + return av_clipf(input, 0.f, 1.f); +} + +static void kelvin2rgb(float k, float *rgb) +{ + float kelvin = k / 100.0f; + + if (kelvin <= 66.0f) { + rgb[0] = 1.0f; + rgb[1] = saturate(0.39008157876901960784f * logf(kelvin) - 0.63184144378862745098f); + } else { + const float t = fmaxf(kelvin - 60.0f, 0.0f); + rgb[0] = saturate(1.29293618606274509804f * powf(t, -0.1332047592f)); + rgb[1] = saturate(1.12989086089529411765f * powf(t, -0.0755148492f)); + } + + if (kelvin >= 66.0f) + rgb[2] = 1.0f; + else if (kelvin <= 19.0f) + rgb[2] = 0.0f; + else + rgb[2] = saturate(0.54320678911019607843f * logf(kelvin - 10.0f) - 1.19625408914f); +} + +static float lerpf(float v0, float v1, float f) +{ + return v0 + (v1 - v0) * f; +} + +static int temperature_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ColorTemperatureContext *s = ctx->priv; + AVFrame *frame = arg; + const int width = frame->width; + const int height = frame->height; + const float mix = s->mix; + const float preserve = s->preserve; + const float *color = s->color; + 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]; + float b = bptr[x]; + float r = rptr[x]; + float nr, ng, nb; + float l0, l1, l; + + nr = r * color[0]; + ng = g * color[1]; + nb = b * color[2]; + + nr = lerpf(r, nr, mix); + ng = lerpf(g, ng, mix); + nb = lerpf(b, nb, mix); + + l0 = (FFMAX3(r, g, b) + FFMIN3(r, g, b)) + FLT_EPSILON; + l1 = (FFMAX3(nr, ng, nb) + FFMIN3(nr, ng, nb)) + FLT_EPSILON; + l = l0 / l1; + + r = nr * l; + g = ng * l; + b = nb * l; + + nr = lerpf(nr, r, preserve); + ng = lerpf(ng, g, preserve); + nb = lerpf(nb, b, preserve); + + 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 temperature_slice16(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) +{ + ColorTemperatureContext *s = ctx->priv; + AVFrame *frame = arg; + const int depth = s->depth; + const int width = frame->width; + const int height = frame->height; + const float preserve = s->preserve; + const float mix = s->mix; + const float *color = s->color; + const int slice_start = (height * jobnr) / nb_jobs; + const int slice_end = (height * (jobnr + 1)) / nb_jobs; + const int glinesize = frame->linesize[0] / sizeof(uint16_t); + const int blinesize = frame->linesize[1] / sizeof(uint16_t); + const int rlinesize = frame->linesize[2] / sizeof(uint16_t); + 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]; + float b = bptr[x]; + float r = rptr[x]; + float nr, ng, nb; + float l0, l1, l; + + nr = r * color[0]; + ng = g * color[1]; + nb = b * color[2]; + + nr = lerpf(r, nr, mix); + ng = lerpf(g, ng, mix); + nb = lerpf(b, nb, mix); + + l0 = (FFMAX3(r, g, b) + FFMIN3(r, g, b)) + FLT_EPSILON; + l1 = (FFMAX3(nr, ng, nb) + FFMIN3(nr, ng, nb)) + FLT_EPSILON; + l = l0 / l1; + + r = nr * l; + g = ng * l; + b = nb * l; + + nr = lerpf(nr, r, preserve); + ng = lerpf(ng, g, preserve); + nb = lerpf(nb, b, preserve); + + gptr[x] = av_clip_uintp2(ng, depth); + bptr[x] = av_clip_uintp2(nb, depth); + rptr[x] = av_clip_uintp2(nr, depth); + } + + gptr += glinesize; + bptr += blinesize; + rptr += rlinesize; + } + + return 0; +} + +static int filter_frame(AVFilterLink *inlink, AVFrame *frame) +{ + AVFilterContext *ctx = inlink->dst; + ColorTemperatureContext *s = ctx->priv; + + kelvin2rgb(s->temperature, s->color); + + 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_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(ctx, formats); +} + +static av_cold int config_input(AVFilterLink *inlink) +{ + AVFilterContext *ctx = inlink->dst; + ColorTemperatureContext *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 ? temperature_slice8 : temperature_slice16; + + return 0; +} + +static const AVFilterPad inputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .filter_frame = filter_frame, + .config_props = config_input, + .needs_writable = 1, + }, + { NULL } +}; + +static const AVFilterPad outputs[] = { + { + .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + }, + { NULL } +}; + +#define OFFSET(x) offsetof(ColorTemperatureContext, x) +#define VF AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM + +static const AVOption colortemperature_options[] = { + { "temperature", "set the temperature in Kelvins", OFFSET(temperature), AV_OPT_TYPE_FLOAT, {.dbl=6500}, 1000, 40000, VF }, + { "mix", "set the mix with filtered output", OFFSET(mix), AV_OPT_TYPE_FLOAT, {.dbl=1}, 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(colortemperature); + +AVFilter ff_vf_colortemperature = { + .name = "colortemperature", + .description = NULL_IF_CONFIG_SMALL("Adjust color temperature of video."), + .priv_size = sizeof(ColorTemperatureContext), + .priv_class = &colortemperature_class, + .query_formats = query_formats, + .inputs = inputs, + .outputs = outputs, + .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, + .process_command = ff_filter_process_command, +};