LCOV - code coverage report
Current view: top level - libavfilter - vf_datascope.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 485 0.0 %
Date: 2017-12-12 11:08:38 Functions: 0 21 0.0 %

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

Generated by: LCOV version 1.13