[FFmpeg-devel] [PATCH] avfilter: add colorcorrect filter

Paul B Mahol onemda at gmail.com
Mon Feb 1 18:52:09 EET 2021


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 doc/filters.texi              |  33 +++++
 libavfilter/Makefile          |   1 +
 libavfilter/allfilters.c      |   1 +
 libavfilter/vf_colorcorrect.c | 218 ++++++++++++++++++++++++++++++++++
 4 files changed, 253 insertions(+)
 create mode 100644 libavfilter/vf_colorcorrect.c

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}.
 
+ at section colorcorrect
+
+Adjust color white balance selectively for blacks and whites.
+This filter operates in YUV colorspace.
+
+The filter accepts the following options:
+
+ at table @option
+ at item rl
+Set the red shadow spot. Allowed range is from -1.0 to 1.0.
+Default value is 0.
+
+ at item bl
+Set the blue shadow spot. Allowed range is from -1.0 to 1.0.
+Default value is 0.
+
+ at item rh
+Set the red highlight spot. Allowed range is from -1.0 to 1.0.
+Default value is 0.
+
+ at item bh
+Set the red highlight spot. Allowed range is from -1.0 to 1.0.
+Default value is 0.
+
+ at item saturation
+Set the amount of saturation. Allowed range is from -3.0 to 3.0.
+Default value is 1.
+ at end table
+
+ at 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 3a4d3d8e31..68ff8e26d2 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 6d269b48bf..3b4620a5d7 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/vf_colorcorrect.c b/libavfilter/vf_colorcorrect.c
new file mode 100644
index 0000000000..edb48c3ff6
--- /dev/null
+++ b/libavfilter/vf_colorcorrect.c
@@ -0,0 +1,218 @@
+/*
+ * 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 <float.h>
+
+#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()                               \
+    ny = y;                                     \
+    nu = saturation * (u + y * (bh - bl) + bl); \
+    nv = saturation * (v + y * (rh - rl) + rl);
+
+static int colorcorrect_slice8(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
+{
+    ColorCorrectContext *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 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 bh = s->bh;
+    const float rh = s->rh;
+
+    for (int y = slice_start; y < slice_end; y++) {
+        for (int x = 0; x < width; x++) {
+            float y = yptr[x] / 255.f;
+            float u = uptr[x] / 255.f - .5f;
+            float v = vptr[x] / 255.f - .5f;
+            float ny, nu, nv;
+
+            PROCESS();
+
+            yptr[x] = av_clip_uint8( ny         * 255.f);
+            uptr[x] = av_clip_uint8((nu + 0.5f) * 255.f);
+            vptr[x] = av_clip_uint8((nv + 0.5f) * 255.f);
+        }
+
+        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 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 bh = s->bh;
+    const float rh = s->rh;
+
+    for (int y = slice_start; y < slice_end; y++) {
+        for (int x = 0; x < width; x++) {
+            float y = yptr[x] / max;
+            float u = uptr[x] / max - 0.5f;
+            float v = vptr[x] / max - 0.5f;
+            float ny, nu, nv;
+
+            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,
+};
-- 
2.17.1



More information about the ffmpeg-devel mailing list