Browse code

avfilter: add video vectorscope filter

Paul B Mahol authored on 2015/08/15 21:37:16
Showing 7 changed files
... ...
@@ -32,6 +32,7 @@ version <next>:
32 32
 - OS X VideoToolbox support
33 33
 - aphasemeter filter
34 34
 - showfreqs filter
35
+- vectorscope filter
35 36
 
36 37
 
37 38
 version 2.7:
... ...
@@ -10497,6 +10497,44 @@ Force a constant quantization parameter. If not set, the filter will use the QP
10497 10497
 from the video stream (if available).
10498 10498
 @end table
10499 10499
 
10500
+@section vectorscope
10501
+
10502
+Display 2 color component values in the two dimensional graph (which is called
10503
+a vectorscope).
10504
+
10505
+This filter accepts the following options:
10506
+
10507
+@table @option
10508
+@item mode
10509
+Set vectorscope mode.
10510
+
10511
+It accepts the following values:
10512
+@table @samp
10513
+@item gray
10514
+Gray values are displayed on graph, higher brightness means more pixels have
10515
+same component color value on location in graph. This is the default mode.
10516
+
10517
+@item color
10518
+Gray values are displayed on graph. Surrounding pixels values which are not
10519
+present in video frame are drawn in gradient of 2 color components which are
10520
+set by option @code{x} and @code{y}.
10521
+
10522
+@item color2
10523
+Actual color components values present in video frame are displayed on graph.
10524
+
10525
+@item color3
10526
+Similar as color2 but higher frequency of same values @code{x} and @code{y}
10527
+on graph increases value of another color component, which is luminance by
10528
+default values of @code{x} and @code{y}.
10529
+@end table
10530
+
10531
+@item x
10532
+Set which color component will be represented on X-axis. Default is @code{1}.
10533
+
10534
+@item y
10535
+Set which color component will be represented on Y-axis. Default is @code{2}.
10536
+@end table
10537
+
10500 10538
 @anchor{vidstabdetect}
10501 10539
 @section vidstabdetect
10502 10540
 
... ...
@@ -226,6 +226,7 @@ OBJS-$(CONFIG_TRANSPOSE_FILTER)              += vf_transpose.o
226 226
 OBJS-$(CONFIG_TRIM_FILTER)                   += trim.o
227 227
 OBJS-$(CONFIG_UNSHARP_FILTER)                += vf_unsharp.o
228 228
 OBJS-$(CONFIG_USPP_FILTER)                   += vf_uspp.o
229
+OBJS-$(CONFIG_VECTORSCOPE_FILTER)            += vf_vectorscope.o
229 230
 OBJS-$(CONFIG_VFLIP_FILTER)                  += vf_vflip.o
230 231
 OBJS-$(CONFIG_VIDSTABDETECT_FILTER)          += vidstabutils.o vf_vidstabdetect.o
231 232
 OBJS-$(CONFIG_VIDSTABTRANSFORM_FILTER)       += vidstabutils.o vf_vidstabtransform.o
... ...
@@ -241,6 +241,7 @@ void avfilter_register_all(void)
241 241
     REGISTER_FILTER(TRIM,           trim,           vf);
242 242
     REGISTER_FILTER(UNSHARP,        unsharp,        vf);
243 243
     REGISTER_FILTER(USPP,           uspp,           vf);
244
+    REGISTER_FILTER(VECTORSCOPE,    vectorscope,    vf);
244 245
     REGISTER_FILTER(VFLIP,          vflip,          vf);
245 246
     REGISTER_FILTER(VIDSTABDETECT,  vidstabdetect,  vf);
246 247
     REGISTER_FILTER(VIDSTABTRANSFORM, vidstabtransform, vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  5
33
-#define LIBAVFILTER_VERSION_MINOR  35
33
+#define LIBAVFILTER_VERSION_MINOR  36
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
... ...
@@ -179,6 +179,7 @@ static int config_output(AVFilterLink *outlink)
179 179
         break;
180 180
     case MODE_COLOR:
181 181
     case MODE_COLOR2:
182
+        av_log(ctx, AV_LOG_WARNING, "This mode is deprecated, use vectorscope filter instead.");
182 183
         outlink->h = outlink->w = 256;
183 184
         break;
184 185
     default:
185 186
new file mode 100644
... ...
@@ -0,0 +1,293 @@
0
+/*
1
+ * Copyright (c) 2015 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/avassert.h"
21
+#include "libavutil/opt.h"
22
+#include "libavutil/parseutils.h"
23
+#include "libavutil/pixdesc.h"
24
+#include "avfilter.h"
25
+#include "formats.h"
26
+#include "internal.h"
27
+#include "video.h"
28
+
29
+enum VectorscopeMode {
30
+    GRAY,
31
+    COLOR,
32
+    COLOR2,
33
+    COLOR3,
34
+    MODE_NB
35
+};
36
+
37
+typedef struct VectorscopeContext {
38
+    const AVClass *class;
39
+    int mode;
40
+    const uint8_t *bg_color;
41
+    int x, y, pd;
42
+    int is_yuv;
43
+} VectorscopeContext;
44
+
45
+#define OFFSET(x) offsetof(VectorscopeContext, x)
46
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
47
+
48
+static const AVOption vectorscope_options[] = {
49
+    { "mode", "set vectorscope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, MODE_NB-1, FLAGS, "mode"},
50
+    { "gray",   0, 0, AV_OPT_TYPE_CONST, {.i64=GRAY},   0, 0, FLAGS, "mode" },
51
+    { "color",  0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR},  0, 0, FLAGS, "mode" },
52
+    { "color2", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR2}, 0, 0, FLAGS, "mode" },
53
+    { "color3", 0, 0, AV_OPT_TYPE_CONST, {.i64=COLOR3}, 0, 0, FLAGS, "mode" },
54
+    { "x", "set color component on X axis", OFFSET(x), AV_OPT_TYPE_INT, {.i64=1}, 0, 2, FLAGS},
55
+    { "y", "set color component on Y axis", OFFSET(y), AV_OPT_TYPE_INT, {.i64=2}, 0, 2, FLAGS},
56
+    { NULL }
57
+};
58
+
59
+AVFILTER_DEFINE_CLASS(vectorscope);
60
+
61
+static const enum AVPixelFormat pix_fmts[] = {
62
+    AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P,
63
+    AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRP,
64
+    AV_PIX_FMT_NONE
65
+};
66
+
67
+static int query_formats(AVFilterContext *ctx)
68
+{
69
+    AVFilterFormats *fmts_list;
70
+
71
+    fmts_list = ff_make_format_list(pix_fmts);
72
+    if (!fmts_list)
73
+        return AVERROR(ENOMEM);
74
+    return ff_set_common_formats(ctx, fmts_list);
75
+}
76
+
77
+static const uint8_t black_yuva_color[4] = { 0, 127, 127, 0 };
78
+static const uint8_t black_gbrp_color[4] = { 0, 0, 0, 0 };
79
+
80
+static int config_input(AVFilterLink *inlink)
81
+{
82
+    VectorscopeContext *s = inlink->dst->priv;
83
+
84
+    if (s->mode == GRAY)
85
+        s->pd = 0;
86
+    else {
87
+        if ((s->x == 1 && s->y == 2) || (s->x == 2 && s->y == 1))
88
+            s->pd = 0;
89
+        else if ((s->x == 0 && s->y == 2) || (s->x == 2 && s->y == 0))
90
+            s->pd = 1;
91
+        else if ((s->x == 0 && s->y == 1) || (s->x == 1 && s->y == 0))
92
+            s->pd = 2;
93
+    }
94
+
95
+    switch (inlink->format) {
96
+    case AV_PIX_FMT_GBRAP:
97
+    case AV_PIX_FMT_GBRP:
98
+        s->bg_color = black_gbrp_color;
99
+        break;
100
+    default:
101
+        s->bg_color = black_yuva_color;
102
+    }
103
+
104
+    return 0;
105
+}
106
+
107
+static int config_output(AVFilterLink *outlink)
108
+{
109
+    VectorscopeContext *s = outlink->src->priv;
110
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
111
+    int depth = desc->comp[0].depth_minus1 + 1;
112
+
113
+    s->is_yuv = !(desc->flags & AV_PIX_FMT_FLAG_RGB);
114
+    outlink->h = outlink->w = 1 << depth;
115
+    outlink->sample_aspect_ratio = (AVRational){1,1};
116
+    return 0;
117
+}
118
+
119
+static void vectorscope(VectorscopeContext *s, AVFrame *in, AVFrame *out, int pd)
120
+{
121
+    const uint8_t * const *src = (const uint8_t * const *)in->data;
122
+    const int slinesizex = in->linesize[s->x];
123
+    const int slinesizey = in->linesize[s->y];
124
+    const int dlinesize = out->linesize[0];
125
+    int i, j, px = s->x, py = s->y;
126
+    const uint8_t *spx = src[px];
127
+    const uint8_t *spy = src[py];
128
+    uint8_t **dst = out->data;
129
+    uint8_t *dpx = dst[px];
130
+    uint8_t *dpy = dst[py];
131
+    uint8_t *dpd = dst[pd];
132
+
133
+    switch (s->mode) {
134
+    case COLOR:
135
+    case GRAY:
136
+        if (s->is_yuv) {
137
+            for (i = 0; i < in->height; i++) {
138
+                const int iwx = i * slinesizex;
139
+                const int iwy = i * slinesizey;
140
+                for (j = 0; j < in->width; j++) {
141
+                    const int x = spx[iwx + j];
142
+                    const int y = spy[iwy + j];
143
+                    const int pos = y * dlinesize + x;
144
+
145
+                    dpd[pos] = FFMIN(dpd[pos] + 1, 255);
146
+                    if (dst[3])
147
+                        dst[3][pos] = 255;
148
+                }
149
+            }
150
+        } else {
151
+            for (i = 0; i < in->height; i++) {
152
+                const int iwx = i * slinesizex;
153
+                const int iwy = i * slinesizey;
154
+                for (j = 0; j < in->width; j++) {
155
+                    const int x = spx[iwx + j];
156
+                    const int y = spy[iwy + j];
157
+                    const int pos = y * dlinesize + x;
158
+
159
+                    dst[0][pos] = FFMIN(dst[0][pos] + 1, 255);
160
+                    dst[1][pos] = FFMIN(dst[1][pos] + 1, 255);
161
+                    dst[2][pos] = FFMIN(dst[2][pos] + 1, 255);
162
+                    if (dst[3])
163
+                        dst[3][pos] = 255;
164
+                }
165
+            }
166
+        }
167
+        if (s->mode == COLOR) {
168
+            for (i = 0; i < out->height; i++) {
169
+                for (j = 0; j < out->width; j++) {
170
+                    if (!dpd[i * out->linesize[pd] + j]) {
171
+                        dpx[i * out->linesize[px] + j] = j;
172
+                        dpy[i * out->linesize[py] + j] = i;
173
+                    }
174
+                }
175
+            }
176
+        }
177
+        break;
178
+    case COLOR2:
179
+        if (s->is_yuv) {
180
+            for (i = 0; i < in->height; i++) {
181
+                const int iw1 = i * slinesizex;
182
+                const int iw2 = i * slinesizey;
183
+                for (j = 0; j < in->width; j++) {
184
+                    const int x = spx[iw1 + j];
185
+                    const int y = spy[iw2 + j];
186
+                    const int pos = y * dlinesize + x;
187
+
188
+                    if (!dpd[pos])
189
+                        dpd[pos] = FFABS(128 - x) + FFABS(128 - y);
190
+                    dpx[pos] = x;
191
+                    dpy[pos] = y;
192
+                    if (dst[3])
193
+                        dst[3][pos] = 255;
194
+                }
195
+            }
196
+        } else {
197
+            for (i = 0; i < in->height; i++) {
198
+                const int iw1 = i * slinesizex;
199
+                const int iw2 = i * slinesizey;
200
+                for (j = 0; j < in->width; j++) {
201
+                    const int x = spx[iw1 + j];
202
+                    const int y = spy[iw2 + j];
203
+                    const int pos = y * dlinesize + x;
204
+
205
+                    if (!dpd[pos])
206
+                        dpd[pos] = FFMIN(x + y, 255);
207
+                    dpx[pos] = x;
208
+                    dpy[pos] = y;
209
+                    if (dst[3])
210
+                        dst[3][pos] = 255;
211
+                }
212
+            }
213
+        }
214
+        break;
215
+    case COLOR3:
216
+        for (i = 0; i < in->height; i++) {
217
+            const int iw1 = i * slinesizex;
218
+            const int iw2 = i * slinesizey;
219
+            for (j = 0; j < in->width; j++) {
220
+                const int x = spx[iw1 + j];
221
+                const int y = spy[iw2 + j];
222
+                const int pos = y * dlinesize + x;
223
+
224
+                dpd[pos] = FFMIN(255, dpd[pos] + 1);
225
+                dpx[pos] = x;
226
+                dpy[pos] = y;
227
+                if (dst[3])
228
+                    dst[3][pos] = 255;
229
+            }
230
+        }
231
+        break;
232
+    default:
233
+        av_assert0(0);
234
+    }
235
+}
236
+
237
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
238
+{
239
+    AVFilterContext *ctx  = inlink->dst;
240
+    VectorscopeContext *s = ctx->priv;
241
+    AVFilterLink *outlink = ctx->outputs[0];
242
+    AVFrame *out;
243
+    uint8_t **dst;;
244
+    int i, k;
245
+
246
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
247
+    if (!out) {
248
+        av_frame_free(&in);
249
+        return AVERROR(ENOMEM);
250
+    }
251
+    out->pts = in->pts;
252
+    dst = out->data;
253
+
254
+    for (k = 0; k < 4 && dst[k]; k++)
255
+        for (i = 0; i < outlink->h ; i++)
256
+            memset(dst[k] + i * out->linesize[k],
257
+                   s->mode == COLOR && k == s->pd ? 0 : s->bg_color[k], outlink->w);
258
+
259
+    vectorscope(s, in, out, s->pd);
260
+
261
+    av_frame_free(&in);
262
+    return ff_filter_frame(outlink, out);
263
+}
264
+
265
+static const AVFilterPad inputs[] = {
266
+    {
267
+        .name         = "default",
268
+        .type         = AVMEDIA_TYPE_VIDEO,
269
+        .filter_frame = filter_frame,
270
+        .config_props = config_input,
271
+    },
272
+    { NULL }
273
+};
274
+
275
+static const AVFilterPad outputs[] = {
276
+    {
277
+        .name         = "default",
278
+        .type         = AVMEDIA_TYPE_VIDEO,
279
+        .config_props = config_output,
280
+    },
281
+    { NULL }
282
+};
283
+
284
+AVFilter ff_vf_vectorscope = {
285
+    .name          = "vectorscope",
286
+    .description   = NULL_IF_CONFIG_SMALL("Video vectorscope."),
287
+    .priv_size     = sizeof(VectorscopeContext),
288
+    .priv_class    = &vectorscope_class,
289
+    .query_formats = query_formats,
290
+    .inputs        = inputs,
291
+    .outputs       = outputs,
292
+};