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

Paul B Mahol onemda at gmail.com
Fri Nov 20 20:13:11 CET 2015


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavfilter/Makefile     |   1 +
 libavfilter/allfilters.c |   1 +
 libavfilter/vf_deflash.c | 199 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 201 insertions(+)
 create mode 100644 libavfilter/vf_deflash.c

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 1f4abeb..f801566 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -122,6 +122,7 @@ OBJS-$(CONFIG_CURVES_FILTER)                 += vf_curves.o
 OBJS-$(CONFIG_DCTDNOIZ_FILTER)               += vf_dctdnoiz.o
 OBJS-$(CONFIG_DEBAND_FILTER)                 += vf_deband.o
 OBJS-$(CONFIG_DECIMATE_FILTER)               += vf_decimate.o
+OBJS-$(CONFIG_DEFLASH_FILTER)                += vf_deflash.o
 OBJS-$(CONFIG_DEFLATE_FILTER)                += vf_neighbor.o
 OBJS-$(CONFIG_DEJUDDER_FILTER)               += vf_dejudder.o
 OBJS-$(CONFIG_DELOGO_FILTER)                 += vf_delogo.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 63b8fdb..daacbc3 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -144,6 +144,7 @@ void avfilter_register_all(void)
     REGISTER_FILTER(DCTDNOIZ,       dctdnoiz,       vf);
     REGISTER_FILTER(DEBAND,         deband,         vf);
     REGISTER_FILTER(DECIMATE,       decimate,       vf);
+    REGISTER_FILTER(DEFLASH,        deflash,        vf);
     REGISTER_FILTER(DEFLATE,        deflate,        vf);
     REGISTER_FILTER(DEJUDDER,       dejudder,       vf);
     REGISTER_FILTER(DELOGO,         delogo,         vf);
diff --git a/libavfilter/vf_deflash.c b/libavfilter/vf_deflash.c
new file mode 100644
index 0000000..3167989
--- /dev/null
+++ b/libavfilter/vf_deflash.c
@@ -0,0 +1,199 @@
+/*
+ * 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/pixdesc.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/internal.h"
+#include "avfilter.h"
+
+#define FF_BUFQUEUE_SIZE 13
+#include "bufferqueue.h"
+
+#include "internal.h"
+#include "video.h"
+
+typedef struct DeFlashContext {
+    const AVClass *class;
+
+    AVFrame *frame;
+    float fthrd[4];
+    int thrd[4];
+    float fthrr[4];
+    int thrr[4];
+
+    struct FFBufQueue q;
+
+    int planeheight[4];
+    int planewidth[4];
+} DeFlashContext;
+
+#define OFFSET(x) offsetof(DeFlashContext, x)
+#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
+
+static const AVOption deflash_options[] = {
+    { "0fd", "set flash threshold detection for 1st color component",  OFFSET(fthrd[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.2}, 0, 5, FLAGS },
+    { "0fr", "set ammount of flash reduction for 1st color component", OFFSET(fthrr[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.05}, 0, 1, FLAGS },
+    { "1fd", "set flash threshold detection for 2nd color component",  OFFSET(fthrd[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.2}, 0, 5, FLAGS },
+    { "1fr", "set ammount of flash reduction for 2nd color component", OFFSET(fthrr[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.05}, 0, 1, FLAGS },
+    { "2fd", "set flash threshold detection for 3rd color component",  OFFSET(fthrd[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.2}, 0, 5, FLAGS },
+    { "2fr", "set ammount of flash reduction for 3rd color component", OFFSET(fthrr[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.05}, 0, 1, FLAGS },
+    { NULL }
+};
+
+AVFILTER_DEFINE_CLASS(deflash);
+
+static int query_formats(AVFilterContext *ctx)
+{
+    static const enum AVPixelFormat pixel_fmts[] = {
+        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_NONE
+    };
+    AVFilterFormats *formats = ff_make_format_list(pixel_fmts);
+    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;
+    DeFlashContext *s = ctx->priv;
+
+    s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
+    s->planewidth[1]  = s->planewidth[2]  = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
+    s->planewidth[0]  = s->planewidth[3]  = inlink->w;
+
+    s->thrd[0] = s->fthrd[0] * 255;
+    s->thrd[1] = s->fthrd[1] * 255;
+    s->thrd[2] = s->fthrd[2] * 255;
+    s->thrr[0] = s->fthrr[0] * 255;
+    s->thrr[1] = s->fthrr[1] * 255;
+    s->thrr[2] = s->fthrr[2] * 255;
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    DeFlashContext *s = ctx->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+    AVFrame *out, *buf;
+    int ret, p;
+
+    if (!s->frame) {
+        s->frame = in;
+        return 0;
+    }
+    if (s->q.available != FF_BUFQUEUE_SIZE ) {
+        ff_bufqueue_add(ctx, &s->q, in);
+        return 0;
+    }
+
+    out = ff_get_video_buffer(outlink, in->width, in->height);
+    if (!out) {
+        av_frame_free(&in);
+        return AVERROR(ENOMEM);
+    }
+    av_frame_copy_props(out, in);
+    
+    for (p = 0; p < 3; p++) {
+        AVFrame *n = ff_bufqueue_peek(&s->q, 0);
+        const uint8_t *src = s->frame->data[p];
+        const uint8_t *next = n->data[p];
+        const int h = s->planeheight[p];
+        const int w = s->planewidth[p];
+        const int thrd = s->thrd[p];
+        const int thrr = s->thrr[p];
+        uint8_t *dst = out->data[p];
+        int y, x, z;
+
+        for (y = 0; y < h; y++) {
+            for (x = 0; x < w; x++) {
+                int diff = next[x] - src[x];
+                int t = 0;
+
+                for (z = 0; z < FF_BUFQUEUE_SIZE - 1; z++) {
+                    AVFrame *f1 = ff_bufqueue_peek(&s->q, z);
+                    AVFrame *f2 = ff_bufqueue_peek(&s->q, z+1);
+
+                    t += FFABS(f2->data[p][f2->linesize[p] * y + x] - f1->data[p][f1->linesize[p] * y + x]);
+                }
+
+                if (t > thrd)
+                    dst[x] = src[x] + thrr * diff / 255;
+                else
+                    dst[x] = next[x];
+            }
+
+            dst  += out->linesize[p];
+            src  += s->frame->linesize[p];
+            next += n->linesize[p];
+        }
+    }
+
+    buf = ff_bufqueue_get(&s->q);
+    av_frame_free(&buf);
+    ff_bufqueue_add(ctx, &s->q, in);
+    ret = ff_filter_frame(outlink, s->frame);
+    s->frame = out;
+
+    return ret;
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+    DeFlashContext *s = ctx->priv;
+}
+
+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_deflash = {
+    .name          = "deflash",
+    .description   = NULL_IF_CONFIG_SMALL("Deflash."),
+    .priv_size     = sizeof(DeFlashContext),
+    .priv_class    = &deflash_class,
+    .uninit        = uninit,
+    .query_formats = query_formats,
+    .inputs        = inputs,
+    .outputs       = outputs,
+};
-- 
1.9.1



More information about the ffmpeg-devel mailing list