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

2018-10-19 Thread Paul B Mahol
Signed-off-by: Paul B Mahol 
---
 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 00..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

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

2018-10-20 Thread Paul B Mahol
Signed-off-by: Paul B Mahol 
---
 doc/filters.texi  |  29 +
 libavfilter/Makefile  |   1 +
 libavfilter/allfilters.c  |   1 +
 libavfilter/vf_vibrance.c | 240 ++
 4 files changed, 271 insertions(+)
 create mode 100644 libavfilter/vf_vibrance.c

diff --git a/doc/filters.texi b/doc/filters.texi
index f314718a56..d49e30b168 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -17274,6 +17274,35 @@ and ones with constant delta pts.
 If there was frames with variable delta, than it will also show min and max 
delta
 encountered.
 
+@section vibrance
+
+Boost or cut saturation.
+
+The filter accepts the following options:
+@table @option
+@item intensity
+Set strength of boost if positive value or cut if negative value. Default is 0.
+Allowed range is from -2 to 2.
+
+@item rbal
+Set the red balance. Default is 1. Allowed range is from -10 to 10.
+
+@item gbal
+Set the green balance. Default is 1. Allowed range is from -10 to 10.
+
+@item bbal
+Set the blue balance. Default is 1. Allowed range is from -10 to 10.
+
+@item rlum
+Set the red luma coefficient.
+
+@item glum
+Set the green luma coefficient.
+
+@item blum
+Set the blue luma coefficient.
+@end table
+
 @anchor{vignette}
 @section vignette
 
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 00..d167ab8e2f
--- /dev/null
+++ b/libavfilter/vf_vibrance.c
@@ -0,0 +1,240 @@
+/*
+ * 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;
+float balance[3];
+float lcoeffs[3];
+
+int depth;
+
+int (*do_slice)(AVFilterContext *s, void *arg,
+int jobnr, int nb_jobs);
+} VibranceContext;
+
+static inline float lerpf(float v0, float v1, float f)
+{
+return v0 + (v1 - v0) * f;
+}
+
+static int vibrance_slice8(AVFilterContext *avctx, void *arg, int jobnr, int 
nb_jobs)
+{
+VibranceContext *s = avctx->priv;
+AVFrame *frame = arg;
+const int width = frame->width;
+const int height = frame->height;
+const float gc = s->lcoeffs[0];
+const float bc = s->lcoeffs[1];
+const float rc = s->lcoeffs[2];
+const float intensity = s->intensity;
+const float gintensity = intensity * s->balance[0];
+const float bintensity = intensity * s->balance[1];
+const float rintensity = intensity * s->balance[2];
+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 = sl

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

2018-10-22 Thread Paul B Mahol
On 10/20/18, Paul B Mahol  wrote:
> Signed-off-by: Paul B Mahol 
> ---
>  doc/filters.texi  |  29 +
>  libavfilter/Makefile  |   1 +
>  libavfilter/allfilters.c  |   1 +
>  libavfilter/vf_vibrance.c | 240 ++
>  4 files changed, 271 insertions(+)
>  create mode 100644 libavfilter/vf_vibrance.c
>

Will apply.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel