Signed-off-by: Paul B Mahol <one...@gmail.com>
---
doc/filters.texi | 38 ++++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/vf_deflicker.c | 455 +++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 495 insertions(+)
create mode 100644 libavfilter/vf_deflicker.c
diff --git a/doc/filters.texi b/doc/filters.texi
index e002f25..83b8111 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -6253,6 +6253,44 @@ Limit the maximum change for each plane, default is
65535.
If 0, plane will remain unchanged.
@end table
+@section deflicker
+
+Remove temporal frame luminance variations.
+
+It accepts the following options:
+
+@table @option
+@item size, s
+Set moving-average filter size. Default is 5. Allowed range is 2 - 129.
+
+@item mode, m
+Set averaging mode to smooth temporal luminance variations.
+
+Available values are:
+@table @samp
+@item am
+Arithmetic mean
+
+@item gm
+Geometric mean
+
+@item hm
+Harmonic mean
+
+@item qm
+Quadratic mean
+
+@item cm
+Cubic mean
+
+@item pm
+Power mean
+
+@item median
+Median
+@end table
+@end table
+
@section dejudder
Remove judder produced by partially interlaced telecined content.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index d6daa7a..6de7cc0 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -155,6 +155,7 @@ OBJS-$(CONFIG_DCTDNOIZ_FILTER) +=
vf_dctdnoiz.o
OBJS-$(CONFIG_DEBAND_FILTER) += vf_deband.o
OBJS-$(CONFIG_DECIMATE_FILTER) += vf_decimate.o
OBJS-$(CONFIG_DEFLATE_FILTER) += vf_neighbor.o
+OBJS-$(CONFIG_DEFLICKER_FILTER) += vf_deflicker.o
OBJS-$(CONFIG_DEINTERLACE_QSV_FILTER) += vf_deinterlace_qsv.o
OBJS-$(CONFIG_DEINTERLACE_VAAPI_FILTER) += vf_deinterlace_vaapi.o
OBJS-$(CONFIG_DEJUDDER_FILTER) += vf_dejudder.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index ec6ec04..0e69f99 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -166,6 +166,7 @@ static void register_all(void)
REGISTER_FILTER(DEBAND, deband, vf);
REGISTER_FILTER(DECIMATE, decimate, vf);
REGISTER_FILTER(DEFLATE, deflate, vf);
+ REGISTER_FILTER(DEFLICKER, deflicker, vf);
REGISTER_FILTER(DEINTERLACE_QSV,deinterlace_qsv,vf);
REGISTER_FILTER(DEINTERLACE_VAAPI, deinterlace_vaapi, vf);
REGISTER_FILTER(DEJUDDER, dejudder, vf);
diff --git a/libavfilter/vf_deflicker.c b/libavfilter/vf_deflicker.c
new file mode 100644
index 0000000..bf0c847
--- /dev/null
+++ b/libavfilter/vf_deflicker.c
@@ -0,0 +1,455 @@
+/*
+ * Copyright (c) 2017 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/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/qsort.h"
+#include "avfilter.h"
+
+#define FF_BUFQUEUE_SIZE 129
+#include "bufferqueue.h"
+
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+#define SIZE FF_BUFQUEUE_SIZE
+
+enum smooth_mode {
+ ARITHMETIC_MEAN,
+ GEOMETRIC_MEAN,
+ HARMONIC_MEAN,
+ QUADRATIC_MEAN,
+ CUBIC_MEAN,
+ POWER_MEAN,
+ MEDIAN,
+ NB_SMOOTH_MODE,
+};
+
+typedef struct DeflickerContext {
+ const AVClass *class;
+
+ int size;
+ int mode;
+
+ int eof;
+ int depth;
+ int nb_planes;
+ int planewidth[4];
+ int planeheight[4];
+
+ uint64_t *histogram;
+ float luminance[SIZE];
+ float sorted[SIZE];
+
+ struct FFBufQueue q;
+ int available;
+
+ void (*get_factor)(AVFilterContext *ctx, float *f);
+ float (*calc_avgy)(AVFilterContext *ctx, AVFrame *in);
+ int (*deflicker)(AVFilterContext *ctx, AVFrame *in, AVFrame *out, float f);
+} DeflickerContext;
+
+#define OFFSET(x) offsetof(DeflickerContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption deflicker_options[] = {
+ { "size", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT,
{.i64=5}, 2, SIZE, FLAGS },
+ { "s", "set how many frames to use", OFFSET(size), AV_OPT_TYPE_INT,
{.i64=5}, 2, SIZE, FLAGS },
+ { "mode", "set how to smooth luminance", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0},
0, 1, FLAGS, "mode" },
+ { "m", "set how to smooth luminance", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0},
0, NB_SMOOTH_MODE-1, FLAGS, "mode" },
+ { "am", "arithmetic mean", 0, AV_OPT_TYPE_CONST, {.i64=ARITHMETIC_MEAN},
0, 0, FLAGS, "mode" },
+ { "gm", "geometric mean", 0, AV_OPT_TYPE_CONST, {.i64=GEOMETRIC_MEAN},
0, 0, FLAGS, "mode" },
+ { "hm", "harmonic mean", 0, AV_OPT_TYPE_CONST, {.i64=HARMONIC_MEAN},
0, 0, FLAGS, "mode" },
+ { "qm", "quadratic mean", 0, AV_OPT_TYPE_CONST, {.i64=QUADRATIC_MEAN},
0, 0, FLAGS, "mode" },
+ { "cm", "cubic mean", 0, AV_OPT_TYPE_CONST, {.i64=CUBIC_MEAN},
0, 0, FLAGS, "mode" },
+ { "pm", "power mean", 0, AV_OPT_TYPE_CONST, {.i64=POWER_MEAN},
0, 0, FLAGS, "mode" },
+ { "median", "median", 0, AV_OPT_TYPE_CONST, {.i64=MEDIAN},
0, 0, FLAGS, "mode" },
+ { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(deflicker);
+
+static int query_formats(AVFilterContext *ctx)
+{
+ static const enum AVPixelFormat pixel_fmts[] = {
+ AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY10,
+ AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY16,
+ AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
+ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
+ AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
+ AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
+ AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_YUVJ444P,
+ AV_PIX_FMT_YUVJ411P,
+ AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
+ AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
+ AV_PIX_FMT_YUV440P10,
+ AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV420P12,
+ AV_PIX_FMT_YUV440P12,
+ AV_PIX_FMT_YUV444P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV420P14,
+ AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
+ AV_PIX_FMT_NONE
+ };
+ AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
+ if (!formats)
+ return AVERROR(ENOMEM);
+ return ff_set_common_formats(ctx, formats);
+}
+
+static int deflicker8(AVFilterContext *ctx, AVFrame *in, AVFrame *out, float f)