From f6621a8e72c010faa77e8d0239fd45a4d71cfbf8 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Mon, 1 Feb 2021 14:02:06 +0100 Subject: [PATCH] avfilter: add colorcorrect filter --- Changelog | 1 + doc/filters.texi | 33 ++++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/version.h | 2 +- libavfilter/vf_colorcorrect.c | 217 ++++++++++++++++++++++++++++++++++ 6 files changed, 254 insertions(+), 1 deletion(-) create mode 100644 libavfilter/vf_colorcorrect.c diff --git a/Changelog b/Changelog index 4a4e6e307a..9a8b54da40 100644 --- a/Changelog +++ b/Changelog @@ -63,6 +63,7 @@ version : - colortemperature filter - colorcontrast filter - PFM encoder +- colorcorrect filter version 4.3: 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 c8e3e7bb7a..14b930bcb1 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 c40ad1d60a..a60b1b5a3d 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/version.h b/libavfilter/version.h index 4df2778b78..6fa5d635f7 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -30,7 +30,7 @@ #include "libavutil/version.h" #define LIBAVFILTER_VERSION_MAJOR 7 -#define LIBAVFILTER_VERSION_MINOR 101 +#define LIBAVFILTER_VERSION_MINOR 102 #define LIBAVFILTER_VERSION_MICRO 100 diff --git a/libavfilter/vf_colorcorrect.c b/libavfilter/vf_colorcorrect.c new file mode 100644 index 0000000000..f998f02e30 --- /dev/null +++ b/libavfilter/vf_colorcorrect.c @@ -0,0 +1,217 @@ +/* + * 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() \ + float y = yptr[x] * imax; \ + float u = uptr[x] * imax - .5f; \ + float v = vptr[x] * imax - .5f; \ + float ny, nu, nv; \ + \ + ny = y; \ + nu = saturation * (u + y * bd + bl); \ + nv = saturation * (v + y * rd + rl); + +static int colorcorrect_slice8(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 float imax = 1.f / max; + 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 bd = s->bh - bl; + const float rd = s->rh - rl; + + for (int y = slice_start; y < slice_end; y++) { + for (int x = 0; x < width; x++) { + PROCESS() + + yptr[x] = av_clip_uint8( ny * max); + uptr[x] = av_clip_uint8((nu + 0.5f) * max); + vptr[x] = av_clip_uint8((nv + 0.5f) * max); + } + + 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 float imax = 1.f / max; + 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 bd = s->bh - bl; + const float rd = s->rh - rl; + + for (int y = slice_start; y < slice_end; y++) { + for (int x = 0; x < width; x++) { + 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, +};