Browse code

avfilter: add datascope filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2016/02/24 06:41:07
Showing 6 changed files
... ...
@@ -7,6 +7,7 @@ version <next>:
7 7
 - loop video filter and aloop audio filter
8 8
 - Bob Weaver deinterlacing filter
9 9
 - firequalizer filter
10
+- datascope filter
10 11
 
11 12
 
12 13
 version 3.0:
... ...
@@ -5240,6 +5240,43 @@ curves=psfile='MyCurvesPresets/purple.acv':green='0.45/0.53'
5240 5240
 @end example
5241 5241
 @end itemize
5242 5242
 
5243
+@section datascope
5244
+
5245
+Video data analysis filter.
5246
+
5247
+This filter shows hexadecimal pixel values of part of video.
5248
+
5249
+The filter accepts the following options:
5250
+
5251
+@table @option
5252
+@item size, s
5253
+Set output video size.
5254
+
5255
+@item x
5256
+Set x offset from where to pick pixels.
5257
+
5258
+@item y
5259
+Set y offset from where to pick pixels.
5260
+
5261
+@item mode
5262
+Set scope mode, can be one of the following:
5263
+@table @samp
5264
+@item mono
5265
+Draw hexadecimal pixel values with white color on black background.
5266
+
5267
+@item color
5268
+Draw hexadecimal pixel values with input video pixel color on black
5269
+background.
5270
+
5271
+@item color2
5272
+Draw hexadecimal pixel values on color background picked from input video,
5273
+the text color is picked in such way so its always visible.
5274
+@end table
5275
+
5276
+@item axis
5277
+Draw rows and columns numbers on left and top of video.
5278
+@end table
5279
+
5243 5280
 @section dctdnoiz
5244 5281
 
5245 5282
 Denoise frames using 2D DCT (frequency domain filtering).
... ...
@@ -134,6 +134,7 @@ OBJS-$(CONFIG_COVER_RECT_FILTER)             += vf_cover_rect.o lavfutils.o
134 134
 OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
135 135
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
136 136
 OBJS-$(CONFIG_CURVES_FILTER)                 += vf_curves.o
137
+OBJS-$(CONFIG_DATASCOPE_FILTER)              += vf_datascope.o
137 138
 OBJS-$(CONFIG_DCTDNOIZ_FILTER)               += vf_dctdnoiz.o
138 139
 OBJS-$(CONFIG_DEBAND_FILTER)                 += vf_deband.o
139 140
 OBJS-$(CONFIG_DECIMATE_FILTER)               += vf_decimate.o
... ...
@@ -155,6 +155,7 @@ void avfilter_register_all(void)
155 155
     REGISTER_FILTER(CROP,           crop,           vf);
156 156
     REGISTER_FILTER(CROPDETECT,     cropdetect,     vf);
157 157
     REGISTER_FILTER(CURVES,         curves,         vf);
158
+    REGISTER_FILTER(DATASCOPE,      datascope,      vf);
158 159
     REGISTER_FILTER(DCTDNOIZ,       dctdnoiz,       vf);
159 160
     REGISTER_FILTER(DEBAND,         deband,         vf);
160 161
     REGISTER_FILTER(DECIMATE,       decimate,       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  36
33
+#define LIBAVFILTER_VERSION_MINOR  37
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,398 @@
0
+/*
1
+ * Copyright (c) 2016 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/intreadwrite.h"
22
+#include "libavutil/opt.h"
23
+#include "libavutil/parseutils.h"
24
+#include "libavutil/pixdesc.h"
25
+#include "libavutil/xga_font_data.h"
26
+#include "avfilter.h"
27
+#include "drawutils.h"
28
+#include "formats.h"
29
+#include "internal.h"
30
+#include "video.h"
31
+
32
+typedef struct DatascopeContext {
33
+    const AVClass *class;
34
+    int ow, oh;
35
+    int x, y;
36
+    int mode;
37
+    int axis;
38
+
39
+    int nb_planes;
40
+    int nb_comps;
41
+    int chars;
42
+    FFDrawContext draw;
43
+    FFDrawColor yellow;
44
+    FFDrawColor white;
45
+    FFDrawColor black;
46
+    FFDrawColor gray;
47
+
48
+    int (*filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
49
+} DatascopeContext;
50
+
51
+#define OFFSET(x) offsetof(DatascopeContext, x)
52
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
53
+
54
+static const AVOption datascope_options[] = {
55
+    { "size", "set output size", OFFSET(ow),   AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
56
+    { "s",    "set output size", OFFSET(ow),   AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
57
+    { "x",    "set x offset", OFFSET(x),    AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
58
+    { "y",    "set y offset", OFFSET(y),    AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
59
+    { "mode", "set scope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "mode" },
60
+    {   "mono",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
61
+    {   "color",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
62
+    {   "color2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode" },
63
+    { "axis",    "draw column/row numbers", OFFSET(axis), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
64
+    { NULL }
65
+};
66
+
67
+AVFILTER_DEFINE_CLASS(datascope);
68
+
69
+static int query_formats(AVFilterContext *ctx)
70
+{
71
+    return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
72
+}
73
+
74
+static void draw_text(DatascopeContext *s, AVFrame *frame, FFDrawColor *color,
75
+                      int x0, int y0, const uint8_t *text, int vertical)
76
+{
77
+    int x = x0;
78
+
79
+    for (; *text; text++) {
80
+        if (*text == '\n') {
81
+            x = x0;
82
+            y0 += 8;
83
+            continue;
84
+        }
85
+        ff_blend_mask(&s->draw, color, frame->data, frame->linesize,
86
+                      frame->width, frame->height,
87
+                      avpriv_cga_font + *text * 8, 1, 8, 8, 0, 0, x, y0);
88
+        if (vertical) {
89
+            x = x0;
90
+            y0 += 8;
91
+        } else {
92
+            x += 8;
93
+        }
94
+    }
95
+}
96
+
97
+static void pick_color(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value)
98
+{
99
+    int p, i;
100
+
101
+    color->rgba[3] = 255;
102
+    for (p = 0; p < draw->nb_planes; p++) {
103
+        if (draw->desc->comp[p].depth == 8) {
104
+            if (draw->nb_planes == 1) {
105
+                for (i = 0; i < 4; i++) {
106
+                    value[i] = in->data[0][y * in->linesize[0] + x * draw->pixelstep[0] + i];
107
+                    color->comp[0].u8[i] = value[i];
108
+                }
109
+            } else {
110
+                value[p] = in->data[p][(y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p])];
111
+                color->comp[p].u8[0] = value[p];
112
+            }
113
+        } else {
114
+            if (draw->nb_planes == 1) {
115
+                for (i = 0; i < 4; i++) {
116
+                    value[i] = AV_RL16(in->data[0] + y * in->linesize[0] + x * draw->pixelstep[0] + i * 2);
117
+                    color->comp[0].u16[i] = value[i];
118
+                }
119
+            } else {
120
+                value[p] = AV_RL16(in->data[p] + (y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p]) * 2);
121
+                color->comp[p].u16[0] = value[p];
122
+            }
123
+        }
124
+    }
125
+}
126
+
127
+static void reverse_color(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse)
128
+{
129
+    int p;
130
+
131
+    reverse->rgba[3] = 255;
132
+    for (p = 0; p < draw->nb_planes; p++) {
133
+        if (draw->desc->comp[p].depth == 8) {
134
+            reverse->comp[p].u8[0] = color->comp[p].u8[0] > 127 ? 0 : 255;
135
+            reverse->comp[p].u8[1] = color->comp[p].u8[1] > 127 ? 0 : 255;
136
+            reverse->comp[p].u8[2] = color->comp[p].u8[2] > 127 ? 0 : 255;
137
+            reverse->comp[p].u8[3] = color->comp[p].u8[3] > 127 ? 0 : 255;
138
+        } else {
139
+            const unsigned max = (1 << draw->desc->comp[p].depth) - 1;
140
+            const unsigned mid = (max + 1) / 2;
141
+
142
+            reverse->comp[p].u16[0] = color->comp[p].u16[0] > mid ? 0 : max;
143
+            reverse->comp[p].u16[1] = color->comp[p].u16[1] > mid ? 0 : max;
144
+            reverse->comp[p].u16[2] = color->comp[p].u16[2] > mid ? 0 : max;
145
+            reverse->comp[p].u16[3] = color->comp[p].u16[3] > mid ? 0 : max;
146
+        }
147
+    }
148
+}
149
+
150
+typedef struct ThreadData {
151
+    AVFrame *in, *out;
152
+    int xoff, yoff;
153
+} ThreadData;
154
+
155
+static int filter_color2(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
156
+{
157
+    DatascopeContext *s = ctx->priv;
158
+    AVFilterLink *outlink = ctx->outputs[0];
159
+    AVFilterLink *inlink = ctx->inputs[0];
160
+    ThreadData *td = arg;
161
+    AVFrame *in = td->in;
162
+    AVFrame *out = td->out;
163
+    const int xoff = td->xoff;
164
+    const int yoff = td->yoff;
165
+    const int P = FFMAX(s->nb_planes, s->nb_comps);
166
+    const int C = s->chars;
167
+    const int W = (outlink->w - xoff) / (C * 10);
168
+    const int H = (outlink->h - yoff) / (P * 12);
169
+    const char *format[2] = {"%02X\n", "%04X\n"};
170
+    const int slice_start = (W * jobnr) / nb_jobs;
171
+    const int slice_end = (W * (jobnr+1)) / nb_jobs;
172
+    int x, y, p;
173
+
174
+    for (y = 0; y < H && (y + s->y < inlink->h); y++) {
175
+        for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
176
+            FFDrawColor color = { { 0 } };
177
+            FFDrawColor reverse = { { 0 } };
178
+            int value[4] = { 0 };
179
+
180
+            pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
181
+            reverse_color(&s->draw, &color, &reverse);
182
+            ff_fill_rectangle(&s->draw, &color, out->data, out->linesize,
183
+                              xoff + x * C * 10, yoff + y * P * 12, C * 10, P * 12);
184
+
185
+            for (p = 0; p < P; p++) {
186
+                char text[256];
187
+
188
+                snprintf(text, sizeof(text), format[C>>2], value[p]);
189
+                draw_text(s, out, &reverse, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
190
+            }
191
+        }
192
+    }
193
+
194
+    return 0;
195
+}
196
+
197
+static int filter_color(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
198
+{
199
+    DatascopeContext *s = ctx->priv;
200
+    AVFilterLink *outlink = ctx->outputs[0];
201
+    AVFilterLink *inlink = ctx->inputs[0];
202
+    ThreadData *td = arg;
203
+    AVFrame *in = td->in;
204
+    AVFrame *out = td->out;
205
+    const int xoff = td->xoff;
206
+    const int yoff = td->yoff;
207
+    const int P = FFMAX(s->nb_planes, s->nb_comps);
208
+    const int C = s->chars;
209
+    const int W = (outlink->w - xoff) / (C * 10);
210
+    const int H = (outlink->h - yoff) / (P * 12);
211
+    const char *format[2] = {"%02X\n", "%04X\n"};
212
+    const int slice_start = (W * jobnr) / nb_jobs;
213
+    const int slice_end = (W * (jobnr+1)) / nb_jobs;
214
+    int x, y, p;
215
+
216
+    for (y = 0; y < H && (y + s->y < inlink->h); y++) {
217
+        for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
218
+            FFDrawColor color = { { 0 } };
219
+            int value[4] = { 0 };
220
+
221
+            pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
222
+
223
+            for (p = 0; p < P; p++) {
224
+                char text[256];
225
+
226
+                snprintf(text, sizeof(text), format[C>>2], value[p]);
227
+                draw_text(s, out, &color, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
228
+            }
229
+        }
230
+    }
231
+
232
+    return 0;
233
+}
234
+
235
+static int filter_mono(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
236
+{
237
+    DatascopeContext *s = ctx->priv;
238
+    AVFilterLink *outlink = ctx->outputs[0];
239
+    AVFilterLink *inlink = ctx->inputs[0];
240
+    ThreadData *td = arg;
241
+    AVFrame *in = td->in;
242
+    AVFrame *out = td->out;
243
+    const int xoff = td->xoff;
244
+    const int yoff = td->yoff;
245
+    const int P = FFMAX(s->nb_planes, s->nb_comps);
246
+    const int C = s->chars;
247
+    const int W = (outlink->w - xoff) / (C * 10);
248
+    const int H = (outlink->h - yoff) / (P * 12);
249
+    const char *format[2] = {"%02X\n", "%04X\n"};
250
+    const int slice_start = (W * jobnr) / nb_jobs;
251
+    const int slice_end = (W * (jobnr+1)) / nb_jobs;
252
+    int x, y, p;
253
+
254
+    for (y = 0; y < H && (y + s->y < inlink->h); y++) {
255
+        for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
256
+            FFDrawColor color = { { 0 } };
257
+            int value[4] = { 0 };
258
+
259
+            pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
260
+            for (p = 0; p < P; p++) {
261
+                char text[256];
262
+
263
+                snprintf(text, sizeof(text), format[C>>2], value[p]);
264
+                draw_text(s, out, &s->white, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
265
+            }
266
+        }
267
+    }
268
+
269
+    return 0;
270
+}
271
+
272
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
273
+{
274
+    AVFilterContext *ctx  = inlink->dst;
275
+    DatascopeContext *s = ctx->priv;
276
+    AVFilterLink *outlink = ctx->outputs[0];
277
+    ThreadData td = { 0 };
278
+    int ymaxlen = 0;
279
+    int xmaxlen = 0;
280
+    AVFrame *out;
281
+
282
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
283
+    if (!out) {
284
+        av_frame_free(&in);
285
+        return AVERROR(ENOMEM);
286
+    }
287
+    out->pts = in->pts;
288
+
289
+    ff_fill_rectangle(&s->draw, &s->black, out->data, out->linesize,
290
+                      0, 0, outlink->w, outlink->h);
291
+
292
+    if (s->axis) {
293
+        const int P = FFMAX(s->nb_planes, s->nb_comps);
294
+        const int C = s->chars;
295
+        int Y = outlink->h / (P * 12);
296
+        int X = outlink->w / (C * 10);
297
+        char text[256] = { 0 };
298
+        int x, y;
299
+
300
+        snprintf(text, sizeof(text), "%d", s->y + Y);
301
+        ymaxlen = strlen(text);
302
+        ymaxlen *= 10;
303
+        snprintf(text, sizeof(text), "%d", s->x + X);
304
+        xmaxlen = strlen(text);
305
+        xmaxlen *= 10;
306
+
307
+        Y = (outlink->h - xmaxlen) / (P * 12);
308
+        X = (outlink->w - ymaxlen) / (C * 10);
309
+
310
+        for (y = 0; y < Y; y++) {
311
+            snprintf(text, sizeof(text), "%d", s->y + y);
312
+
313
+            ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
314
+                              0, xmaxlen + y * P * 12 + (P + 1) * P - 2, ymaxlen, 10);
315
+
316
+            draw_text(s, out, &s->yellow, 2, xmaxlen + y * P * 12 + (P + 1) * P, text, 0);
317
+        }
318
+
319
+        for (x = 0; x < X; x++) {
320
+            snprintf(text, sizeof(text), "%d", s->x + x);
321
+
322
+            ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
323
+                              ymaxlen + x * C * 10 + 2 * C - 2, 0, 10, xmaxlen);
324
+
325
+            draw_text(s, out, &s->yellow, ymaxlen + x * C * 10 + 2 * C, 2, text, 1);
326
+        }
327
+    }
328
+
329
+    td.in = in; td.out = out, td.yoff = xmaxlen, td.xoff = ymaxlen;
330
+    ctx->internal->execute(ctx, s->filter, &td, NULL, FFMIN(ctx->graph->nb_threads, FFMAX(outlink->w / 20, 1)));
331
+
332
+    av_frame_free(&in);
333
+    return ff_filter_frame(outlink, out);
334
+}
335
+
336
+static int config_input(AVFilterLink *inlink)
337
+{
338
+    DatascopeContext *s = inlink->dst->priv;
339
+
340
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
341
+    ff_draw_init(&s->draw, inlink->format, 0);
342
+    ff_draw_color(&s->draw, &s->white,  (uint8_t[]){ 255, 255, 255, 255} );
343
+    ff_draw_color(&s->draw, &s->black,  (uint8_t[]){ 0, 0, 0, 0} );
344
+    ff_draw_color(&s->draw, &s->yellow, (uint8_t[]){ 255, 255, 0, 255} );
345
+    ff_draw_color(&s->draw, &s->gray,   (uint8_t[]){ 77, 77, 77, 255} );
346
+    s->chars = (s->draw.desc->comp[0].depth + 7) / 8 * 2;
347
+    s->nb_comps = s->draw.desc->nb_components;
348
+
349
+    switch (s->mode) {
350
+    case 0: s->filter = filter_mono;   break;
351
+    case 1: s->filter = filter_color;  break;
352
+    case 2: s->filter = filter_color2; break;
353
+    }
354
+
355
+    return 0;
356
+}
357
+
358
+static int config_output(AVFilterLink *outlink)
359
+{
360
+    DatascopeContext *s = outlink->src->priv;
361
+
362
+    outlink->h = s->oh;
363
+    outlink->w = s->ow;
364
+    outlink->sample_aspect_ratio = (AVRational){1,1};
365
+
366
+    return 0;
367
+}
368
+
369
+static const AVFilterPad inputs[] = {
370
+    {
371
+        .name         = "default",
372
+        .type         = AVMEDIA_TYPE_VIDEO,
373
+        .filter_frame = filter_frame,
374
+        .config_props = config_input,
375
+    },
376
+    { NULL }
377
+};
378
+
379
+static const AVFilterPad outputs[] = {
380
+    {
381
+        .name         = "default",
382
+        .type         = AVMEDIA_TYPE_VIDEO,
383
+        .config_props = config_output,
384
+    },
385
+    { NULL }
386
+};
387
+
388
+AVFilter ff_vf_datascope = {
389
+    .name          = "datascope",
390
+    .description   = NULL_IF_CONFIG_SMALL("Video data analysis."),
391
+    .priv_size     = sizeof(DatascopeContext),
392
+    .priv_class    = &datascope_class,
393
+    .query_formats = query_formats,
394
+    .inputs        = inputs,
395
+    .outputs       = outputs,
396
+    .flags         = AVFILTER_FLAG_SLICE_THREADS,
397
+};