]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_datascope.c
Merge commit '05a4bacbf7ece618553d339afe1d0b57bc87aea8'
[ffmpeg] / libavfilter / vf_datascope.c
1 /*
2  * Copyright (c) 2016 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/avassert.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/opt.h"
24 #include "libavutil/parseutils.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/xga_font_data.h"
27 #include "avfilter.h"
28 #include "drawutils.h"
29 #include "formats.h"
30 #include "internal.h"
31 #include "video.h"
32
33 typedef struct DatascopeContext {
34     const AVClass *class;
35     int ow, oh;
36     int x, y;
37     int mode;
38     int axis;
39
40     int nb_planes;
41     int nb_comps;
42     int chars;
43     FFDrawContext draw;
44     FFDrawColor yellow;
45     FFDrawColor white;
46     FFDrawColor black;
47     FFDrawColor gray;
48
49     int (*filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
50 } DatascopeContext;
51
52 #define OFFSET(x) offsetof(DatascopeContext, x)
53 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
54
55 static const AVOption datascope_options[] = {
56     { "size", "set output size", OFFSET(ow),   AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
57     { "s",    "set output size", OFFSET(ow),   AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
58     { "x",    "set x offset", OFFSET(x),    AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
59     { "y",    "set y offset", OFFSET(y),    AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
60     { "mode", "set scope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "mode" },
61     {   "mono",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
62     {   "color",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
63     {   "color2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode" },
64     { "axis",    "draw column/row numbers", OFFSET(axis), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
65     { NULL }
66 };
67
68 AVFILTER_DEFINE_CLASS(datascope);
69
70 static int query_formats(AVFilterContext *ctx)
71 {
72     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
73 }
74
75 static void draw_text(DatascopeContext *s, AVFrame *frame, FFDrawColor *color,
76                       int x0, int y0, const uint8_t *text, int vertical)
77 {
78     int x = x0;
79
80     for (; *text; text++) {
81         if (*text == '\n') {
82             x = x0;
83             y0 += 8;
84             continue;
85         }
86         ff_blend_mask(&s->draw, color, frame->data, frame->linesize,
87                       frame->width, frame->height,
88                       avpriv_cga_font + *text * 8, 1, 8, 8, 0, 0, x, y0);
89         if (vertical) {
90             x = x0;
91             y0 += 8;
92         } else {
93             x += 8;
94         }
95     }
96 }
97
98 static void pick_color(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value)
99 {
100     int p, i;
101
102     color->rgba[3] = 255;
103     for (p = 0; p < draw->nb_planes; p++) {
104         if (draw->desc->comp[p].depth == 8) {
105             if (draw->nb_planes == 1) {
106                 for (i = 0; i < 4; i++) {
107                     value[i] = in->data[0][y * in->linesize[0] + x * draw->pixelstep[0] + i];
108                     color->comp[0].u8[i] = value[i];
109                 }
110             } else {
111                 value[p] = in->data[p][(y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p])];
112                 color->comp[p].u8[0] = value[p];
113             }
114         } else {
115             if (draw->nb_planes == 1) {
116                 for (i = 0; i < 4; i++) {
117                     value[i] = AV_RL16(in->data[0] + y * in->linesize[0] + x * draw->pixelstep[0] + i * 2);
118                     color->comp[0].u16[i] = value[i];
119                 }
120             } else {
121                 value[p] = AV_RL16(in->data[p] + (y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p]) * 2);
122                 color->comp[p].u16[0] = value[p];
123             }
124         }
125     }
126 }
127
128 static void reverse_color(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse)
129 {
130     int p;
131
132     reverse->rgba[3] = 255;
133     for (p = 0; p < draw->nb_planes; p++) {
134         if (draw->desc->comp[p].depth == 8) {
135             reverse->comp[p].u8[0] = color->comp[p].u8[0] > 127 ? 0 : 255;
136             reverse->comp[p].u8[1] = color->comp[p].u8[1] > 127 ? 0 : 255;
137             reverse->comp[p].u8[2] = color->comp[p].u8[2] > 127 ? 0 : 255;
138             reverse->comp[p].u8[3] = color->comp[p].u8[3] > 127 ? 0 : 255;
139         } else {
140             const unsigned max = (1 << draw->desc->comp[p].depth) - 1;
141             const unsigned mid = (max + 1) / 2;
142
143             reverse->comp[p].u16[0] = color->comp[p].u16[0] > mid ? 0 : max;
144             reverse->comp[p].u16[1] = color->comp[p].u16[1] > mid ? 0 : max;
145             reverse->comp[p].u16[2] = color->comp[p].u16[2] > mid ? 0 : max;
146             reverse->comp[p].u16[3] = color->comp[p].u16[3] > mid ? 0 : max;
147         }
148     }
149 }
150
151 typedef struct ThreadData {
152     AVFrame *in, *out;
153     int xoff, yoff;
154 } ThreadData;
155
156 static int filter_color2(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
157 {
158     DatascopeContext *s = ctx->priv;
159     AVFilterLink *outlink = ctx->outputs[0];
160     AVFilterLink *inlink = ctx->inputs[0];
161     ThreadData *td = arg;
162     AVFrame *in = td->in;
163     AVFrame *out = td->out;
164     const int xoff = td->xoff;
165     const int yoff = td->yoff;
166     const int P = FFMAX(s->nb_planes, s->nb_comps);
167     const int C = s->chars;
168     const int W = (outlink->w - xoff) / (C * 10);
169     const int H = (outlink->h - yoff) / (P * 12);
170     const char *format[2] = {"%02X\n", "%04X\n"};
171     const int slice_start = (W * jobnr) / nb_jobs;
172     const int slice_end = (W * (jobnr+1)) / nb_jobs;
173     int x, y, p;
174
175     for (y = 0; y < H && (y + s->y < inlink->h); y++) {
176         for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
177             FFDrawColor color = { { 0 } };
178             FFDrawColor reverse = { { 0 } };
179             int value[4] = { 0 };
180
181             pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
182             reverse_color(&s->draw, &color, &reverse);
183             ff_fill_rectangle(&s->draw, &color, out->data, out->linesize,
184                               xoff + x * C * 10, yoff + y * P * 12, C * 10, P * 12);
185
186             for (p = 0; p < P; p++) {
187                 char text[256];
188
189                 snprintf(text, sizeof(text), format[C>>2], value[p]);
190                 draw_text(s, out, &reverse, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
191             }
192         }
193     }
194
195     return 0;
196 }
197
198 static int filter_color(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
199 {
200     DatascopeContext *s = ctx->priv;
201     AVFilterLink *outlink = ctx->outputs[0];
202     AVFilterLink *inlink = ctx->inputs[0];
203     ThreadData *td = arg;
204     AVFrame *in = td->in;
205     AVFrame *out = td->out;
206     const int xoff = td->xoff;
207     const int yoff = td->yoff;
208     const int P = FFMAX(s->nb_planes, s->nb_comps);
209     const int C = s->chars;
210     const int W = (outlink->w - xoff) / (C * 10);
211     const int H = (outlink->h - yoff) / (P * 12);
212     const char *format[2] = {"%02X\n", "%04X\n"};
213     const int slice_start = (W * jobnr) / nb_jobs;
214     const int slice_end = (W * (jobnr+1)) / nb_jobs;
215     int x, y, p;
216
217     for (y = 0; y < H && (y + s->y < inlink->h); y++) {
218         for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
219             FFDrawColor color = { { 0 } };
220             int value[4] = { 0 };
221
222             pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
223
224             for (p = 0; p < P; p++) {
225                 char text[256];
226
227                 snprintf(text, sizeof(text), format[C>>2], value[p]);
228                 draw_text(s, out, &color, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
229             }
230         }
231     }
232
233     return 0;
234 }
235
236 static int filter_mono(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
237 {
238     DatascopeContext *s = ctx->priv;
239     AVFilterLink *outlink = ctx->outputs[0];
240     AVFilterLink *inlink = ctx->inputs[0];
241     ThreadData *td = arg;
242     AVFrame *in = td->in;
243     AVFrame *out = td->out;
244     const int xoff = td->xoff;
245     const int yoff = td->yoff;
246     const int P = FFMAX(s->nb_planes, s->nb_comps);
247     const int C = s->chars;
248     const int W = (outlink->w - xoff) / (C * 10);
249     const int H = (outlink->h - yoff) / (P * 12);
250     const char *format[2] = {"%02X\n", "%04X\n"};
251     const int slice_start = (W * jobnr) / nb_jobs;
252     const int slice_end = (W * (jobnr+1)) / nb_jobs;
253     int x, y, p;
254
255     for (y = 0; y < H && (y + s->y < inlink->h); y++) {
256         for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
257             FFDrawColor color = { { 0 } };
258             int value[4] = { 0 };
259
260             pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
261             for (p = 0; p < P; p++) {
262                 char text[256];
263
264                 snprintf(text, sizeof(text), format[C>>2], value[p]);
265                 draw_text(s, out, &s->white, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
266             }
267         }
268     }
269
270     return 0;
271 }
272
273 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
274 {
275     AVFilterContext *ctx  = inlink->dst;
276     DatascopeContext *s = ctx->priv;
277     AVFilterLink *outlink = ctx->outputs[0];
278     ThreadData td = { 0 };
279     int ymaxlen = 0;
280     int xmaxlen = 0;
281     AVFrame *out;
282
283     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
284     if (!out) {
285         av_frame_free(&in);
286         return AVERROR(ENOMEM);
287     }
288     out->pts = in->pts;
289
290     ff_fill_rectangle(&s->draw, &s->black, out->data, out->linesize,
291                       0, 0, outlink->w, outlink->h);
292
293     if (s->axis) {
294         const int P = FFMAX(s->nb_planes, s->nb_comps);
295         const int C = s->chars;
296         int Y = outlink->h / (P * 12);
297         int X = outlink->w / (C * 10);
298         char text[256] = { 0 };
299         int x, y;
300
301         snprintf(text, sizeof(text), "%d", s->y + Y);
302         ymaxlen = strlen(text);
303         ymaxlen *= 10;
304         snprintf(text, sizeof(text), "%d", s->x + X);
305         xmaxlen = strlen(text);
306         xmaxlen *= 10;
307
308         Y = (outlink->h - xmaxlen) / (P * 12);
309         X = (outlink->w - ymaxlen) / (C * 10);
310
311         for (y = 0; y < Y; y++) {
312             snprintf(text, sizeof(text), "%d", s->y + y);
313
314             ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
315                               0, xmaxlen + y * P * 12 + (P + 1) * P - 2, ymaxlen, 10);
316
317             draw_text(s, out, &s->yellow, 2, xmaxlen + y * P * 12 + (P + 1) * P, text, 0);
318         }
319
320         for (x = 0; x < X; x++) {
321             snprintf(text, sizeof(text), "%d", s->x + x);
322
323             ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
324                               ymaxlen + x * C * 10 + 2 * C - 2, 0, 10, xmaxlen);
325
326             draw_text(s, out, &s->yellow, ymaxlen + x * C * 10 + 2 * C, 2, text, 1);
327         }
328     }
329
330     td.in = in; td.out = out, td.yoff = xmaxlen, td.xoff = ymaxlen;
331     ctx->internal->execute(ctx, s->filter, &td, NULL, FFMIN(ctx->graph->nb_threads, FFMAX(outlink->w / 20, 1)));
332
333     av_frame_free(&in);
334     return ff_filter_frame(outlink, out);
335 }
336
337 static int config_input(AVFilterLink *inlink)
338 {
339     DatascopeContext *s = inlink->dst->priv;
340
341     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
342     ff_draw_init(&s->draw, inlink->format, 0);
343     ff_draw_color(&s->draw, &s->white,  (uint8_t[]){ 255, 255, 255, 255} );
344     ff_draw_color(&s->draw, &s->black,  (uint8_t[]){ 0, 0, 0, 0} );
345     ff_draw_color(&s->draw, &s->yellow, (uint8_t[]){ 255, 255, 0, 255} );
346     ff_draw_color(&s->draw, &s->gray,   (uint8_t[]){ 77, 77, 77, 255} );
347     s->chars = (s->draw.desc->comp[0].depth + 7) / 8 * 2;
348     s->nb_comps = s->draw.desc->nb_components;
349
350     switch (s->mode) {
351     case 0: s->filter = filter_mono;   break;
352     case 1: s->filter = filter_color;  break;
353     case 2: s->filter = filter_color2; break;
354     }
355
356     return 0;
357 }
358
359 static int config_output(AVFilterLink *outlink)
360 {
361     DatascopeContext *s = outlink->src->priv;
362
363     outlink->h = s->oh;
364     outlink->w = s->ow;
365     outlink->sample_aspect_ratio = (AVRational){1,1};
366
367     return 0;
368 }
369
370 static const AVFilterPad inputs[] = {
371     {
372         .name         = "default",
373         .type         = AVMEDIA_TYPE_VIDEO,
374         .filter_frame = filter_frame,
375         .config_props = config_input,
376     },
377     { NULL }
378 };
379
380 static const AVFilterPad outputs[] = {
381     {
382         .name         = "default",
383         .type         = AVMEDIA_TYPE_VIDEO,
384         .config_props = config_output,
385     },
386     { NULL }
387 };
388
389 AVFilter ff_vf_datascope = {
390     .name          = "datascope",
391     .description   = NULL_IF_CONFIG_SMALL("Video data analysis."),
392     .priv_size     = sizeof(DatascopeContext),
393     .priv_class    = &datascope_class,
394     .query_formats = query_formats,
395     .inputs        = inputs,
396     .outputs       = outputs,
397     .flags         = AVFILTER_FLAG_SLICE_THREADS,
398 };