[FFmpeg-cvslog] avfilter: add tlut2 filter

Paul B Mahol git at videolan.org
Fri Aug 4 12:47:50 EEST 2017


ffmpeg | branch: master | Paul B Mahol <onemda at gmail.com> | Fri Aug  4 10:29:12 2017 +0200| [80bc648e77972482843017aedf8795e5246ee819] | committer: Paul B Mahol

avfilter: add tlut2 filter

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=80bc648e77972482843017aedf8795e5246ee819
---

 Changelog                |   1 +
 doc/filters.texi         |   8 +-
 libavfilter/Makefile     |   1 +
 libavfilter/allfilters.c |   1 +
 libavfilter/version.h    |   4 +-
 libavfilter/vf_lut2.c    | 185 +++++++++++++++++++++++++++++++++++------------
 6 files changed, 150 insertions(+), 50 deletions(-)

diff --git a/Changelog b/Changelog
index 3074942021..86ce41807d 100644
--- a/Changelog
+++ b/Changelog
@@ -30,6 +30,7 @@ version <next>:
 - libvmaf video filter
 - Dolby E decoder and SMPTE 337M demuxer
 - unpremultiply video filter
+- tlut2 video filter
 
 version 3.3:
 - CrystalHD decoder moved to new decode API
diff --git a/doc/filters.texi b/doc/filters.texi
index 96abffbbdd..a920bf935e 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -9946,9 +9946,13 @@ lutyuv=u='(val-maxval/2)*2+maxval/2':v='(val-maxval/2)*2+maxval/2'
 @end example
 @end itemize
 
- at section lut2
+ at section lut2, tlut2
 
-Compute and apply a lookup table from two video inputs.
+The @code{lut2} filter takes two input streams and outputs one
+stream.
+
+The @code{tlut2} (time lut2) filter takes two consecutive frames
+from one single stream.
 
 This filter accepts the following parameters:
 @table @option
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index f0bb8e77e5..f615c669e7 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -312,6 +312,7 @@ OBJS-$(CONFIG_THRESHOLD_FILTER)              += vf_threshold.o framesync2.o
 OBJS-$(CONFIG_THUMBNAIL_FILTER)              += vf_thumbnail.o
 OBJS-$(CONFIG_TILE_FILTER)                   += vf_tile.o
 OBJS-$(CONFIG_TINTERLACE_FILTER)             += vf_tinterlace.o
+OBJS-$(CONFIG_TLUT2_FILTER)                  += vf_lut2.o framesync2.o
 OBJS-$(CONFIG_TRANSPOSE_FILTER)              += vf_transpose.o
 OBJS-$(CONFIG_TRIM_FILTER)                   += trim.o
 OBJS-$(CONFIG_UNPREMULTIPLY_FILTER)          += vf_premultiply.o framesync2.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 0fca662a23..32f26804b0 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -323,6 +323,7 @@ static void register_all(void)
     REGISTER_FILTER(THUMBNAIL,      thumbnail,      vf);
     REGISTER_FILTER(TILE,           tile,           vf);
     REGISTER_FILTER(TINTERLACE,     tinterlace,     vf);
+    REGISTER_FILTER(TLUT2,          tlut2,          vf);
     REGISTER_FILTER(TRANSPOSE,      transpose,      vf);
     REGISTER_FILTER(TRIM,           trim,           vf);
     REGISTER_FILTER(UNPREMULTIPLY,  unpremultiply,  vf);
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 04ea8b71f8..ff0467cc15 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -30,8 +30,8 @@
 #include "libavutil/version.h"
 
 #define LIBAVFILTER_VERSION_MAJOR   6
-#define LIBAVFILTER_VERSION_MINOR  96
-#define LIBAVFILTER_VERSION_MICRO 101
+#define LIBAVFILTER_VERSION_MINOR  97
+#define LIBAVFILTER_VERSION_MICRO 100
 
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
                                                LIBAVFILTER_VERSION_MINOR, \
diff --git a/libavfilter/vf_lut2.c b/libavfilter/vf_lut2.c
index f7e4a6a656..0859285382 100644
--- a/libavfilter/vf_lut2.c
+++ b/libavfilter/vf_lut2.c
@@ -61,6 +61,8 @@ typedef struct LUT2Context {
     int width[4], height[4];
     int nb_planes;
     int depth, depthx, depthy;
+    int tlut2;
+    AVFrame *prev_frame;        /* only used with tlut2 */
 
     void (*lut2)(struct LUT2Context *s, AVFrame *dst, AVFrame *srcx, AVFrame *srcy);
 
@@ -70,7 +72,7 @@ typedef struct LUT2Context {
 #define OFFSET(x) offsetof(LUT2Context, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
 
-static const AVOption lut2_options[] = {
+static const AVOption options[] = {
     { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
     { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
     { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]),  AV_OPT_TYPE_STRING, { .str = "x" }, .flags = FLAGS },
@@ -83,6 +85,8 @@ static av_cold void uninit(AVFilterContext *ctx)
     LUT2Context *s = ctx->priv;
     int i;
 
+    av_frame_free(&s->prev_frame);
+
     for (i = 0; i < 4; i++) {
         av_expr_free(s->comp_expr[i]);
         s->comp_expr[i] = NULL;
@@ -133,6 +137,11 @@ static int config_inputx(AVFilterLink *inlink)
     s->depthx = desc->comp[0].depth;
     s->var_values[VAR_BITDEPTHX] = s->depthx;
 
+    if (s->tlut2) {
+        s->depthy = desc->comp[0].depth;
+        s->var_values[VAR_BITDEPTHY] = s->depthy;
+    }
+
     return 0;
 }
 
@@ -232,13 +241,64 @@ static int config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
     LUT2Context *s = ctx->priv;
-    AVFilterLink *srcx = ctx->inputs[0];
-    AVFilterLink *srcy = ctx->inputs[1];
-    FFFrameSyncIn *in;
     int p, ret;
 
     s->depth = s->depthx + s->depthy;
 
+    s->lut2 = s->depth > 16 ? lut2_16bit : lut2_8bit;
+
+    for (p = 0; p < s->nb_planes; p++) {
+        s->lut[p] = av_malloc_array(1 << s->depth, sizeof(uint16_t));
+        if (!s->lut[p])
+            return AVERROR(ENOMEM);
+    }
+
+    for (p = 0; p < s->nb_planes; p++) {
+        double res;
+        int x, y;
+
+        /* create the parsed expression */
+        av_expr_free(s->comp_expr[p]);
+        s->comp_expr[p] = NULL;
+        ret = av_expr_parse(&s->comp_expr[p], s->comp_expr_str[p],
+                            var_names, NULL, NULL, NULL, NULL, 0, ctx);
+        if (ret < 0) {
+            av_log(ctx, AV_LOG_ERROR,
+                   "Error when parsing the expression '%s' for the component %d.\n",
+                   s->comp_expr_str[p], p);
+            return AVERROR(EINVAL);
+        }
+
+        /* compute the lut */
+        for (y = 0; y < (1 << s->depthx); y++) {
+            s->var_values[VAR_Y] = y;
+            for (x = 0; x < (1 << s->depthx); x++) {
+                s->var_values[VAR_X] = x;
+                res = av_expr_eval(s->comp_expr[p], s->var_values, s);
+                if (isnan(res)) {
+                    av_log(ctx, AV_LOG_ERROR,
+                           "Error when evaluating the expression '%s' for the values %d and %d for the component %d.\n",
+                           s->comp_expr_str[p], x, y, p);
+                    return AVERROR(EINVAL);
+                }
+
+                s->lut[p][(y << s->depthx) + x] = res;
+            }
+        }
+    }
+
+    return 0;
+}
+
+static int lut2_config_output(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    LUT2Context *s = ctx->priv;
+    AVFilterLink *srcx = ctx->inputs[0];
+    AVFilterLink *srcy = ctx->inputs[1];
+    FFFrameSyncIn *in;
+    int ret;
+
     if (srcx->format != srcy->format) {
         av_log(ctx, AV_LOG_ERROR, "inputs must be of same pixel format\n");
         return AVERROR(EINVAL);
@@ -281,47 +341,8 @@ static int config_output(AVFilterLink *outlink)
     s->fs.opaque   = s;
     s->fs.on_event = process_frame;
 
-    s->lut2 = s->depth > 16 ? lut2_16bit : lut2_8bit;
-
-    for (p = 0; p < s->nb_planes; p++) {
-        s->lut[p] = av_malloc_array(1 << s->depth, sizeof(uint16_t));
-        if (!s->lut[p])
-            return AVERROR(ENOMEM);
-    }
-
-    for (p = 0; p < s->nb_planes; p++) {
-        double res;
-        int x, y;
-
-        /* create the parsed expression */
-        av_expr_free(s->comp_expr[p]);
-        s->comp_expr[p] = NULL;
-        ret = av_expr_parse(&s->comp_expr[p], s->comp_expr_str[p],
-                            var_names, NULL, NULL, NULL, NULL, 0, ctx);
-        if (ret < 0) {
-            av_log(ctx, AV_LOG_ERROR,
-                   "Error when parsing the expression '%s' for the component %d.\n",
-                   s->comp_expr_str[p], p);
-            return AVERROR(EINVAL);
-        }
-
-        /* compute the lut */
-        for (y = 0; y < (1 << s->depthx); y++) {
-            s->var_values[VAR_Y] = y;
-            for (x = 0; x < (1 << s->depthx); x++) {
-                s->var_values[VAR_X] = x;
-                res = av_expr_eval(s->comp_expr[p], s->var_values, s);
-                if (isnan(res)) {
-                    av_log(ctx, AV_LOG_ERROR,
-                           "Error when evaluating the expression '%s' for the values %d and %d for the component %d.\n",
-                           s->comp_expr_str[p], x, y, p);
-                    return AVERROR(EINVAL);
-                }
-
-                s->lut[p][(y << s->depthx) + x] = res;
-            }
-        }
-    }
+    if ((ret = config_output(outlink)) < 0)
+        return ret;
 
     return ff_framesync2_configure(&s->fs);
 }
@@ -350,11 +371,13 @@ static const AVFilterPad outputs[] = {
     {
         .name          = "default",
         .type          = AVMEDIA_TYPE_VIDEO,
-        .config_props  = config_output,
+        .config_props  = lut2_config_output,
     },
     { NULL }
 };
 
+#define lut2_options options
+
 AVFILTER_DEFINE_CLASS(lut2);
 
 AVFilter ff_vf_lut2 = {
@@ -369,3 +392,73 @@ AVFilter ff_vf_lut2 = {
     .outputs       = outputs,
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
 };
+
+#if CONFIG_TLUT2_FILTER
+
+static av_cold int init(AVFilterContext *ctx)
+{
+    LUT2Context *s = ctx->priv;
+
+    s->tlut2 = !strcmp(ctx->filter->name, "tlut2");
+
+    return 0;
+}
+
+static int tlut2_filter_frame(AVFilterLink *inlink, AVFrame *frame)
+{
+    LUT2Context *s = inlink->dst->priv;
+    AVFilterLink *outlink = inlink->dst->outputs[0];
+
+    if (s->prev_frame) {
+        AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
+        if (!out) {
+            av_frame_free(&s->prev_frame);
+            s->prev_frame = frame;
+            return AVERROR(ENOMEM);
+        }
+        av_frame_copy_props(out, frame);
+        s->lut2(s, out, frame, s->prev_frame);
+        av_frame_free(&s->prev_frame);
+        s->prev_frame = frame;
+        return ff_filter_frame(outlink, out);
+    }
+    s->prev_frame = frame;
+    return 0;
+}
+
+#define tlut2_options options
+
+AVFILTER_DEFINE_CLASS(tlut2);
+
+static const AVFilterPad tlut2_inputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .filter_frame  = tlut2_filter_frame,
+        .config_props  = config_inputx,
+    },
+    { NULL }
+};
+
+static const AVFilterPad tlut2_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_VIDEO,
+        .config_props  = config_output,
+    },
+    { NULL }
+};
+
+AVFilter ff_vf_tlut2 = {
+    .name          = "tlut2",
+    .description   = NULL_IF_CONFIG_SMALL("Compute and apply a lookup table from two successive frames."),
+    .priv_size     = sizeof(LUT2Context),
+    .priv_class    = &tlut2_class,
+    .query_formats = query_formats,
+    .init          = init,
+    .uninit        = uninit,
+    .inputs        = tlut2_inputs,
+    .outputs       = tlut2_outputs,
+};
+
+#endif



More information about the ffmpeg-cvslog mailing list