GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/vf_datascope.c Lines: 0 520 0.0 %
Date: 2021-01-27 11:56:10 Branches: 0 272 0.0 %

Line Branch Exec Source
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 dformat;
39
    int axis;
40
    int components;
41
    float opacity;
42
43
    int nb_planes;
44
    int nb_comps;
45
    int chars;
46
    FFDrawContext draw;
47
    FFDrawColor yellow;
48
    FFDrawColor white;
49
    FFDrawColor black;
50
    FFDrawColor gray;
51
52
    void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
53
    void (*reverse_color)(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse);
54
    int (*filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
55
} DatascopeContext;
56
57
#define OFFSET(x) offsetof(DatascopeContext, x)
58
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
59
#define FLAGSR AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
60
61
static const AVOption datascope_options[] = {
62
    { "size", "set output size", OFFSET(ow),   AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
63
    { "s",    "set output size", OFFSET(ow),   AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
64
    { "x",    "set x offset", OFFSET(x),    AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
65
    { "y",    "set y offset", OFFSET(y),    AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
66
    { "mode", "set scope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "mode" },
67
    {   "mono",   NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
68
    {   "color",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
69
    {   "color2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode" },
70
    { "axis",    "draw column/row numbers", OFFSET(axis), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
71
    { "opacity", "set background opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
72
    { "format", "set display number format", OFFSET(dformat), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "format" },
73
    {   "hex",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "format" },
74
    {   "dec",  NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "format" },
75
    { "components", "set components to display", OFFSET(components), AV_OPT_TYPE_INT, {.i64=15}, 1, 15, FLAGS },
76
    { NULL }
77
};
78
79
AVFILTER_DEFINE_CLASS(datascope);
80
81
static int query_formats(AVFilterContext *ctx)
82
{
83
    return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
84
}
85
86
static void draw_text(FFDrawContext *draw, AVFrame *frame, FFDrawColor *color,
87
                      int x0, int y0, const uint8_t *text, int vertical)
88
{
89
    int x = x0;
90
91
    for (; *text; text++) {
92
        if (*text == '\n') {
93
            x = x0;
94
            y0 += 8;
95
            continue;
96
        }
97
        ff_blend_mask(draw, color, frame->data, frame->linesize,
98
                      frame->width, frame->height,
99
                      avpriv_cga_font + *text * 8, 1, 8, 8, 0, 0, x, y0);
100
        if (vertical) {
101
            x = x0;
102
            y0 += 8;
103
        } else {
104
            x += 8;
105
        }
106
    }
107
}
108
109
static void pick_color8(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value)
110
{
111
    int p, i;
112
113
    color->rgba[3] = 255;
114
    for (p = 0; p < draw->nb_planes; p++) {
115
        if (draw->nb_planes == 1) {
116
            for (i = 0; i < 4; i++) {
117
                value[i] = in->data[0][y * in->linesize[0] + x * draw->pixelstep[0] + i];
118
                color->comp[0].u8[i] = value[i];
119
            }
120
        } else {
121
            value[p] = in->data[p][(y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p])];
122
            color->comp[p].u8[0] = value[p];
123
        }
124
    }
125
}
126
127
static void pick_color16(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value)
128
{
129
    int p, i;
130
131
    color->rgba[3] = 255;
132
    for (p = 0; p < draw->nb_planes; p++) {
133
        if (draw->nb_planes == 1) {
134
            for (i = 0; i < 4; i++) {
135
                value[i] = AV_RL16(in->data[0] + y * in->linesize[0] + x * draw->pixelstep[0] + i * 2);
136
                color->comp[0].u16[i] = value[i];
137
            }
138
        } else {
139
            value[p] = AV_RL16(in->data[p] + (y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p]) * 2);
140
            color->comp[p].u16[0] = value[p];
141
        }
142
    }
143
}
144
145
static void reverse_color8(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse)
146
{
147
    int p;
148
149
    reverse->rgba[3] = 255;
150
    for (p = 0; p < draw->nb_planes; p++) {
151
        reverse->comp[p].u8[0] = color->comp[p].u8[0] > 127 ? 0 : 255;
152
        reverse->comp[p].u8[1] = color->comp[p].u8[1] > 127 ? 0 : 255;
153
        reverse->comp[p].u8[2] = color->comp[p].u8[2] > 127 ? 0 : 255;
154
    }
155
}
156
157
static void reverse_color16(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse)
158
{
159
    int p;
160
161
    reverse->rgba[3] = 255;
162
    for (p = 0; p < draw->nb_planes; p++) {
163
        const unsigned max = (1 << draw->desc->comp[p].depth) - 1;
164
        const unsigned mid = (max + 1) / 2;
165
166
        reverse->comp[p].u16[0] = color->comp[p].u16[0] > mid ? 0 : max;
167
        reverse->comp[p].u16[1] = color->comp[p].u16[1] > mid ? 0 : max;
168
        reverse->comp[p].u16[2] = color->comp[p].u16[2] > mid ? 0 : max;
169
    }
170
}
171
172
typedef struct ThreadData {
173
    AVFrame *in, *out;
174
    int xoff, yoff, PP;
175
} ThreadData;
176
177
static int filter_color2(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
178
{
179
    DatascopeContext *s = ctx->priv;
180
    AVFilterLink *outlink = ctx->outputs[0];
181
    AVFilterLink *inlink = ctx->inputs[0];
182
    ThreadData *td = arg;
183
    AVFrame *in = td->in;
184
    AVFrame *out = td->out;
185
    const int PP = td->PP;
186
    const int xoff = td->xoff;
187
    const int yoff = td->yoff;
188
    const int P = FFMAX(s->nb_planes, s->nb_comps);
189
    const int C = s->chars;
190
    const int D = ((s->chars - s->dformat) >> 2) + s->dformat * 2;
191
    const int W = (outlink->w - xoff) / (C * 10);
192
    const int H = (outlink->h - yoff) / (PP * 12);
193
    const char *format[4] = {"%02X\n", "%04X\n", "%03d\n", "%05d\n"};
194
    const int slice_start = (W * jobnr) / nb_jobs;
195
    const int slice_end = (W * (jobnr+1)) / nb_jobs;
196
    int x, y, p;
197
198
    for (y = 0; y < H && (y + s->y < inlink->h); y++) {
199
        for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
200
            FFDrawColor color = { { 0 } };
201
            FFDrawColor reverse = { { 0 } };
202
            int value[4] = { 0 }, pp = 0;
203
204
            s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
205
            s->reverse_color(&s->draw, &color, &reverse);
206
            ff_fill_rectangle(&s->draw, &color, out->data, out->linesize,
207
                              xoff + x * C * 10, yoff + y * PP * 12, C * 10, PP * 12);
208
209
            for (p = 0; p < P; p++) {
210
                char text[256];
211
212
                if (!(s->components & (1 << p)))
213
                    continue;
214
                snprintf(text, sizeof(text), format[D], value[p]);
215
                draw_text(&s->draw, out, &reverse, xoff + x * C * 10 + 2, yoff + y * PP * 12 + pp * 10 + 2, text, 0);
216
                pp++;
217
            }
218
        }
219
    }
220
221
    return 0;
222
}
223
224
static int filter_color(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
225
{
226
    DatascopeContext *s = ctx->priv;
227
    AVFilterLink *outlink = ctx->outputs[0];
228
    AVFilterLink *inlink = ctx->inputs[0];
229
    ThreadData *td = arg;
230
    AVFrame *in = td->in;
231
    AVFrame *out = td->out;
232
    const int PP = td->PP;
233
    const int xoff = td->xoff;
234
    const int yoff = td->yoff;
235
    const int P = FFMAX(s->nb_planes, s->nb_comps);
236
    const int C = s->chars;
237
    const int D = ((s->chars - s->dformat) >> 2) + s->dformat * 2;
238
    const int W = (outlink->w - xoff) / (C * 10);
239
    const int H = (outlink->h - yoff) / (PP * 12);
240
    const char *format[4] = {"%02X\n", "%04X\n", "%03d\n", "%05d\n"};
241
    const int slice_start = (W * jobnr) / nb_jobs;
242
    const int slice_end = (W * (jobnr+1)) / nb_jobs;
243
    int x, y, p;
244
245
    for (y = 0; y < H && (y + s->y < inlink->h); y++) {
246
        for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
247
            FFDrawColor color = { { 0 } };
248
            int value[4] = { 0 }, pp = 0;
249
250
            s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
251
252
            for (p = 0; p < P; p++) {
253
                char text[256];
254
255
                if (!(s->components & (1 << p)))
256
                    continue;
257
                snprintf(text, sizeof(text), format[D], value[p]);
258
                draw_text(&s->draw, out, &color, xoff + x * C * 10 + 2, yoff + y * PP * 12 + pp * 10 + 2, text, 0);
259
                pp++;
260
            }
261
        }
262
    }
263
264
    return 0;
265
}
266
267
static int filter_mono(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
268
{
269
    DatascopeContext *s = ctx->priv;
270
    AVFilterLink *outlink = ctx->outputs[0];
271
    AVFilterLink *inlink = ctx->inputs[0];
272
    ThreadData *td = arg;
273
    AVFrame *in = td->in;
274
    AVFrame *out = td->out;
275
    const int PP = td->PP;
276
    const int xoff = td->xoff;
277
    const int yoff = td->yoff;
278
    const int P = FFMAX(s->nb_planes, s->nb_comps);
279
    const int C = s->chars;
280
    const int D = ((s->chars - s->dformat) >> 2) + s->dformat * 2;
281
    const int W = (outlink->w - xoff) / (C * 10);
282
    const int H = (outlink->h - yoff) / (PP * 12);
283
    const char *format[4] = {"%02X\n", "%04X\n", "%03d\n", "%05d\n"};
284
    const int slice_start = (W * jobnr) / nb_jobs;
285
    const int slice_end = (W * (jobnr+1)) / nb_jobs;
286
    int x, y, p;
287
288
    for (y = 0; y < H && (y + s->y < inlink->h); y++) {
289
        for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
290
            FFDrawColor color = { { 0 } };
291
            int value[4] = { 0 }, pp = 0;
292
293
            s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
294
            for (p = 0; p < P; p++) {
295
                char text[256];
296
297
                if (!(s->components & (1 << p)))
298
                    continue;
299
                snprintf(text, sizeof(text), format[D], value[p]);
300
                draw_text(&s->draw, out, &s->white, xoff + x * C * 10 + 2, yoff + y * PP * 12 + pp * 10 + 2, text, 0);
301
                pp++;
302
            }
303
        }
304
    }
305
306
    return 0;
307
}
308
309
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
310
{
311
    AVFilterContext *ctx  = inlink->dst;
312
    DatascopeContext *s = ctx->priv;
313
    AVFilterLink *outlink = ctx->outputs[0];
314
    const int P = FFMAX(s->nb_planes, s->nb_comps);
315
    ThreadData td = { 0 };
316
    int ymaxlen = 0;
317
    int xmaxlen = 0;
318
    int PP = 0;
319
    AVFrame *out;
320
321
    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
322
    if (!out) {
323
        av_frame_free(&in);
324
        return AVERROR(ENOMEM);
325
    }
326
    out->pts = in->pts;
327
328
    ff_fill_rectangle(&s->draw, &s->black, out->data, out->linesize,
329
                      0, 0, outlink->w, outlink->h);
330
331
    for (int p = 0; p < P; p++) {
332
        if (s->components & (1 << p))
333
            PP++;
334
    }
335
    PP = FFMAX(PP, 1);
336
337
    if (s->axis) {
338
        const int C = s->chars;
339
        int Y = outlink->h / (PP * 12);
340
        int X = outlink->w / (C * 10);
341
        char text[256] = { 0 };
342
        int x, y;
343
344
        snprintf(text, sizeof(text), "%d", s->y + Y);
345
        ymaxlen = strlen(text);
346
        ymaxlen *= 10;
347
        snprintf(text, sizeof(text), "%d", s->x + X);
348
        xmaxlen = strlen(text);
349
        xmaxlen *= 10;
350
351
        Y = (outlink->h - xmaxlen) / (PP * 12);
352
        X = (outlink->w - ymaxlen) / (C * 10);
353
354
        for (y = 0; y < Y; y++) {
355
            snprintf(text, sizeof(text), "%d", s->y + y);
356
357
            ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
358
                              0, xmaxlen + y * PP * 12 + (PP + 1) * PP - 2, ymaxlen, 10);
359
360
            draw_text(&s->draw, out, &s->yellow, 2, xmaxlen + y * PP * 12 + (PP + 1) * PP, text, 0);
361
        }
362
363
        for (x = 0; x < X; x++) {
364
            snprintf(text, sizeof(text), "%d", s->x + x);
365
366
            ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
367
                              ymaxlen + x * C * 10 + 2 * C - 2, 0, 10, xmaxlen);
368
369
            draw_text(&s->draw, out, &s->yellow, ymaxlen + x * C * 10 + 2 * C, 2, text, 1);
370
        }
371
    }
372
373
    td.in = in; td.out = out, td.yoff = xmaxlen, td.xoff = ymaxlen, td.PP = PP;
374
    ctx->internal->execute(ctx, s->filter, &td, NULL, FFMIN(ff_filter_get_nb_threads(ctx), FFMAX(outlink->w / 20, 1)));
375
376
    av_frame_free(&in);
377
    return ff_filter_frame(outlink, out);
378
}
379
380
static int config_input(AVFilterLink *inlink)
381
{
382
    DatascopeContext *s = inlink->dst->priv;
383
    uint8_t alpha = s->opacity * 255;
384
385
    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
386
    ff_draw_init(&s->draw, inlink->format, 0);
387
    ff_draw_color(&s->draw, &s->white,  (uint8_t[]){ 255, 255, 255, 255} );
388
    ff_draw_color(&s->draw, &s->black,  (uint8_t[]){ 0, 0, 0, alpha} );
389
    ff_draw_color(&s->draw, &s->yellow, (uint8_t[]){ 255, 255, 0, 255} );
390
    ff_draw_color(&s->draw, &s->gray,   (uint8_t[]){ 77, 77, 77, 255} );
391
    s->chars = (s->draw.desc->comp[0].depth + 7) / 8 * 2 + s->dformat;
392
    s->nb_comps = s->draw.desc->nb_components;
393
394
    switch (s->mode) {
395
    case 0: s->filter = filter_mono;   break;
396
    case 1: s->filter = filter_color;  break;
397
    case 2: s->filter = filter_color2; break;
398
    }
399
400
    if (s->draw.desc->comp[0].depth <= 8) {
401
        s->pick_color = pick_color8;
402
        s->reverse_color = reverse_color8;
403
    } else {
404
        s->pick_color = pick_color16;
405
        s->reverse_color = reverse_color16;
406
    }
407
408
    return 0;
409
}
410
411
static int config_output(AVFilterLink *outlink)
412
{
413
    DatascopeContext *s = outlink->src->priv;
414
415
    outlink->h = s->oh;
416
    outlink->w = s->ow;
417
    outlink->sample_aspect_ratio = (AVRational){1,1};
418
419
    return 0;
420
}
421
422
static const AVFilterPad inputs[] = {
423
    {
424
        .name         = "default",
425
        .type         = AVMEDIA_TYPE_VIDEO,
426
        .filter_frame = filter_frame,
427
        .config_props = config_input,
428
    },
429
    { NULL }
430
};
431
432
static const AVFilterPad outputs[] = {
433
    {
434
        .name         = "default",
435
        .type         = AVMEDIA_TYPE_VIDEO,
436
        .config_props = config_output,
437
    },
438
    { NULL }
439
};
440
441
AVFilter ff_vf_datascope = {
442
    .name          = "datascope",
443
    .description   = NULL_IF_CONFIG_SMALL("Video data analysis."),
444
    .priv_size     = sizeof(DatascopeContext),
445
    .priv_class    = &datascope_class,
446
    .query_formats = query_formats,
447
    .inputs        = inputs,
448
    .outputs       = outputs,
449
    .flags         = AVFILTER_FLAG_SLICE_THREADS,
450
};
451
452
typedef struct PixscopeContext {
453
    const AVClass *class;
454
455
    float xpos, ypos;
456
    float wx, wy;
457
    int w, h;
458
    float o;
459
460
    int x, y;
461
    int ww, wh;
462
463
    int nb_planes;
464
    int nb_comps;
465
    int is_rgb;
466
    uint8_t rgba_map[4];
467
    FFDrawContext draw;
468
    FFDrawColor   dark;
469
    FFDrawColor   black;
470
    FFDrawColor   white;
471
    FFDrawColor   green;
472
    FFDrawColor   blue;
473
    FFDrawColor   red;
474
    FFDrawColor  *colors[4];
475
476
    uint16_t values[4][80][80];
477
478
    void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
479
} PixscopeContext;
480
481
#define POFFSET(x) offsetof(PixscopeContext, x)
482
483
static const AVOption pixscope_options[] = {
484
    { "x",  "set scope x offset",  POFFSET(xpos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0,  1, FLAGS },
485
    { "y",  "set scope y offset",  POFFSET(ypos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0,  1, FLAGS },
486
    { "w",  "set scope width",     POFFSET(w),    AV_OPT_TYPE_INT,   {.i64=7},   1, 80, FLAGS },
487
    { "h",  "set scope height",    POFFSET(h),    AV_OPT_TYPE_INT,   {.i64=7},   1, 80, FLAGS },
488
    { "o",  "set window opacity",  POFFSET(o),    AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0,  1, FLAGS },
489
    { "wx", "set window x offset", POFFSET(wx),   AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1,  1, FLAGS },
490
    { "wy", "set window y offset", POFFSET(wy),   AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1,  1, FLAGS },
491
    { NULL }
492
};
493
494
AVFILTER_DEFINE_CLASS(pixscope);
495
496
static int pixscope_config_input(AVFilterLink *inlink)
497
{
498
    PixscopeContext *s = inlink->dst->priv;
499
500
    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
501
    ff_draw_init(&s->draw, inlink->format, 0);
502
    ff_draw_color(&s->draw, &s->dark,  (uint8_t[]){ 0, 0, 0, s->o * 255} );
503
    ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
504
    ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
505
    ff_draw_color(&s->draw, &s->green, (uint8_t[]){   0, 255,   0, 255} );
506
    ff_draw_color(&s->draw, &s->blue,  (uint8_t[]){   0,   0, 255, 255} );
507
    ff_draw_color(&s->draw, &s->red,   (uint8_t[]){ 255,   0,   0, 255} );
508
    s->nb_comps = s->draw.desc->nb_components;
509
    s->is_rgb   = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB;
510
511
    if (s->is_rgb) {
512
        s->colors[0] = &s->red;
513
        s->colors[1] = &s->green;
514
        s->colors[2] = &s->blue;
515
        s->colors[3] = &s->white;
516
        ff_fill_rgba_map(s->rgba_map, inlink->format);
517
    } else {
518
        s->colors[0] = &s->white;
519
        s->colors[1] = &s->blue;
520
        s->colors[2] = &s->red;
521
        s->colors[3] = &s->white;
522
        s->rgba_map[0] = 0;
523
        s->rgba_map[1] = 1;
524
        s->rgba_map[2] = 2;
525
        s->rgba_map[3] = 3;
526
    }
527
528
    if (s->draw.desc->comp[0].depth <= 8) {
529
        s->pick_color = pick_color8;
530
    } else {
531
        s->pick_color = pick_color16;
532
    }
533
534
    if (inlink->w < 640 || inlink->h < 480) {
535
        av_log(inlink->dst, AV_LOG_ERROR, "min supported resolution is 640x480\n");
536
        return AVERROR(EINVAL);
537
    }
538
539
    s->ww = 300;
540
    s->wh = 300 * 1.6;
541
    s->x = s->xpos * (inlink->w - 1);
542
    s->y = s->ypos * (inlink->h - 1);
543
    if (s->x + s->w >= inlink->w || s->y + s->h >= inlink->h) {
544
        av_log(inlink->dst, AV_LOG_WARNING, "scope position is out of range, clipping\n");
545
        s->x = FFMIN(s->x, inlink->w - s->w);
546
        s->y = FFMIN(s->y, inlink->h - s->h);
547
    }
548
549
    return 0;
550
}
551
552
#define SQR(x) ((x)*(x))
553
554
static int pixscope_filter_frame(AVFilterLink *inlink, AVFrame *in)
555
{
556
    AVFilterContext *ctx  = inlink->dst;
557
    PixscopeContext *s = ctx->priv;
558
    AVFilterLink *outlink = ctx->outputs[0];
559
    AVFrame *out = ff_get_video_buffer(outlink, in->width, in->height);
560
    int max[4] = { 0 }, min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
561
    float average[4] = { 0 };
562
    double std[4] = { 0 }, rms[4] = { 0 };
563
    const char rgba[4] = { 'R', 'G', 'B', 'A' };
564
    const char yuva[4] = { 'Y', 'U', 'V', 'A' };
565
    int x, y, X, Y, i, w, h;
566
    char text[128];
567
568
    if (!out) {
569
        av_frame_free(&in);
570
        return AVERROR(ENOMEM);
571
    }
572
    av_frame_copy_props(out, in);
573
    av_frame_copy(out, in);
574
575
    w = s->ww / s->w;
576
    h = s->ww / s->h;
577
578
    if (s->wx >= 0) {
579
        X = (in->width - s->ww) * s->wx;
580
    } else {
581
        X = (in->width - s->ww) * -s->wx;
582
    }
583
    if (s->wy >= 0) {
584
        Y = (in->height - s->wh) * s->wy;
585
    } else {
586
        Y = (in->height - s->wh) * -s->wy;
587
    }
588
589
    if (s->wx < 0) {
590
        if (s->x + s->w >= X && (s->x + s->w <= X + s->ww) &&
591
            s->y + s->h >= Y && (s->y + s->h <= Y + s->wh)) {
592
            X = (in->width - s->ww) * (1 + s->wx);
593
        }
594
    }
595
596
    if (s->wy < 0) {
597
        if (s->x + s->w >= X && (s->x + s->w <= X + s->ww) &&
598
            s->y + s->h >= Y && (s->y + s->h <= Y + s->wh)) {
599
            Y = (in->height - s->wh) * (1 + s->wy);
600
        }
601
    }
602
603
    ff_blend_rectangle(&s->draw, &s->dark, out->data, out->linesize,
604
                       out->width, out->height,
605
                       X,
606
                       Y,
607
                       s->ww,
608
                       s->wh);
609
610
    for (y = 0; y < s->h; y++) {
611
        for (x = 0; x < s->w; x++) {
612
            FFDrawColor color = { { 0 } };
613
            int value[4] = { 0 };
614
615
            s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
616
            ff_fill_rectangle(&s->draw, &color, out->data, out->linesize,
617
                              x * w + (s->ww - 4 - (s->w * w)) / 2 + X, y * h + 2 + Y, w, h);
618
            for (i = 0; i < 4; i++) {
619
                s->values[i][x][y] = value[i];
620
                rms[i]     += (double)value[i] * (double)value[i];
621
                average[i] += value[i];
622
                min[i]      = FFMIN(min[i], value[i]);
623
                max[i]      = FFMAX(max[i], value[i]);
624
            }
625
        }
626
    }
627
628
    ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
629
                       out->width, out->height,
630
                       s->x - 2, s->y - 2, s->w + 4, 1);
631
632
    ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
633
                       out->width, out->height,
634
                       s->x - 1, s->y - 1, s->w + 2, 1);
635
636
    ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
637
                       out->width, out->height,
638
                       s->x - 1, s->y - 1, 1, s->h + 2);
639
640
    ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
641
                       out->width, out->height,
642
                       s->x - 2, s->y - 2, 1, s->h + 4);
643
644
    ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
645
                       out->width, out->height,
646
                       s->x - 1, s->y + 1 + s->h, s->w + 3, 1);
647
648
    ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
649
                       out->width, out->height,
650
                       s->x - 2, s->y + 2 + s->h, s->w + 4, 1);
651
652
    ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
653
                       out->width, out->height,
654
                       s->x + 1 + s->w, s->y - 1, 1, s->h + 2);
655
656
    ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
657
                       out->width, out->height,
658
                       s->x + 2 + s->w, s->y - 2, 1, s->h + 5);
659
660
    for (i = 0; i < 4; i++) {
661
        rms[i] /= s->w * s->h;
662
        rms[i]  = sqrt(rms[i]);
663
        average[i] /= s->w * s->h;
664
    }
665
666
    for (y = 0; y < s->h; y++) {
667
        for (x = 0; x < s->w; x++) {
668
            for (i = 0; i < 4; i++)
669
                std[i] += SQR(s->values[i][x][y] - average[i]);
670
        }
671
    }
672
673
    for (i = 0; i < 4; i++) {
674
        std[i] /= s->w * s->h;
675
        std[i]  = sqrt(std[i]);
676
    }
677
678
    snprintf(text, sizeof(text), "CH   AVG    MIN    MAX    RMS\n");
679
    draw_text(&s->draw, out, &s->white,        X + 28, Y + s->ww +  5,           text, 0);
680
    for (i = 0; i < s->nb_comps; i++) {
681
        int c = s->rgba_map[i];
682
683
        snprintf(text, sizeof(text), "%c  %07.1f %05d %05d %07.1f\n", s->is_rgb ? rgba[i] : yuva[i], average[c], min[c], max[c], rms[c]);
684
        draw_text(&s->draw, out, s->colors[i], X + 28, Y + s->ww + 15 * (i + 1), text, 0);
685
    }
686
    snprintf(text, sizeof(text), "CH   STD\n");
687
    draw_text(&s->draw, out, &s->white,        X + 28, Y + s->ww + 15 * (0 + 5), text, 0);
688
    for (i = 0; i < s->nb_comps; i++) {
689
        int c = s->rgba_map[i];
690
691
        snprintf(text, sizeof(text), "%c  %07.2f\n", s->is_rgb ? rgba[i] : yuva[i], std[c]);
692
        draw_text(&s->draw, out, s->colors[i], X + 28, Y + s->ww + 15 * (i + 6), text, 0);
693
    }
694
695
    av_frame_free(&in);
696
    return ff_filter_frame(outlink, out);
697
}
698
699
static const AVFilterPad pixscope_inputs[] = {
700
    {
701
        .name           = "default",
702
        .type           = AVMEDIA_TYPE_VIDEO,
703
        .filter_frame   = pixscope_filter_frame,
704
        .config_props   = pixscope_config_input,
705
    },
706
    { NULL }
707
};
708
709
static const AVFilterPad pixscope_outputs[] = {
710
    {
711
        .name         = "default",
712
        .type         = AVMEDIA_TYPE_VIDEO,
713
    },
714
    { NULL }
715
};
716
717
AVFilter ff_vf_pixscope = {
718
    .name          = "pixscope",
719
    .description   = NULL_IF_CONFIG_SMALL("Pixel data analysis."),
720
    .priv_size     = sizeof(PixscopeContext),
721
    .priv_class    = &pixscope_class,
722
    .query_formats = query_formats,
723
    .inputs        = pixscope_inputs,
724
    .outputs       = pixscope_outputs,
725
    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
726
};
727
728
typedef struct PixelValues {
729
    uint16_t p[4];
730
} PixelValues;
731
732
typedef struct OscilloscopeContext {
733
    const AVClass *class;
734
735
    float xpos, ypos;
736
    float tx, ty;
737
    float size;
738
    float tilt;
739
    float theight, twidth;
740
    float o;
741
    int components;
742
    int grid;
743
    int statistics;
744
    int scope;
745
746
    int x1, y1, x2, y2;
747
    int ox, oy;
748
    int height, width;
749
750
    int max;
751
    int nb_planes;
752
    int nb_comps;
753
    int is_rgb;
754
    uint8_t rgba_map[4];
755
    FFDrawContext draw;
756
    FFDrawColor   dark;
757
    FFDrawColor   black;
758
    FFDrawColor   white;
759
    FFDrawColor   green;
760
    FFDrawColor   blue;
761
    FFDrawColor   red;
762
    FFDrawColor   cyan;
763
    FFDrawColor   magenta;
764
    FFDrawColor   gray;
765
    FFDrawColor  *colors[4];
766
767
    int nb_values;
768
    PixelValues  *values;
769
770
    void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
771
    void (*draw_trace)(struct OscilloscopeContext *s, AVFrame *frame);
772
} OscilloscopeContext;
773
774
#define OOFFSET(x) offsetof(OscilloscopeContext, x)
775
776
static const AVOption oscilloscope_options[] = {
777
    { "x",  "set scope x position",    OOFFSET(xpos),       AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1,  FLAGSR },
778
    { "y",  "set scope y position",    OOFFSET(ypos),       AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1,  FLAGSR },
779
    { "s",  "set scope size",          OOFFSET(size),       AV_OPT_TYPE_FLOAT, {.dbl=0.8}, 0, 1,  FLAGSR },
780
    { "t",  "set scope tilt",          OOFFSET(tilt),       AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1,  FLAGSR },
781
    { "o",  "set trace opacity",       OOFFSET(o),          AV_OPT_TYPE_FLOAT, {.dbl=0.8}, 0, 1,  FLAGSR },
782
    { "tx", "set trace x position",    OOFFSET(tx),         AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1,  FLAGSR },
783
    { "ty", "set trace y position",    OOFFSET(ty),         AV_OPT_TYPE_FLOAT, {.dbl=0.9}, 0, 1,  FLAGSR },
784
    { "tw", "set trace width",         OOFFSET(twidth),     AV_OPT_TYPE_FLOAT, {.dbl=0.8},.1, 1,  FLAGSR },
785
    { "th", "set trace height",        OOFFSET(theight),    AV_OPT_TYPE_FLOAT, {.dbl=0.3},.1, 1,  FLAGSR },
786
    { "c",  "set components to trace", OOFFSET(components), AV_OPT_TYPE_INT,   {.i64=7},   0, 15, FLAGSR },
787
    { "g",  "draw trace grid",         OOFFSET(grid),       AV_OPT_TYPE_BOOL,  {.i64=1},   0, 1,  FLAGSR },
788
    { "st", "draw statistics",         OOFFSET(statistics), AV_OPT_TYPE_BOOL,  {.i64=1},   0, 1,  FLAGSR },
789
    { "sc", "draw scope",              OOFFSET(scope),      AV_OPT_TYPE_BOOL,  {.i64=1},   0, 1,  FLAGSR },
790
    { NULL }
791
};
792
793
AVFILTER_DEFINE_CLASS(oscilloscope);
794
795
static void oscilloscope_uninit(AVFilterContext *ctx)
796
{
797
    OscilloscopeContext *s = ctx->priv;
798
799
    av_freep(&s->values);
800
}
801
802
static void draw_line(FFDrawContext *draw, int x0, int y0, int x1, int y1,
803
                      AVFrame *out, FFDrawColor *color)
804
{
805
    int dx = FFABS(x1 - x0), sx = x0 < x1 ? 1 : -1;
806
    int dy = FFABS(y1 - y0), sy = y0 < y1 ? 1 : -1;
807
    int err = (dx > dy ? dx : -dy) / 2, e2;
808
    int p, i;
809
810
    for (;;) {
811
        if (x0 >= 0 && y0 >= 0 && x0 < out->width && y0 < out->height) {
812
            for (p = 0; p < draw->nb_planes; p++) {
813
                if (draw->desc->comp[p].depth == 8) {
814
                    if (draw->nb_planes == 1) {
815
                        for (i = 0; i < draw->desc->nb_components; i++) {
816
                            out->data[0][y0 * out->linesize[0] + x0 * draw->pixelstep[0] + i] = color->comp[0].u8[i];
817
                        }
818
                    } else {
819
                        out->data[p][out->linesize[p] * (y0 >> draw->vsub[p]) + (x0 >> draw->hsub[p])] = color->comp[p].u8[0];
820
                    }
821
                } else {
822
                    if (draw->nb_planes == 1) {
823
                        for (i = 0; i < draw->desc->nb_components; i++) {
824
                            AV_WN16(out->data[0] + y0 * out->linesize[0] + (x0 * draw->pixelstep[0] + i), color->comp[0].u16[i]);
825
                        }
826
                    } else {
827
                        AV_WN16(out->data[p] + out->linesize[p] * (y0 >> draw->vsub[p]) + (x0 >> draw->hsub[p]) * 2, color->comp[p].u16[0]);
828
                    }
829
                }
830
            }
831
        }
832
833
        if (x0 == x1 && y0 == y1)
834
            break;
835
836
        e2 = err;
837
838
        if (e2 >-dx) {
839
            err -= dy;
840
            x0 += sx;
841
        }
842
843
        if (e2 < dy) {
844
            err += dx;
845
            y0 += sy;
846
        }
847
    }
848
}
849
850
static void draw_trace8(OscilloscopeContext *s, AVFrame *frame)
851
{
852
    int i, c;
853
854
    for (i = 1; i < s->nb_values; i++) {
855
        for (c = 0; c < s->nb_comps; c++) {
856
            if ((1 << c) & s->components) {
857
                int x = i * s->width / s->nb_values;
858
                int px = (i - 1) * s->width / s->nb_values;
859
                int py = s->height - s->values[i-1].p[s->rgba_map[c]] * s->height / 256;
860
                int y = s->height - s->values[i].p[s->rgba_map[c]] * s->height / 256;
861
862
                draw_line(&s->draw, s->ox + x, s->oy + y, s->ox + px, s->oy + py, frame, s->colors[c]);
863
            }
864
        }
865
    }
866
}
867
868
869
static void draw_trace16(OscilloscopeContext *s, AVFrame *frame)
870
{
871
    int i, c;
872
873
    for (i = 1; i < s->nb_values; i++) {
874
        for (c = 0; c < s->nb_comps; c++) {
875
            if ((1 << c) & s->components) {
876
                int x = i * s->width / s->nb_values;
877
                int px = (i - 1) * s->width / s->nb_values;
878
                int py = s->height - s->values[i-1].p[s->rgba_map[c]] * s->height / s->max;
879
                int y = s->height - s->values[i].p[s->rgba_map[c]] * s->height / s->max;
880
881
                draw_line(&s->draw, s->ox + x, s->oy + y, s->ox + px, s->oy + py, frame, s->colors[c]);
882
            }
883
        }
884
    }
885
}
886
887
static void update_oscilloscope(AVFilterContext *ctx)
888
{
889
    OscilloscopeContext *s = ctx->priv;
890
    AVFilterLink *inlink = ctx->inputs[0];
891
    int cx, cy, size;
892
    double tilt;
893
894
    ff_draw_color(&s->draw, &s->dark,    (uint8_t[]){   0,   0,   0, s->o * 255} );
895
    s->height = s->theight * inlink->h;
896
    s->width = s->twidth * inlink->w;
897
    size = hypot(inlink->w, inlink->h);
898
    size *= s->size;
899
    tilt  = (s->tilt - 0.5) * M_PI;
900
    cx = s->xpos * (inlink->w - 1);
901
    cy = s->ypos * (inlink->h - 1);
902
    s->x1 = cx - size / 2.0 * cos(tilt);
903
    s->x2 = cx + size / 2.0 * cos(tilt);
904
    s->y1 = cy - size / 2.0 * sin(tilt);
905
    s->y2 = cy + size / 2.0 * sin(tilt);
906
    s->ox = (inlink->w - s->width) * s->tx;
907
    s->oy = (inlink->h - s->height) * s->ty;
908
}
909
910
static int oscilloscope_config_input(AVFilterLink *inlink)
911
{
912
    OscilloscopeContext *s = inlink->dst->priv;
913
    int size;
914
915
    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
916
    ff_draw_init(&s->draw, inlink->format, 0);
917
    ff_draw_color(&s->draw, &s->black,   (uint8_t[]){   0,   0,   0, 255} );
918
    ff_draw_color(&s->draw, &s->white,   (uint8_t[]){ 255, 255, 255, 255} );
919
    ff_draw_color(&s->draw, &s->green,   (uint8_t[]){   0, 255,   0, 255} );
920
    ff_draw_color(&s->draw, &s->blue,    (uint8_t[]){   0,   0, 255, 255} );
921
    ff_draw_color(&s->draw, &s->red,     (uint8_t[]){ 255,   0,   0, 255} );
922
    ff_draw_color(&s->draw, &s->cyan,    (uint8_t[]){   0, 255, 255, 255} );
923
    ff_draw_color(&s->draw, &s->magenta, (uint8_t[]){ 255,   0, 255, 255} );
924
    ff_draw_color(&s->draw, &s->gray,    (uint8_t[]){ 128, 128, 128, 255} );
925
    s->nb_comps = s->draw.desc->nb_components;
926
    s->is_rgb   = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB;
927
928
    if (s->is_rgb) {
929
        s->colors[0] = &s->red;
930
        s->colors[1] = &s->green;
931
        s->colors[2] = &s->blue;
932
        s->colors[3] = &s->white;
933
        ff_fill_rgba_map(s->rgba_map, inlink->format);
934
    } else {
935
        s->colors[0] = &s->white;
936
        s->colors[1] = &s->cyan;
937
        s->colors[2] = &s->magenta;
938
        s->colors[3] = &s->white;
939
        s->rgba_map[0] = 0;
940
        s->rgba_map[1] = 1;
941
        s->rgba_map[2] = 2;
942
        s->rgba_map[3] = 3;
943
    }
944
945
    if (s->draw.desc->comp[0].depth <= 8) {
946
        s->pick_color = pick_color8;
947
        s->draw_trace = draw_trace8;
948
    } else {
949
        s->pick_color = pick_color16;
950
        s->draw_trace = draw_trace16;
951
    }
952
953
    s->max = (1 << s->draw.desc->comp[0].depth);
954
    size = hypot(inlink->w, inlink->h);
955
956
    s->values = av_calloc(size, sizeof(*s->values));
957
    if (!s->values)
958
        return AVERROR(ENOMEM);
959
960
    update_oscilloscope(inlink->dst);
961
962
    return 0;
963
}
964
965
static void draw_scope(OscilloscopeContext *s, int x0, int y0, int x1, int y1,
966
                       AVFrame *out, PixelValues *p, int state)
967
{
968
    int dx = FFABS(x1 - x0), sx = x0 < x1 ? 1 : -1;
969
    int dy = FFABS(y1 - y0), sy = y0 < y1 ? 1 : -1;
970
    int err = (dx > dy ? dx : -dy) / 2, e2;
971
972
    for (;;) {
973
        if (x0 >= 0 && y0 >= 0 && x0 < out->width && y0 < out->height) {
974
            FFDrawColor color = { { 0 } };
975
            int value[4] = { 0 };
976
977
            s->pick_color(&s->draw, &color, out, x0, y0, value);
978
            s->values[s->nb_values].p[0] = value[0];
979
            s->values[s->nb_values].p[1] = value[1];
980
            s->values[s->nb_values].p[2] = value[2];
981
            s->values[s->nb_values].p[3] = value[3];
982
            s->nb_values++;
983
984
            if (s->scope) {
985
                if (s->draw.desc->comp[0].depth == 8) {
986
                    if (s->draw.nb_planes == 1) {
987
                        int i;
988
989
                        for (i = 0; i < s->nb_comps; i++)
990
                            out->data[0][out->linesize[0] * y0 + x0 * s->draw.pixelstep[0] + i] = 255 * ((s->nb_values + state) & 1);
991
                    } else {
992
                        out->data[0][out->linesize[0] * y0 + x0] = 255 * ((s->nb_values + state) & 1);
993
                    }
994
                } else {
995
                    if (s->draw.nb_planes == 1) {
996
                        int i;
997
998
                        for (i = 0; i < s->nb_comps; i++)
999
                            AV_WN16(out->data[0] + out->linesize[0] * y0 + x0 * s->draw.pixelstep[0] + i, (s->max - 1) * ((s->nb_values + state) & 1));
1000
                    } else {
1001
                        AV_WN16(out->data[0] + out->linesize[0] * y0 + 2 * x0, (s->max - 1) * ((s->nb_values + state) & 1));
1002
                    }
1003
                }
1004
            }
1005
        }
1006
1007
        if (x0 == x1 && y0 == y1)
1008
            break;
1009
1010
        e2 = err;
1011
1012
        if (e2 >-dx) {
1013
            err -= dy;
1014
            x0 += sx;
1015
        }
1016
1017
        if (e2 < dy) {
1018
            err += dx;
1019
            y0 += sy;
1020
        }
1021
    }
1022
}
1023
1024
static int oscilloscope_filter_frame(AVFilterLink *inlink, AVFrame *frame)
1025
{
1026
    AVFilterContext *ctx  = inlink->dst;
1027
    OscilloscopeContext *s = ctx->priv;
1028
    AVFilterLink *outlink = ctx->outputs[0];
1029
    float average[4] = { 0 };
1030
    int max[4] = { 0 };
1031
    int min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
1032
    int i, c;
1033
1034
    s->nb_values = 0;
1035
    draw_scope(s, s->x1, s->y1, s->x2, s->y2, frame, s->values, inlink->frame_count_in & 1);
1036
    ff_blend_rectangle(&s->draw, &s->dark, frame->data, frame->linesize,
1037
                       frame->width, frame->height,
1038
                       s->ox, s->oy, s->width, s->height + 20 * s->statistics);
1039
1040
    if (s->grid && outlink->h >= 10) {
1041
        ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
1042
                          s->ox, s->oy, s->width - 1, 1);
1043
1044
        for (i = 1; i < 5; i++) {
1045
            ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
1046
                              s->ox, s->oy + i * (s->height - 1) / 4, s->width, 1);
1047
        }
1048
1049
        for (i = 0; i < 10; i++) {
1050
            ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
1051
                              s->ox + i * (s->width - 1) / 10, s->oy, 1, s->height);
1052
        }
1053
1054
        ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
1055
                          s->ox + s->width - 1, s->oy, 1, s->height);
1056
    }
1057
1058
    s->draw_trace(s, frame);
1059
1060
    for (i = 0; i < s->nb_values; i++) {
1061
        for (c = 0; c < s->nb_comps; c++) {
1062
            if ((1 << c) & s->components) {
1063
                max[c] = FFMAX(max[c], s->values[i].p[s->rgba_map[c]]);
1064
                min[c] = FFMIN(min[c], s->values[i].p[s->rgba_map[c]]);
1065
                average[c] += s->values[i].p[s->rgba_map[c]];
1066
            }
1067
        }
1068
    }
1069
    for (c = 0; c < s->nb_comps; c++) {
1070
        average[c] /= s->nb_values;
1071
    }
1072
1073
    if (s->statistics && s->height > 10 && s->width > 280 * av_popcount(s->components)) {
1074
        for (c = 0, i = 0; c < s->nb_comps; c++) {
1075
            if ((1 << c) & s->components) {
1076
                const char rgba[4] = { 'R', 'G', 'B', 'A' };
1077
                const char yuva[4] = { 'Y', 'U', 'V', 'A' };
1078
                char text[128];
1079
1080
                snprintf(text, sizeof(text), "%c avg:%.1f min:%d max:%d\n", s->is_rgb ? rgba[c] : yuva[c], average[c], min[c], max[c]);
1081
                draw_text(&s->draw, frame, &s->white, s->ox +  2 + 280 * i++, s->oy + s->height + 4, text, 0);
1082
            }
1083
        }
1084
    }
1085
1086
    return ff_filter_frame(outlink, frame);
1087
}
1088
1089
static int oscilloscope_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
1090
                                        char *res, int res_len, int flags)
1091
{
1092
    int ret;
1093
1094
    ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
1095
    if (ret < 0)
1096
        return ret;
1097
1098
    update_oscilloscope(ctx);
1099
1100
    return 0;
1101
}
1102
1103
static const AVFilterPad oscilloscope_inputs[] = {
1104
    {
1105
        .name           = "default",
1106
        .type           = AVMEDIA_TYPE_VIDEO,
1107
        .filter_frame   = oscilloscope_filter_frame,
1108
        .config_props   = oscilloscope_config_input,
1109
        .needs_writable = 1,
1110
    },
1111
    { NULL }
1112
};
1113
1114
static const AVFilterPad oscilloscope_outputs[] = {
1115
    {
1116
        .name         = "default",
1117
        .type         = AVMEDIA_TYPE_VIDEO,
1118
    },
1119
    { NULL }
1120
};
1121
1122
AVFilter ff_vf_oscilloscope = {
1123
    .name          = "oscilloscope",
1124
    .description   = NULL_IF_CONFIG_SMALL("2D Video Oscilloscope."),
1125
    .priv_size     = sizeof(OscilloscopeContext),
1126
    .priv_class    = &oscilloscope_class,
1127
    .query_formats = query_formats,
1128
    .uninit        = oscilloscope_uninit,
1129
    .inputs        = oscilloscope_inputs,
1130
    .outputs       = oscilloscope_outputs,
1131
    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
1132
    .process_command = oscilloscope_process_command,
1133
};