2 * Copyright (c) 2016 Paul B Mahol
4 * This file is part of FFmpeg.
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.
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.
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
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"
28 #include "drawutils.h"
33 typedef struct DatascopeContext {
50 void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
51 void (*reverse_color)(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse);
52 int (*filter)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
55 #define OFFSET(x) offsetof(DatascopeContext, x)
56 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
58 static const AVOption datascope_options[] = {
59 { "size", "set output size", OFFSET(ow), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
60 { "s", "set output size", OFFSET(ow), AV_OPT_TYPE_IMAGE_SIZE, {.str="hd720"}, 0, 0, FLAGS },
61 { "x", "set x offset", OFFSET(x), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
62 { "y", "set y offset", OFFSET(y), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS },
63 { "mode", "set scope mode", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, 2, FLAGS, "mode" },
64 { "mono", NULL, 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "mode" },
65 { "color", NULL, 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "mode" },
66 { "color2", NULL, 0, AV_OPT_TYPE_CONST, {.i64=2}, 0, 0, FLAGS, "mode" },
67 { "axis", "draw column/row numbers", OFFSET(axis), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS },
68 { "opacity", "set background opacity", OFFSET(opacity), AV_OPT_TYPE_FLOAT, {.dbl=0.75}, 0, 1, FLAGS },
72 AVFILTER_DEFINE_CLASS(datascope);
74 static int query_formats(AVFilterContext *ctx)
76 return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
79 static void draw_text(FFDrawContext *draw, AVFrame *frame, FFDrawColor *color,
80 int x0, int y0, const uint8_t *text, int vertical)
84 for (; *text; text++) {
90 ff_blend_mask(draw, color, frame->data, frame->linesize,
91 frame->width, frame->height,
92 avpriv_cga_font + *text * 8, 1, 8, 8, 0, 0, x, y0);
102 static void pick_color8(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value)
106 color->rgba[3] = 255;
107 for (p = 0; p < draw->nb_planes; p++) {
108 if (draw->nb_planes == 1) {
109 for (i = 0; i < 4; i++) {
110 value[i] = in->data[0][y * in->linesize[0] + x * draw->pixelstep[0] + i];
111 color->comp[0].u8[i] = value[i];
114 value[p] = in->data[p][(y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p])];
115 color->comp[p].u8[0] = value[p];
120 static void pick_color16(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value)
124 color->rgba[3] = 255;
125 for (p = 0; p < draw->nb_planes; p++) {
126 if (draw->nb_planes == 1) {
127 for (i = 0; i < 4; i++) {
128 value[i] = AV_RL16(in->data[0] + y * in->linesize[0] + x * draw->pixelstep[0] + i * 2);
129 color->comp[0].u16[i] = value[i];
132 value[p] = AV_RL16(in->data[p] + (y >> draw->vsub[p]) * in->linesize[p] + (x >> draw->hsub[p]) * 2);
133 color->comp[p].u16[0] = value[p];
138 static void reverse_color8(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse)
142 reverse->rgba[3] = 255;
143 for (p = 0; p < draw->nb_planes; p++) {
144 reverse->comp[p].u8[0] = color->comp[p].u8[0] > 127 ? 0 : 255;
145 reverse->comp[p].u8[1] = color->comp[p].u8[1] > 127 ? 0 : 255;
146 reverse->comp[p].u8[2] = color->comp[p].u8[2] > 127 ? 0 : 255;
150 static void reverse_color16(FFDrawContext *draw, FFDrawColor *color, FFDrawColor *reverse)
154 reverse->rgba[3] = 255;
155 for (p = 0; p < draw->nb_planes; p++) {
156 const unsigned max = (1 << draw->desc->comp[p].depth) - 1;
157 const unsigned mid = (max + 1) / 2;
159 reverse->comp[p].u16[0] = color->comp[p].u16[0] > mid ? 0 : max;
160 reverse->comp[p].u16[1] = color->comp[p].u16[1] > mid ? 0 : max;
161 reverse->comp[p].u16[2] = color->comp[p].u16[2] > mid ? 0 : max;
165 typedef struct ThreadData {
170 static int filter_color2(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
172 DatascopeContext *s = ctx->priv;
173 AVFilterLink *outlink = ctx->outputs[0];
174 AVFilterLink *inlink = ctx->inputs[0];
175 ThreadData *td = arg;
176 AVFrame *in = td->in;
177 AVFrame *out = td->out;
178 const int xoff = td->xoff;
179 const int yoff = td->yoff;
180 const int P = FFMAX(s->nb_planes, s->nb_comps);
181 const int C = s->chars;
182 const int W = (outlink->w - xoff) / (C * 10);
183 const int H = (outlink->h - yoff) / (P * 12);
184 const char *format[2] = {"%02X\n", "%04X\n"};
185 const int slice_start = (W * jobnr) / nb_jobs;
186 const int slice_end = (W * (jobnr+1)) / nb_jobs;
189 for (y = 0; y < H && (y + s->y < inlink->h); y++) {
190 for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
191 FFDrawColor color = { { 0 } };
192 FFDrawColor reverse = { { 0 } };
193 int value[4] = { 0 };
195 s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
196 s->reverse_color(&s->draw, &color, &reverse);
197 ff_fill_rectangle(&s->draw, &color, out->data, out->linesize,
198 xoff + x * C * 10, yoff + y * P * 12, C * 10, P * 12);
200 for (p = 0; p < P; p++) {
203 snprintf(text, sizeof(text), format[C>>2], value[p]);
204 draw_text(&s->draw, out, &reverse, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
212 static int filter_color(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
214 DatascopeContext *s = ctx->priv;
215 AVFilterLink *outlink = ctx->outputs[0];
216 AVFilterLink *inlink = ctx->inputs[0];
217 ThreadData *td = arg;
218 AVFrame *in = td->in;
219 AVFrame *out = td->out;
220 const int xoff = td->xoff;
221 const int yoff = td->yoff;
222 const int P = FFMAX(s->nb_planes, s->nb_comps);
223 const int C = s->chars;
224 const int W = (outlink->w - xoff) / (C * 10);
225 const int H = (outlink->h - yoff) / (P * 12);
226 const char *format[2] = {"%02X\n", "%04X\n"};
227 const int slice_start = (W * jobnr) / nb_jobs;
228 const int slice_end = (W * (jobnr+1)) / nb_jobs;
231 for (y = 0; y < H && (y + s->y < inlink->h); y++) {
232 for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
233 FFDrawColor color = { { 0 } };
234 int value[4] = { 0 };
236 s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
238 for (p = 0; p < P; p++) {
241 snprintf(text, sizeof(text), format[C>>2], value[p]);
242 draw_text(&s->draw, out, &color, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
250 static int filter_mono(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
252 DatascopeContext *s = ctx->priv;
253 AVFilterLink *outlink = ctx->outputs[0];
254 AVFilterLink *inlink = ctx->inputs[0];
255 ThreadData *td = arg;
256 AVFrame *in = td->in;
257 AVFrame *out = td->out;
258 const int xoff = td->xoff;
259 const int yoff = td->yoff;
260 const int P = FFMAX(s->nb_planes, s->nb_comps);
261 const int C = s->chars;
262 const int W = (outlink->w - xoff) / (C * 10);
263 const int H = (outlink->h - yoff) / (P * 12);
264 const char *format[2] = {"%02X\n", "%04X\n"};
265 const int slice_start = (W * jobnr) / nb_jobs;
266 const int slice_end = (W * (jobnr+1)) / nb_jobs;
269 for (y = 0; y < H && (y + s->y < inlink->h); y++) {
270 for (x = slice_start; x < slice_end && (x + s->x < inlink->w); x++) {
271 FFDrawColor color = { { 0 } };
272 int value[4] = { 0 };
274 s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
275 for (p = 0; p < P; p++) {
278 snprintf(text, sizeof(text), format[C>>2], value[p]);
279 draw_text(&s->draw, out, &s->white, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
287 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
289 AVFilterContext *ctx = inlink->dst;
290 DatascopeContext *s = ctx->priv;
291 AVFilterLink *outlink = ctx->outputs[0];
292 ThreadData td = { 0 };
297 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
300 return AVERROR(ENOMEM);
304 ff_fill_rectangle(&s->draw, &s->black, out->data, out->linesize,
305 0, 0, outlink->w, outlink->h);
308 const int P = FFMAX(s->nb_planes, s->nb_comps);
309 const int C = s->chars;
310 int Y = outlink->h / (P * 12);
311 int X = outlink->w / (C * 10);
312 char text[256] = { 0 };
315 snprintf(text, sizeof(text), "%d", s->y + Y);
316 ymaxlen = strlen(text);
318 snprintf(text, sizeof(text), "%d", s->x + X);
319 xmaxlen = strlen(text);
322 Y = (outlink->h - xmaxlen) / (P * 12);
323 X = (outlink->w - ymaxlen) / (C * 10);
325 for (y = 0; y < Y; y++) {
326 snprintf(text, sizeof(text), "%d", s->y + y);
328 ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
329 0, xmaxlen + y * P * 12 + (P + 1) * P - 2, ymaxlen, 10);
331 draw_text(&s->draw, out, &s->yellow, 2, xmaxlen + y * P * 12 + (P + 1) * P, text, 0);
334 for (x = 0; x < X; x++) {
335 snprintf(text, sizeof(text), "%d", s->x + x);
337 ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
338 ymaxlen + x * C * 10 + 2 * C - 2, 0, 10, xmaxlen);
340 draw_text(&s->draw, out, &s->yellow, ymaxlen + x * C * 10 + 2 * C, 2, text, 1);
344 td.in = in; td.out = out, td.yoff = xmaxlen, td.xoff = ymaxlen;
345 ctx->internal->execute(ctx, s->filter, &td, NULL, FFMIN(ff_filter_get_nb_threads(ctx), FFMAX(outlink->w / 20, 1)));
348 return ff_filter_frame(outlink, out);
351 static int config_input(AVFilterLink *inlink)
353 DatascopeContext *s = inlink->dst->priv;
354 uint8_t alpha = s->opacity * 255;
356 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
357 ff_draw_init(&s->draw, inlink->format, 0);
358 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
359 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, alpha} );
360 ff_draw_color(&s->draw, &s->yellow, (uint8_t[]){ 255, 255, 0, 255} );
361 ff_draw_color(&s->draw, &s->gray, (uint8_t[]){ 77, 77, 77, 255} );
362 s->chars = (s->draw.desc->comp[0].depth + 7) / 8 * 2;
363 s->nb_comps = s->draw.desc->nb_components;
366 case 0: s->filter = filter_mono; break;
367 case 1: s->filter = filter_color; break;
368 case 2: s->filter = filter_color2; break;
371 if (s->draw.desc->comp[0].depth <= 8) {
372 s->pick_color = pick_color8;
373 s->reverse_color = reverse_color8;
375 s->pick_color = pick_color16;
376 s->reverse_color = reverse_color16;
382 static int config_output(AVFilterLink *outlink)
384 DatascopeContext *s = outlink->src->priv;
388 outlink->sample_aspect_ratio = (AVRational){1,1};
393 static const AVFilterPad inputs[] = {
396 .type = AVMEDIA_TYPE_VIDEO,
397 .filter_frame = filter_frame,
398 .config_props = config_input,
403 static const AVFilterPad outputs[] = {
406 .type = AVMEDIA_TYPE_VIDEO,
407 .config_props = config_output,
412 AVFilter ff_vf_datascope = {
414 .description = NULL_IF_CONFIG_SMALL("Video data analysis."),
415 .priv_size = sizeof(DatascopeContext),
416 .priv_class = &datascope_class,
417 .query_formats = query_formats,
420 .flags = AVFILTER_FLAG_SLICE_THREADS,
423 typedef struct PixscopeContext {
424 const AVClass *class;
445 FFDrawColor *colors[4];
447 void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
450 #define POFFSET(x) offsetof(PixscopeContext, x)
452 static const AVOption pixscope_options[] = {
453 { "x", "set scope x offset", POFFSET(xpos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
454 { "y", "set scope y offset", POFFSET(ypos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
455 { "w", "set scope width", POFFSET(w), AV_OPT_TYPE_INT, {.i64=7}, 1, 80, FLAGS },
456 { "h", "set scope height", POFFSET(h), AV_OPT_TYPE_INT, {.i64=7}, 1, 80, FLAGS },
457 { "o", "set window opacity", POFFSET(o), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
458 { "wx", "set window x offset", POFFSET(wx), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1, FLAGS },
459 { "wy", "set window y offset", POFFSET(wy), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1, FLAGS },
463 AVFILTER_DEFINE_CLASS(pixscope);
465 static int pixscope_config_input(AVFilterLink *inlink)
467 PixscopeContext *s = inlink->dst->priv;
469 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
470 ff_draw_init(&s->draw, inlink->format, 0);
471 ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} );
472 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
473 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
474 ff_draw_color(&s->draw, &s->green, (uint8_t[]){ 0, 255, 0, 255} );
475 ff_draw_color(&s->draw, &s->blue, (uint8_t[]){ 0, 0, 255, 255} );
476 ff_draw_color(&s->draw, &s->red, (uint8_t[]){ 255, 0, 0, 255} );
477 s->nb_comps = s->draw.desc->nb_components;
478 s->is_rgb = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB;
481 s->colors[0] = &s->red;
482 s->colors[1] = &s->green;
483 s->colors[2] = &s->blue;
484 s->colors[3] = &s->white;
485 ff_fill_rgba_map(s->rgba_map, inlink->format);
487 s->colors[0] = &s->white;
488 s->colors[1] = &s->blue;
489 s->colors[2] = &s->red;
490 s->colors[3] = &s->white;
497 if (s->draw.desc->comp[0].depth <= 8) {
498 s->pick_color = pick_color8;
500 s->pick_color = pick_color16;
503 if (inlink->w < 640 || inlink->h < 480) {
504 av_log(inlink->dst, AV_LOG_ERROR, "min supported resolution is 640x480\n");
505 return AVERROR(EINVAL);
510 s->x = s->xpos * (inlink->w - 1);
511 s->y = s->ypos * (inlink->h - 1);
512 if (s->x + s->w >= inlink->w || s->y + s->h >= inlink->h) {
513 av_log(inlink->dst, AV_LOG_WARNING, "scope position is out of range, clipping\n");
514 s->x = FFMIN(s->x, inlink->w - s->w);
515 s->y = FFMIN(s->y, inlink->h - s->h);
521 static int pixscope_filter_frame(AVFilterLink *inlink, AVFrame *in)
523 AVFilterContext *ctx = inlink->dst;
524 PixscopeContext *s = ctx->priv;
525 AVFilterLink *outlink = ctx->outputs[0];
526 AVFrame *out = ff_get_video_buffer(outlink, in->width, in->height);
527 int max[4] = { 0 }, min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
528 float average[4] = { 0 };
529 double rms[4] = { 0 };
530 const char rgba[4] = { 'R', 'G', 'B', 'A' };
531 const char yuva[4] = { 'Y', 'U', 'V', 'A' };
532 int x, y, X, Y, i, w, h;
537 return AVERROR(ENOMEM);
539 av_frame_copy_props(out, in);
540 av_frame_copy(out, in);
546 X = (in->width - s->ww) * s->wx;
548 X = (in->width - s->ww) * -s->wx;
551 Y = (in->height - s->wh) * s->wy;
553 Y = (in->height - s->wh) * -s->wy;
557 if (s->x + s->w >= X && (s->x + s->w <= X + s->ww) &&
558 s->y + s->h >= Y && (s->y + s->h <= Y + s->wh)) {
559 X = (in->width - s->ww) * (1 + s->wx);
564 if (s->x + s->w >= X && (s->x + s->w <= X + s->ww) &&
565 s->y + s->h >= Y && (s->y + s->h <= Y + s->wh)) {
566 Y = (in->height - s->wh) * (1 + s->wy);
570 ff_blend_rectangle(&s->draw, &s->dark, out->data, out->linesize,
571 out->width, out->height,
577 for (y = 0; y < s->h; y++) {
578 for (x = 0; x < s->w; x++) {
579 FFDrawColor color = { { 0 } };
580 int value[4] = { 0 };
582 s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
583 ff_fill_rectangle(&s->draw, &color, out->data, out->linesize,
584 x * w + (s->ww - 4 - (s->w * w)) / 2 + X, y * h + 2 + Y, w, h);
585 for (i = 0; i < 4; i++) {
586 rms[i] += (double)value[i] * (double)value[i];
587 average[i] += value[i];
588 min[i] = FFMIN(min[i], value[i]);
589 max[i] = FFMAX(max[i], value[i]);
594 ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
595 out->width, out->height,
596 s->x - 2, s->y - 2, s->w + 4, 1);
598 ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
599 out->width, out->height,
600 s->x - 1, s->y - 1, s->w + 2, 1);
602 ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
603 out->width, out->height,
604 s->x - 1, s->y - 1, 1, s->h + 2);
606 ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
607 out->width, out->height,
608 s->x - 2, s->y - 2, 1, s->h + 4);
610 ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
611 out->width, out->height,
612 s->x - 1, s->y + 1 + s->h, s->w + 3, 1);
614 ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
615 out->width, out->height,
616 s->x - 2, s->y + 2 + s->h, s->w + 4, 1);
618 ff_blend_rectangle(&s->draw, &s->white, out->data, out->linesize,
619 out->width, out->height,
620 s->x + 1 + s->w, s->y - 1, 1, s->h + 2);
622 ff_blend_rectangle(&s->draw, &s->black, out->data, out->linesize,
623 out->width, out->height,
624 s->x + 2 + s->w, s->y - 2, 1, s->h + 5);
626 for (i = 0; i < 4; i++) {
627 rms[i] /= s->w * s->h;
628 rms[i] = sqrt(rms[i]);
629 average[i] /= s->w * s->h;
632 snprintf(text, sizeof(text), "CH AVG MIN MAX RMS\n");
633 draw_text(&s->draw, out, &s->white, X + 28, Y + s->ww + 20, text, 0);
634 for (i = 0; i < s->nb_comps; i++) {
635 int c = s->rgba_map[i];
637 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]);
638 draw_text(&s->draw, out, s->colors[i], X + 28, Y + s->ww + 20 * (i + 2), text, 0);
642 return ff_filter_frame(outlink, out);
645 static const AVFilterPad pixscope_inputs[] = {
648 .type = AVMEDIA_TYPE_VIDEO,
649 .filter_frame = pixscope_filter_frame,
650 .config_props = pixscope_config_input,
655 static const AVFilterPad pixscope_outputs[] = {
658 .type = AVMEDIA_TYPE_VIDEO,
663 AVFilter ff_vf_pixscope = {
665 .description = NULL_IF_CONFIG_SMALL("Pixel data analysis."),
666 .priv_size = sizeof(PixscopeContext),
667 .priv_class = &pixscope_class,
668 .query_formats = query_formats,
669 .inputs = pixscope_inputs,
670 .outputs = pixscope_outputs,
671 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
674 typedef struct PixelValues {
678 typedef struct OscilloscopeContext {
679 const AVClass *class;
685 float theight, twidth;
711 FFDrawColor *colors[4];
716 void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
717 void (*draw_trace)(struct OscilloscopeContext *s, AVFrame *frame);
718 } OscilloscopeContext;
720 #define OOFFSET(x) offsetof(OscilloscopeContext, x)
722 static const AVOption oscilloscope_options[] = {
723 { "x", "set scope x position", OOFFSET(xpos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
724 { "y", "set scope y position", OOFFSET(ypos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
725 { "s", "set scope size", OOFFSET(size), AV_OPT_TYPE_FLOAT, {.dbl=0.8}, 0, 1, FLAGS },
726 { "t", "set scope tilt", OOFFSET(tilt), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
727 { "o", "set trace opacity", OOFFSET(o), AV_OPT_TYPE_FLOAT, {.dbl=0.8}, 0, 1, FLAGS },
728 { "tx", "set trace x position", OOFFSET(tx), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS },
729 { "ty", "set trace y position", OOFFSET(ty), AV_OPT_TYPE_FLOAT, {.dbl=0.9}, 0, 1, FLAGS },
730 { "tw", "set trace width", OOFFSET(twidth), AV_OPT_TYPE_FLOAT, {.dbl=0.8},.1, 1, FLAGS },
731 { "th", "set trace height", OOFFSET(theight), AV_OPT_TYPE_FLOAT, {.dbl=0.3},.1, 1, FLAGS },
732 { "c", "set components to trace", OOFFSET(components), AV_OPT_TYPE_INT, {.i64=7}, 0, 15, FLAGS },
733 { "g", "draw trace grid", OOFFSET(grid), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
734 { "st", "draw statistics", OOFFSET(statistics), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
735 { "sc", "draw scope", OOFFSET(scope), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
739 AVFILTER_DEFINE_CLASS(oscilloscope);
741 static void oscilloscope_uninit(AVFilterContext *ctx)
743 OscilloscopeContext *s = ctx->priv;
745 av_freep(&s->values);
748 static void draw_line(FFDrawContext *draw, int x0, int y0, int x1, int y1,
749 AVFrame *out, FFDrawColor *color)
751 int dx = FFABS(x1 - x0), sx = x0 < x1 ? 1 : -1;
752 int dy = FFABS(y1 - y0), sy = y0 < y1 ? 1 : -1;
753 int err = (dx > dy ? dx : -dy) / 2, e2;
757 if (x0 >= 0 && y0 >= 0 && x0 < out->width && y0 < out->height) {
758 for (p = 0; p < draw->nb_planes; p++) {
759 if (draw->desc->comp[p].depth == 8) {
760 if (draw->nb_planes == 1) {
761 for (i = 0; i < 4; i++) {
762 out->data[0][y0 * out->linesize[0] + x0 * draw->pixelstep[0] + i] = color->comp[0].u8[i];
765 out->data[p][out->linesize[p] * (y0 >> draw->vsub[p]) + (x0 >> draw->hsub[p])] = color->comp[p].u8[0];
768 if (draw->nb_planes == 1) {
769 for (i = 0; i < 4; i++) {
770 AV_WN16(out->data[0] + y0 * out->linesize[0] + 2 * (x0 * draw->pixelstep[0] + i), color->comp[0].u16[i]);
773 AV_WN16(out->data[p] + out->linesize[p] * (y0 >> draw->vsub[p]) + (x0 >> draw->hsub[p]) * 2, color->comp[p].u16[0]);
779 if (x0 == x1 && y0 == y1)
796 static void draw_trace8(OscilloscopeContext *s, AVFrame *frame)
800 for (i = 1; i < s->nb_values; i++) {
801 for (c = 0; c < s->nb_comps; c++) {
802 if ((1 << c) & s->components) {
803 int x = i * s->width / s->nb_values;
804 int px = (i - 1) * s->width / s->nb_values;
805 int py = s->height - s->values[i-1].p[s->rgba_map[c]] * s->height / 256;
806 int y = s->height - s->values[i].p[s->rgba_map[c]] * s->height / 256;
808 draw_line(&s->draw, s->ox + x, s->oy + y, s->ox + px, s->oy + py, frame, s->colors[c]);
815 static void draw_trace16(OscilloscopeContext *s, AVFrame *frame)
819 for (i = 1; i < s->nb_values; i++) {
820 for (c = 0; c < s->nb_comps; c++) {
821 if ((1 << c) & s->components) {
822 int x = i * s->width / s->nb_values;
823 int px = (i - 1) * s->width / s->nb_values;
824 int py = s->height - s->values[i-1].p[s->rgba_map[c]] * s->height / s->max;
825 int y = s->height - s->values[i].p[s->rgba_map[c]] * s->height / s->max;
827 draw_line(&s->draw, s->ox + x, s->oy + y, s->ox + px, s->oy + py, frame, s->colors[c]);
833 static int oscilloscope_config_input(AVFilterLink *inlink)
835 OscilloscopeContext *s = inlink->dst->priv;
839 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
840 ff_draw_init(&s->draw, inlink->format, 0);
841 ff_draw_color(&s->draw, &s->dark, (uint8_t[]){ 0, 0, 0, s->o * 255} );
842 ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
843 ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
844 ff_draw_color(&s->draw, &s->green, (uint8_t[]){ 0, 255, 0, 255} );
845 ff_draw_color(&s->draw, &s->blue, (uint8_t[]){ 0, 0, 255, 255} );
846 ff_draw_color(&s->draw, &s->red, (uint8_t[]){ 255, 0, 0, 255} );
847 ff_draw_color(&s->draw, &s->cyan, (uint8_t[]){ 0, 255, 255, 255} );
848 ff_draw_color(&s->draw, &s->magenta, (uint8_t[]){ 255, 0, 255, 255} );
849 ff_draw_color(&s->draw, &s->gray, (uint8_t[]){ 128, 128, 128, 255} );
850 s->nb_comps = s->draw.desc->nb_components;
851 s->is_rgb = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB;
854 s->colors[0] = &s->red;
855 s->colors[1] = &s->green;
856 s->colors[2] = &s->blue;
857 s->colors[3] = &s->white;
858 ff_fill_rgba_map(s->rgba_map, inlink->format);
860 s->colors[0] = &s->white;
861 s->colors[1] = &s->cyan;
862 s->colors[2] = &s->magenta;
863 s->colors[3] = &s->white;
870 if (s->draw.desc->comp[0].depth <= 8) {
871 s->pick_color = pick_color8;
872 s->draw_trace = draw_trace8;
874 s->pick_color = pick_color16;
875 s->draw_trace = draw_trace16;
878 s->max = (1 << s->draw.desc->comp[0].depth);
879 cx = s->xpos * (inlink->w - 1);
880 cy = s->ypos * (inlink->h - 1);
881 s->height = s->theight * inlink->h;
882 s->width = s->twidth * inlink->w;
883 size = hypot(inlink->w, inlink->h);
885 s->values = av_calloc(size, sizeof(*s->values));
887 return AVERROR(ENOMEM);
890 tilt = (s->tilt - 0.5) * M_PI;
891 s->x1 = cx - size / 2.0 * cos(tilt);
892 s->x2 = cx + size / 2.0 * cos(tilt);
893 s->y1 = cy - size / 2.0 * sin(tilt);
894 s->y2 = cy + size / 2.0 * sin(tilt);
895 s->ox = (inlink->w - s->width) * s->tx;
896 s->oy = (inlink->h - s->height) * s->ty;
901 static void draw_scope(OscilloscopeContext *s, int x0, int y0, int x1, int y1,
902 AVFrame *out, PixelValues *p, int state)
904 int dx = FFABS(x1 - x0), sx = x0 < x1 ? 1 : -1;
905 int dy = FFABS(y1 - y0), sy = y0 < y1 ? 1 : -1;
906 int err = (dx > dy ? dx : -dy) / 2, e2;
909 if (x0 >= 0 && y0 >= 0 && x0 < out->width && y0 < out->height) {
910 FFDrawColor color = { { 0 } };
911 int value[4] = { 0 };
913 s->pick_color(&s->draw, &color, out, x0, y0, value);
914 s->values[s->nb_values].p[0] = value[0];
915 s->values[s->nb_values].p[1] = value[1];
916 s->values[s->nb_values].p[2] = value[2];
917 s->values[s->nb_values].p[3] = value[3];
921 if (s->draw.desc->comp[0].depth == 8) {
922 if (s->draw.nb_planes == 1) {
925 for (i = 0; i < s->draw.pixelstep[0]; i++)
926 out->data[0][out->linesize[0] * y0 + x0 * s->draw.pixelstep[0] + i] = 255 * ((s->nb_values + state) & 1);
928 out->data[0][out->linesize[0] * y0 + x0] = 255 * ((s->nb_values + state) & 1);
931 if (s->draw.nb_planes == 1) {
934 for (i = 0; i < s->draw.pixelstep[0]; i++)
935 AV_WN16(out->data[0] + out->linesize[0] * y0 + 2 * x0 * (s->draw.pixelstep[0] + i), (s->max - 1) * ((s->nb_values + state) & 1));
937 AV_WN16(out->data[0] + out->linesize[0] * y0 + 2 * x0, (s->max - 1) * ((s->nb_values + state) & 1));
943 if (x0 == x1 && y0 == y1)
960 static int oscilloscope_filter_frame(AVFilterLink *inlink, AVFrame *frame)
962 AVFilterContext *ctx = inlink->dst;
963 OscilloscopeContext *s = ctx->priv;
964 AVFilterLink *outlink = ctx->outputs[0];
965 float average[4] = { 0 };
967 int min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
971 draw_scope(s, s->x1, s->y1, s->x2, s->y2, frame, s->values, inlink->frame_count_in & 1);
972 ff_blend_rectangle(&s->draw, &s->dark, frame->data, frame->linesize,
973 frame->width, frame->height,
974 s->ox, s->oy, s->width, s->height + 20 * s->statistics);
977 ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
978 s->ox, s->oy, s->width - 1, 1);
980 for (i = 1; i < 5; i++) {
981 ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
982 s->ox, s->oy + i * (s->height - 1) / 4, s->width, 1);
985 for (i = 0; i < 10; i++) {
986 ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
987 s->ox + i * (s->width - 1) / 10, s->oy, 1, s->height);
990 ff_fill_rectangle(&s->draw, &s->gray, frame->data, frame->linesize,
991 s->ox + s->width - 1, s->oy, 1, s->height);
994 s->draw_trace(s, frame);
996 for (i = 0; i < s->nb_values; i++) {
997 for (c = 0; c < s->nb_comps; c++) {
998 if ((1 << c) & s->components) {
999 max[c] = FFMAX(max[c], s->values[i].p[s->rgba_map[c]]);
1000 min[c] = FFMIN(min[c], s->values[i].p[s->rgba_map[c]]);
1001 average[c] += s->values[i].p[s->rgba_map[c]];
1005 for (c = 0; c < s->nb_comps; c++) {
1006 average[c] /= s->nb_values;
1009 if (s->statistics && s->height > 10 && s->width > 280 * av_popcount(s->components)) {
1010 for (c = 0, i = 0; c < s->nb_comps; c++) {
1011 if ((1 << c) & s->components) {
1012 const char rgba[4] = { 'R', 'G', 'B', 'A' };
1013 const char yuva[4] = { 'Y', 'U', 'V', 'A' };
1016 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]);
1017 draw_text(&s->draw, frame, &s->white, s->ox + 2 + 280 * i++, s->oy + s->height + 4, text, 0);
1022 return ff_filter_frame(outlink, frame);
1025 static const AVFilterPad oscilloscope_inputs[] = {
1028 .type = AVMEDIA_TYPE_VIDEO,
1029 .filter_frame = oscilloscope_filter_frame,
1030 .config_props = oscilloscope_config_input,
1031 .needs_writable = 1,
1036 static const AVFilterPad oscilloscope_outputs[] = {
1039 .type = AVMEDIA_TYPE_VIDEO,
1044 AVFilter ff_vf_oscilloscope = {
1045 .name = "oscilloscope",
1046 .description = NULL_IF_CONFIG_SMALL("2D Video Oscilloscope."),
1047 .priv_size = sizeof(OscilloscopeContext),
1048 .priv_class = &oscilloscope_class,
1049 .query_formats = query_formats,
1050 .uninit = oscilloscope_uninit,
1051 .inputs = oscilloscope_inputs,
1052 .outputs = oscilloscope_outputs,
1053 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,