From 8bee929b86c23b8b398780b2d5ec5e37dc2fb66a Mon Sep 17 00:00:00 2001
From: Paul B Mahol <onemda@gmail.com>
Date: Sun, 14 Aug 2016 18:10:55 +0200
Subject: [PATCH] avfilter: add bitplanenoise filter

---
 doc/filters.texi               |  11 ++
 libavfilter/Makefile           |   1 +
 libavfilter/allfilters.c       |   1 +
 libavfilter/vf_bitplanenoise.c | 258 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 271 insertions(+)
 create mode 100644 libavfilter/vf_bitplanenoise.c

diff --git a/doc/filters.texi b/doc/filters.texi
index 3a9e251..e65cb48 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -4410,6 +4410,17 @@ The filter accepts the following option:
 Set the minimal luminance value. Default is @code{16}.
 @end table
 
+@section bitplanenoise
+
+Show & measure bit plane noise.
+
+The filter accepts the following option:
+
+@table @option
+@item bitplane
+Set which plane to analyze & filter.
+@end table
+
 @section blackdetect
 
 Detect video intervals that are (almost) completely black. Can be
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 0d94f84..8cf1cab 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -125,6 +125,7 @@ OBJS-$(CONFIG_ALPHAMERGE_FILTER)             += vf_alphamerge.o
 OBJS-$(CONFIG_ATADENOISE_FILTER)             += vf_atadenoise.o
 OBJS-$(CONFIG_BBOX_FILTER)                   += bbox.o vf_bbox.o
 OBJS-$(CONFIG_BENCH_FILTER)                  += f_bench.o
+OBJS-$(CONFIG_BITPLANENOISE_FILTER)          += vf_bitplanenoise.o
 OBJS-$(CONFIG_BLACKDETECT_FILTER)            += vf_blackdetect.o
 
 # video filters
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index feed4f8..74e56cc 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -143,6 +143,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(ASS,            ass,            vf);
     REGISTER_FILTER(BENCH,          bench,          vf);
     REGISTER_FILTER(BBOX,           bbox,           vf);
+    REGISTER_FILTER(BITPLANENOISE,  bitplanenoise,  vf);
     REGISTER_FILTER(BLACKDETECT,    blackdetect,    vf);
     REGISTER_FILTER(BLACKFRAME,     blackframe,     vf);
     REGISTER_FILTER(BLEND,          blend,          vf);
diff --git a/libavfilter/vf_bitplanenoise.c b/libavfilter/vf_bitplanenoise.c
new file mode 100644
index 0000000..7badad2
--- /dev/null
+++ b/libavfilter/vf_bitplanenoise.c
@@ -0,0 +1,258 @@
+/*
+ * Copyright (c) 2016 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 BPNContext {
+    const AVClass *class;
+
+    int bitplane;
+
+    int nb_planes;
+    int planeheight[4];
+    int planewidth[4];
+    int depth;
+} BPNContext;
+
+#define OFFSET(x) offsetof(BPNContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+static const AVOption bitplanenoise_options[] = {
+    { "bitplane", "set bit plane to filter",  OFFSET(bitplane), AV_OPT_TYPE_INT, {.i64=1}, 1, 16, FLAGS},
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(bitplanenoise);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pixfmts[] = {
+        AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV411P,
+        AV_PIX_FMT_YUV440P,
+        AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ411P,
+        AV_PIX_FMT_YUVJ440P,
+        AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV420P9,
+        AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV420P10,
+        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_YUV444P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV420P16,
+        AV_PIX_FMT_GRAY8,
+        AV_PIX_FMT_GRAY16,
+        AV_PIX_FMT_NONE
+    };
+
+    AVFilterFormats *formats = ff_make_format_list(pixfmts);
+    if (!formats)
+        return AVERROR(ENOMEM);
+    return ff_set_common_formats(ctx, formats);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    AVFilterContext *ctx = inlink->dst;
+    BPNContext *s = ctx->priv;
+
+    s->nb_planes = desc->nb_components;
+
+    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;
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    AVFilterLink *outlink = ctx->outputs[0];
+    BPNContext *s = ctx->priv;
+    const int mask = (1 << (s->bitplane - 1));
+    const int factor = (1 << s->depth) - 1;
+    float stats[4] = { 0 };
+    char metabuf[128];
+    int plane, y, x, bit;
+    AVFrame *out;
+
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+    if (!out) {
+        av_frame_free(&in);
+        return AVERROR(ENOMEM);
+    }
+    av_frame_copy_props(out, in);
+
+    if (s->depth <= 8) {
+        for (plane = 0; plane < s->nb_planes; plane++) {
+            const int linesize = in->linesize[plane];
+            const int dlinesize = out->linesize[plane];
+            uint8_t *val = in->data[plane];
+            uint8_t *dst = out->data[plane];
+
+            for (y = 0; y < s->planeheight[plane] - 1; y++) {
+                bit = (((val[0] & mask) == (val[0 + 1] & mask)) +
+                       ((val[0] & mask) == (val[0 + 1 + linesize] & mask)) +
+                       ((val[0] & mask) == (val[0 + linesize] & mask))) > 1;
+                dst[0] = factor * bit;
+                stats[plane] += bit;
+
+                for (x = 1; x < s->planewidth[plane] - 1; x++) {
+                    bit = (((val[x] & mask) == (val[x - 1] & mask)) +
+                           ((val[x] & mask) == (val[x + 1] & mask)) +
+                           ((val[x] & mask) == (val[x + linesize] & mask))) > 1;
+                    dst[x] = factor * bit;
+                    stats[plane] += bit;
+                }
+
+                bit = (((val[x] & mask) == (val[x - 1] & mask)) +
+                       ((val[x] & mask) == (val[x - 1 - linesize] & mask)) +
+                       ((val[x] & mask) == (val[x - linesize] & mask))) > 1;
+                dst[x] = factor * bit;
+                stats[plane] += bit;
+
+                val += linesize;
+                dst += dlinesize;
+            }
+
+            bit = (((val[0] & mask) == (val[0 + 1] & mask)) +
+                   ((val[0] & mask) == (val[0 + 1 - linesize] & mask)) +
+                   ((val[0] & mask) == (val[0 - linesize] & mask))) > 1;
+            dst[0] = factor * bit;
+            stats[plane] += bit;
+
+            for (x = 1; x < s->planewidth[plane] - 1; x++) {
+                bit = (((val[x] & mask) == (val[x - 1] & mask)) +
+                       ((val[x] & mask) == (val[x + 1] & mask)) +
+                       ((val[x] & mask) == (val[x - linesize] & mask))) > 1;
+                dst[x] = factor * bit;
+                stats[plane] += bit;
+            }
+
+            bit = (((val[x] & mask) == (val[x - 1] & mask)) +
+                   ((val[x] & mask) == (val[x - 1 - linesize] & mask)) +
+                   ((val[x] & mask) == (val[x - linesize] & mask))) > 1;
+            dst[x] = factor * bit;
+            stats[plane] += bit;
+        }
+    } else {
+        for (plane = 0; plane < s->nb_planes; plane++) {
+            const int linesize = in->linesize[plane] / 2;
+            const int dlinesize = out->linesize[plane] / 2;
+            uint16_t *val = (uint16_t *)in->data[plane];
+            uint16_t *dst = (uint16_t *)out->data[plane];
+
+            val = (uint16_t *)in->data[plane];
+            for (y = 0; y < s->planeheight[plane] - 1; y++) {
+                bit = (((val[0] & mask) == (val[0 + 1] & mask)) +
+                       ((val[0] & mask) == (val[0 + 1 + linesize] & mask)) +
+                       ((val[0] & mask) == (val[0 + linesize] & mask))) > 1;
+                dst[0] = factor * bit;
+                stats[plane] += bit;
+
+                for (x = 1; x < s->planewidth[plane] - 1; x++) {
+                    bit = (((val[x] & mask) == (val[x - 1] & mask)) +
+                           ((val[x] & mask) == (val[x + 1] & mask)) +
+                           ((val[x] & mask) == (val[x + linesize] & mask))) > 1;
+                    dst[x] = factor * bit;
+                    stats[plane] += bit;
+                }
+
+                bit = (((val[x] & mask) == (val[x - 1] & mask)) +
+                       ((val[x] & mask) == (val[x - 1 - linesize] & mask)) +
+                       ((val[x] & mask) == (val[x - linesize] & mask))) > 1;
+                dst[x] = factor * bit;
+                stats[plane] += bit;
+
+                val += linesize;
+                dst += dlinesize;
+            }
+
+            bit = (((val[0] & mask) == (val[0 + 1] & mask)) +
+                   ((val[0] & mask) == (val[0 + 1 - linesize] & mask)) +
+                   ((val[0] & mask) == (val[0 - linesize] & mask))) > 1;
+            dst[0] = factor * bit;
+            stats[plane] += bit;
+
+            for (x = 1; x < s->planewidth[plane] - 1; x++) {
+                bit = (((val[x] & mask) == (val[x - 1] & mask)) +
+                       ((val[x] & mask) == (val[x + 1] & mask)) +
+                       ((val[x] & mask) == (val[x - linesize] & mask))) > 1;
+                dst[x] = factor * bit;
+                stats[plane] += bit;
+            }
+
+            bit = (((val[x] & mask) == (val[x - 1] & mask)) +
+                   ((val[x] & mask) == (val[x - 1 - linesize] & mask)) +
+                   ((val[x] & mask) == (val[x - linesize] & mask))) > 1;
+            dst[x] = factor * bit;
+            stats[plane] += bit;
+        }
+    }
+
+    for (plane = 0; plane < s->nb_planes; plane++) {
+        char key[32];
+
+        stats[plane] /= s->planewidth[plane] * s->planeheight[plane];
+        snprintf(key, sizeof(key), "lavfi.bitplanenoise.%d.%d", plane, s->bitplane);
+        snprintf(metabuf, sizeof(metabuf), "%f", 1. - stats[plane]);
+        av_dict_set(&out->metadata, key, metabuf, 0);
+    }
+
+    av_frame_free(&in);
+
+    return ff_filter_frame(outlink, out);
+}
+
+static const AVFilterPad inputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_VIDEO,
+        .filter_frame   = filter_frame,
+        .config_props   = config_input,
+    },
+    { NULL }
+};
+
+static const AVFilterPad outputs[] = {
+    {
+        .name = "default",
+        .type = AVMEDIA_TYPE_VIDEO,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_bitplanenoise = {
+    .name           = "bitplanenoise",
+    .description    = NULL_IF_CONFIG_SMALL("Show & measure bit plane noise."),
+    .priv_size      = sizeof(BPNContext),
+    .query_formats  = query_formats,
+    .inputs         = inputs,
+    .outputs        = outputs,
+    .priv_class     = &bitplanenoise_class,
+    .flags          = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};
-- 
2.8.3

