[FFmpeg-devel] avfilter: add pseudocolor filter

Message ID 20170724170200.10566-1-onemda@gmail.com
State New
Headers

Commit Message

Paul B Mahol July 24, 2017, 5:02 p.m. UTC
Signed-off-by: Paul B Mahol <onemda@gmail.com>
---
 libavfilter/Makefile         |   1 +
 libavfilter/allfilters.c     |   1 +
 libavfilter/vf_pseudocolor.c | 199 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 201 insertions(+)
 create mode 100644 libavfilter/vf_pseudocolor.c
  

Comments

Moritz Barsnick July 24, 2017, 8:18 p.m. UTC | #1
On Mon, Jul 24, 2017 at 19:02:00 +0200, Paul B Mahol wrote:
> +    .name          = "pseudocolor",
> +    .description   = NULL_IF_CONFIG_SMALL("Generate a pseudocolored video frames"),

I don't understand. If it generates, it sounds like it would be a vsrc_
filter. But it has inputs, and does something with them. Sorry, I can't
reverse engineer or read the docs. (Yes, if I google for it, I see what
VirtualDub does there... But something with palette files. Related?)

(Apart from that: Mixture of singular and plural in the description.)

Just wondering, and feeling a bit stupid, ;-)
Moritz
  
Paul B Mahol Aug. 17, 2017, 6:44 p.m. UTC | #2
Hi,

better patch attached.
  

Patch

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index ee16361..6b24745 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -258,6 +258,7 @@  OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
 OBJS-$(CONFIG_PP7_FILTER)                    += vf_pp7.o
 OBJS-$(CONFIG_PREMULTIPLY_FILTER)            += vf_premultiply.o framesync.o
 OBJS-$(CONFIG_PREWITT_FILTER)                += vf_convolution.o
+OBJS-$(CONFIG_PSEUDOCOLOR_FILTER)            += vf_pseudocolor.o
 OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o framesync.o
 OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
 OBJS-$(CONFIG_QP_FILTER)                     += vf_qp.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index b1c2d11..da570b2 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -269,6 +269,7 @@  static void register_all(void)
     REGISTER_FILTER(PP7,            pp7,            vf);
     REGISTER_FILTER(PREMULTIPLY,    premultiply,    vf);
     REGISTER_FILTER(PREWITT,        prewitt,        vf);
+    REGISTER_FILTER(PSEUDOCOLOR,    pseudocolor,    vf);
     REGISTER_FILTER(PSNR,           psnr,           vf);
     REGISTER_FILTER(PULLUP,         pullup,         vf);
     REGISTER_FILTER(QP,             qp,             vf);
diff --git a/libavfilter/vf_pseudocolor.c b/libavfilter/vf_pseudocolor.c
new file mode 100644
index 0000000..413b6e2
--- /dev/null
+++ b/libavfilter/vf_pseudocolor.c
@@ -0,0 +1,199 @@ 
+/*
+ * 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/attributes.h"
+#include "libavutil/common.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixdesc.h"
+#include "avfilter.h"
+#include "formats.h"
+#include "internal.h"
+#include "video.h"
+
+typedef struct PseudoColorContext {
+    const AVClass *class;
+    int is_16bit;
+    int nb_planes;
+    int color;
+    int linesize[4];
+    int width[4], height[4];
+    uint16_t lut[3][256*256];
+} PseudoColorContext;
+
+#define OFFSET(x) offsetof(PseudoColorContext, x)
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
+
+static const AVOption pseudocolor_options[] = {
+    { "color", "set a color", OFFSET(color), AV_OPT_TYPE_INT, {.i64=0}, 0, 10, .flags=FLAGS },
+    { NULL }
+};
+
+static const enum AVPixelFormat yuv_pix_fmts[] = { AV_PIX_FMT_YUV444P, AV_PIX_FMT_NONE };
+
+static int query_formats(AVFilterContext *ctx)
+{
+    const enum AVPixelFormat *pix_fmts = yuv_pix_fmts;
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
+    if (!fmts_list)
+        return AVERROR(ENOMEM);
+    return ff_set_common_formats(ctx, fmts_list);
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+    AVFilterContext *ctx = inlink->dst;
+    PseudoColorContext *s = ctx->priv;
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
+    int ret, hsub, vsub;
+
+    s->is_16bit = desc->comp[0].depth > 8;
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
+
+    if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
+        return ret;
+
+    hsub = desc->log2_chroma_w;
+    vsub = desc->log2_chroma_h;
+    s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
+    s->height[0] = s->height[3] = inlink->h;
+    s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
+    s->width[0]  = s->width[3]  = inlink->w;
+
+    return 0;
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+    AVFilterContext *ctx = inlink->dst;
+    PseudoColorContext *s = ctx->priv;
+    AVFilterLink *outlink = ctx->outputs[0];
+    AVFrame *out;
+    int x, y, plane;
+
+    if (av_frame_is_writable(in)) {
+        out = in;
+    } else {
+        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);
+    }
+
+    for (plane = 0; plane < s->nb_planes; plane++) {
+        const uint8_t *src = in->data[0];
+        uint8_t *dst = out->data[plane];
+
+        for (y = 0; y < s->height[plane]; y++) {
+            for (x = 0; x < s->width[plane]; x++) {
+                dst[x] = s->lut[plane][src[x]];
+            }
+            src += in->linesize[plane];
+            dst += out->linesize[plane];
+        }
+    }
+
+    if (out != in)
+        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 }
+};
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    PseudoColorContext *s = ctx->priv;
+    int i;
+
+    for (i = 0; i < 256; i++) {
+        float div = i / 256. * 6;
+        float idiv;
+        int asc = modff(div, &idiv) * 255;
+        int desc = 255 - asc;;
+
+        switch ((int)idiv) {
+        case 0:
+            s->lut[0][i] = i;
+            s->lut[1][i] = desc;
+            s->lut[2][i] = 0;
+            break;
+        case 1:
+            s->lut[0][i] = i;
+            s->lut[1][i] = 0;
+            s->lut[2][i] = asc;
+            break;
+        case 2:
+            s->lut[0][i] = i;
+            s->lut[1][i] = asc;
+            s->lut[2][i] = desc;
+            break;
+        case 3:
+            s->lut[0][i] = i;
+            s->lut[1][i] = desc;
+            s->lut[2][i] = asc;
+            break;
+        case 4:
+            s->lut[0][i] = i;
+            s->lut[1][i] = 0;
+            s->lut[2][i] = desc;
+            break;
+        case 5:
+            s->lut[0][i] = i;
+            s->lut[1][i] = asc;
+            s->lut[2][i] = 0;
+            break;
+        };
+    }
+
+    return 0;
+}
+
+AVFILTER_DEFINE_CLASS(pseudocolor);
+
+AVFilter ff_vf_pseudocolor = {
+    .name          = "pseudocolor",
+    .description   = NULL_IF_CONFIG_SMALL("Generate a pseudocolored video frames"),
+    .priv_size     = sizeof(PseudoColorContext),
+    .priv_class    = &pseudocolor_class,
+    .init          = init,
+    .query_formats = query_formats,
+    .inputs        = inputs,
+    .outputs       = outputs,
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
+};