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

Paul B Mahol onemda at gmail.com
Fri Oct 19 22:27:58 EEST 2018


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 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,
+};
-- 
2.17.1



More information about the ffmpeg-devel mailing list