Browse code

avfilter: add pseudocolor filter

Paul B Mahol authored on 2017/08/18 01:01:01
Showing 6 changed files
... ...
@@ -32,6 +32,7 @@ version <next>:
32 32
 - unpremultiply video filter
33 33
 - tlut2 video filter
34 34
 - floodfill video filter
35
+- pseudocolor video filter
35 36
 
36 37
 version 3.3:
37 38
 - CrystalHD decoder moved to new decode API
... ...
@@ -11785,6 +11785,51 @@ Set value which will be multiplied with filtered result.
11785 11785
 Set value which will be added to filtered result.
11786 11786
 @end table
11787 11787
 
11788
+@section pseudocolor
11789
+
11790
+Alter frame colors in video with pseudocolors.
11791
+
11792
+This filter accept the following options:
11793
+
11794
+@table @option
11795
+@item c0
11796
+set pixel first component expression
11797
+
11798
+@item c1
11799
+set pixel second component expression
11800
+
11801
+@item c2
11802
+set pixel third component expression
11803
+
11804
+@item c3
11805
+set pixel fourth component expression, corresponds to the alpha component
11806
+
11807
+@item i
11808
+set component to use as base for altering colors
11809
+@end table
11810
+
11811
+Each of them specifies the expression to use for computing the lookup table for
11812
+the corresponding pixel component values.
11813
+
11814
+The expressions can contain the following constants and functions:
11815
+
11816
+@table @option
11817
+@item w
11818
+@item h
11819
+The input width and height.
11820
+
11821
+@item val
11822
+The input value for the pixel component.
11823
+
11824
+@item ymin, umin, vmin, amin
11825
+The minimum allowed component value.
11826
+
11827
+@item ymax, umax, vmax, amax
11828
+The maximum allowed component value.
11829
+@end table
11830
+
11831
+All expressions default to "val".
11832
+
11788 11833
 @section psnr
11789 11834
 
11790 11835
 Obtain the average, maximum and minimum PSNR (Peak Signal to Noise
... ...
@@ -259,6 +259,7 @@ OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
259 259
 OBJS-$(CONFIG_PP7_FILTER)                    += vf_pp7.o
260 260
 OBJS-$(CONFIG_PREMULTIPLY_FILTER)            += vf_premultiply.o framesync2.o
261 261
 OBJS-$(CONFIG_PREWITT_FILTER)                += vf_convolution.o
262
+OBJS-$(CONFIG_PSEUDOCOLOR_FILTER)            += vf_pseudocolor.o
262 263
 OBJS-$(CONFIG_PSNR_FILTER)                   += vf_psnr.o dualinput.o framesync.o
263 264
 OBJS-$(CONFIG_PULLUP_FILTER)                 += vf_pullup.o
264 265
 OBJS-$(CONFIG_QP_FILTER)                     += vf_qp.o
... ...
@@ -270,6 +270,7 @@ static void register_all(void)
270 270
     REGISTER_FILTER(PP7,            pp7,            vf);
271 271
     REGISTER_FILTER(PREMULTIPLY,    premultiply,    vf);
272 272
     REGISTER_FILTER(PREWITT,        prewitt,        vf);
273
+    REGISTER_FILTER(PSEUDOCOLOR,    pseudocolor,    vf);
273 274
     REGISTER_FILTER(PSNR,           psnr,           vf);
274 275
     REGISTER_FILTER(PULLUP,         pullup,         vf);
275 276
     REGISTER_FILTER(QP,             qp,             vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR   6
33
-#define LIBAVFILTER_VERSION_MINOR  99
33
+#define LIBAVFILTER_VERSION_MINOR 100
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,256 @@
0
+/*
1
+ * Copyright (c) 2017 Paul B Mahol
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/attributes.h"
21
+#include "libavutil/common.h"
22
+#include "libavutil/eval.h"
23
+#include "libavutil/imgutils.h"
24
+#include "libavutil/opt.h"
25
+#include "libavutil/pixdesc.h"
26
+#include "avfilter.h"
27
+#include "formats.h"
28
+#include "internal.h"
29
+#include "video.h"
30
+
31
+static const char *const var_names[] = {
32
+    "w",        ///< width of the input video
33
+    "h",        ///< height of the input video
34
+    "val",      ///< input value for the pixel
35
+    "ymin",
36
+    "umin",
37
+    "vmin",
38
+    "amin",
39
+    "ymax",
40
+    "umax",
41
+    "vmax",
42
+    "amax",
43
+    NULL
44
+};
45
+
46
+enum var_name {
47
+    VAR_W,
48
+    VAR_H,
49
+    VAR_VAL,
50
+    VAR_YMIN,
51
+    VAR_UMIN,
52
+    VAR_VMIN,
53
+    VAR_AMIN,
54
+    VAR_YMAX,
55
+    VAR_UMAX,
56
+    VAR_VMAX,
57
+    VAR_AMAX,
58
+    VAR_VARS_NB
59
+};
60
+
61
+typedef struct PseudoColorContext {
62
+    const AVClass *class;
63
+    int max;
64
+    int index;
65
+    int nb_planes;
66
+    int color;
67
+    int linesize[4];
68
+    int width[4], height[4];
69
+    double var_values[VAR_VARS_NB];
70
+    char   *comp_expr_str[4];
71
+    AVExpr *comp_expr[4];
72
+    float lut[4][256];
73
+} PseudoColorContext;
74
+
75
+#define OFFSET(x) offsetof(PseudoColorContext, x)
76
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
77
+
78
+static const AVOption pseudocolor_options[] = {
79
+    { "c0", "set component #0 expression", OFFSET(comp_expr_str[0]), AV_OPT_TYPE_STRING, {.str="val"},   .flags = FLAGS },
80
+    { "c1", "set component #1 expression", OFFSET(comp_expr_str[1]), AV_OPT_TYPE_STRING, {.str="val"},   .flags = FLAGS },
81
+    { "c2", "set component #2 expression", OFFSET(comp_expr_str[2]), AV_OPT_TYPE_STRING, {.str="val"},   .flags = FLAGS },
82
+    { "c3", "set component #3 expression", OFFSET(comp_expr_str[3]), AV_OPT_TYPE_STRING, {.str="val"},   .flags = FLAGS },
83
+    { "i",  "set component as base",       OFFSET(index),            AV_OPT_TYPE_INT,    {.i64=0}, 0, 3, .flags = FLAGS },
84
+    { NULL }
85
+};
86
+
87
+static const enum AVPixelFormat pix_fmts[] = {
88
+    AV_PIX_FMT_GRAY8,
89
+    AV_PIX_FMT_YUV444P, AV_PIX_FMT_GBRP,
90
+    AV_PIX_FMT_YUVA444P, AV_PIX_FMT_GBRAP,
91
+    AV_PIX_FMT_NONE
92
+};
93
+
94
+static int query_formats(AVFilterContext *ctx)
95
+{
96
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
97
+    if (!fmts_list)
98
+        return AVERROR(ENOMEM);
99
+    return ff_set_common_formats(ctx, fmts_list);
100
+}
101
+
102
+static int config_input(AVFilterLink *inlink)
103
+{
104
+    AVFilterContext *ctx = inlink->dst;
105
+    PseudoColorContext *s = ctx->priv;
106
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
107
+    int depth, ret, hsub, vsub, color;
108
+
109
+    depth = desc->comp[0].depth;
110
+    s->max = (1 << depth) - 1;
111
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
112
+
113
+    if (s->index >= s->nb_planes) {
114
+        av_log(ctx, AV_LOG_ERROR, "index out of allowed range\n");
115
+        return AVERROR(EINVAL);
116
+    }
117
+
118
+    if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
119
+        return ret;
120
+
121
+    hsub = desc->log2_chroma_w;
122
+    vsub = desc->log2_chroma_h;
123
+    s->height[1] = s->height[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
124
+    s->height[0] = s->height[3] = inlink->h;
125
+    s->width[1]  = s->width[2]  = AV_CEIL_RSHIFT(inlink->w, hsub);
126
+    s->width[0]  = s->width[3]  = inlink->w;
127
+
128
+    s->var_values[VAR_W] = inlink->w;
129
+    s->var_values[VAR_H] = inlink->h;
130
+
131
+    s->var_values[VAR_YMIN] = 16 * (1 << (depth - 8));
132
+    s->var_values[VAR_UMIN] = 16 * (1 << (depth - 8));
133
+    s->var_values[VAR_VMIN] = 16 * (1 << (depth - 8));
134
+    s->var_values[VAR_AMIN] = 0;
135
+    s->var_values[VAR_YMAX] = 235 * (1 << (depth - 8));
136
+    s->var_values[VAR_UMAX] = 240 * (1 << (depth - 8));
137
+    s->var_values[VAR_VMAX] = 240 * (1 << (depth - 8));
138
+    s->var_values[VAR_AMAX] = s->max;
139
+
140
+    for (color = 0; color < s->nb_planes; color++) {
141
+        double res;
142
+        int val;
143
+
144
+        /* create the parsed expression */
145
+        av_expr_free(s->comp_expr[color]);
146
+        s->comp_expr[color] = NULL;
147
+        ret = av_expr_parse(&s->comp_expr[color], s->comp_expr_str[color],
148
+                            var_names, NULL, NULL, NULL, NULL, 0, ctx);
149
+        if (ret < 0) {
150
+            av_log(ctx, AV_LOG_ERROR,
151
+                   "Error when parsing the expression '%s' for the component %d and color %d.\n",
152
+                   s->comp_expr_str[color], color, color);
153
+            return AVERROR(EINVAL);
154
+        }
155
+
156
+        /* compute the lut */
157
+        for (val = 0; val < FF_ARRAY_ELEMS(s->lut[color]); val++) {
158
+            s->var_values[VAR_VAL] = val;
159
+
160
+            res = av_expr_eval(s->comp_expr[color], s->var_values, s);
161
+            if (isnan(res)) {
162
+                av_log(ctx, AV_LOG_ERROR,
163
+                       "Error when evaluating the expression '%s' for the value %d for the component %d.\n",
164
+                       s->comp_expr_str[color], val, color);
165
+                return AVERROR(EINVAL);
166
+            }
167
+            s->lut[color][val] = res;
168
+        }
169
+    }
170
+
171
+    return 0;
172
+}
173
+
174
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
175
+{
176
+    AVFilterContext *ctx = inlink->dst;
177
+    PseudoColorContext *s = ctx->priv;
178
+    AVFilterLink *outlink = ctx->outputs[0];
179
+    AVFrame *out;
180
+    int x, y, plane;
181
+
182
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
183
+    if (!out) {
184
+        av_frame_free(&in);
185
+        return AVERROR(ENOMEM);
186
+    }
187
+    av_frame_copy_props(out, in);
188
+
189
+    for (plane = 0; plane < s->nb_planes; plane++) {
190
+        const uint8_t *index = in->data[s->index];
191
+        const uint8_t *src = in->data[plane];
192
+        uint8_t *dst = out->data[plane];
193
+
194
+        for (y = 0; y < s->height[plane]; y++) {
195
+            for (x = 0; x < s->width[plane]; x++) {
196
+                int v = s->lut[plane][index[x]];
197
+
198
+                if (v >= 0 && v <= s->max) {
199
+                    dst[x] = v;
200
+                } else {
201
+                    dst[x] = src[x];
202
+                }
203
+            }
204
+            index += in->linesize[s->index];
205
+            src += in->linesize[plane];
206
+            dst += out->linesize[plane];
207
+        }
208
+    }
209
+
210
+    av_frame_free(&in);
211
+    return ff_filter_frame(outlink, out);
212
+}
213
+
214
+static const AVFilterPad inputs[] = {
215
+    {
216
+        .name         = "default",
217
+        .type         = AVMEDIA_TYPE_VIDEO,
218
+        .filter_frame = filter_frame,
219
+        .config_props = config_input,
220
+    },
221
+    { NULL }
222
+};
223
+
224
+static const AVFilterPad outputs[] = {
225
+    {
226
+        .name = "default",
227
+        .type = AVMEDIA_TYPE_VIDEO,
228
+    },
229
+    { NULL }
230
+};
231
+
232
+static av_cold void uninit(AVFilterContext *ctx)
233
+{
234
+    PseudoColorContext *s = ctx->priv;
235
+    int i;
236
+
237
+    for (i = 0; i < 4; i++) {
238
+        av_expr_free(s->comp_expr[i]);
239
+        s->comp_expr[i] = NULL;
240
+    }
241
+}
242
+
243
+AVFILTER_DEFINE_CLASS(pseudocolor);
244
+
245
+AVFilter ff_vf_pseudocolor = {
246
+    .name          = "pseudocolor",
247
+    .description   = NULL_IF_CONFIG_SMALL("Make pseudocolored video frames."),
248
+    .priv_size     = sizeof(PseudoColorContext),
249
+    .priv_class    = &pseudocolor_class,
250
+    .uninit        = uninit,
251
+    .query_formats = query_formats,
252
+    .inputs        = inputs,
253
+    .outputs       = outputs,
254
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
255
+};