[FFmpeg-devel] [PATCH] avfilter: add pixscope filter

Dave Rice dave at dericed.com
Mon Apr 24 22:29:02 EEST 2017


> On Apr 22, 2017, at 4:29 PM, Paul B Mahol <onemda at gmail.com> wrote:
> 
> Signed-off-by: Paul B Mahol <onemda at gmail.com>
> ---
> libavfilter/Makefile       |   1 +
> libavfilter/allfilters.c   |   1 +
> libavfilter/vf_datascope.c | 218 +++++++++++++++++++++++++++++++++++++++++++--
> 3 files changed, 213 insertions(+), 7 deletions(-)
> 
> diff --git a/libavfilter/Makefile b/libavfilter/Makefile
> index 50c5132..feb1404 100644
> --- a/libavfilter/Makefile
> +++ b/libavfilter/Makefile
> @@ -244,6 +244,7 @@ OBJS-$(CONFIG_PERMS_FILTER)                  += f_perms.o
> OBJS-$(CONFIG_PERSPECTIVE_FILTER)            += vf_perspective.o
> OBJS-$(CONFIG_PHASE_FILTER)                  += vf_phase.o
> OBJS-$(CONFIG_PIXDESCTEST_FILTER)            += vf_pixdesctest.o
> +OBJS-$(CONFIG_PIXSCOPE_FILTER)               += vf_datascope.o
> OBJS-$(CONFIG_PP_FILTER)                     += vf_pp.o
> OBJS-$(CONFIG_PP7_FILTER)                    += vf_pp7.o
> OBJS-$(CONFIG_PREMULTIPLY_FILTER)            += vf_premultiply.o framesync.o
> diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
> index f482adb..262f14b 100644
> --- a/libavfilter/allfilters.c
> +++ b/libavfilter/allfilters.c
> @@ -254,6 +254,7 @@ static void register_all(void)
>     REGISTER_FILTER(PERSPECTIVE,    perspective,    vf);
>     REGISTER_FILTER(PHASE,          phase,          vf);
>     REGISTER_FILTER(PIXDESCTEST,    pixdesctest,    vf);
> +    REGISTER_FILTER(PIXSCOPE,       pixscope,       vf);
>     REGISTER_FILTER(PP,             pp,             vf);
>     REGISTER_FILTER(PP7,            pp7,            vf);
>     REGISTER_FILTER(PREMULTIPLY,    premultiply,    vf);
> diff --git a/libavfilter/vf_datascope.c b/libavfilter/vf_datascope.c
> index 01a5d99..057a503 100644
> --- a/libavfilter/vf_datascope.c
> +++ b/libavfilter/vf_datascope.c
> @@ -76,7 +76,7 @@ static int query_formats(AVFilterContext *ctx)
>     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
> }
> 
> -static void draw_text(DatascopeContext *s, AVFrame *frame, FFDrawColor *color,
> +static void draw_text(FFDrawContext *draw, AVFrame *frame, FFDrawColor *color,
>                       int x0, int y0, const uint8_t *text, int vertical)
> {
>     int x = x0;
> @@ -87,7 +87,7 @@ static void draw_text(DatascopeContext *s, AVFrame *frame, FFDrawColor *color,
>             y0 += 8;
>             continue;
>         }
> -        ff_blend_mask(&s->draw, color, frame->data, frame->linesize,
> +        ff_blend_mask(draw, color, frame->data, frame->linesize,
>                       frame->width, frame->height,
>                       avpriv_cga_font + *text * 8, 1, 8, 8, 0, 0, x, y0);
>         if (vertical) {
> @@ -201,7 +201,7 @@ static int filter_color2(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs
>                 char text[256];
> 
>                 snprintf(text, sizeof(text), format[C>>2], value[p]);
> -                draw_text(s, out, &reverse, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
> +                draw_text(&s->draw, out, &reverse, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
>             }
>         }
>     }
> @@ -239,7 +239,7 @@ static int filter_color(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
>                 char text[256];
> 
>                 snprintf(text, sizeof(text), format[C>>2], value[p]);
> -                draw_text(s, out, &color, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
> +                draw_text(&s->draw, out, &color, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
>             }
>         }
>     }
> @@ -276,7 +276,7 @@ static int filter_mono(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
>                 char text[256];
> 
>                 snprintf(text, sizeof(text), format[C>>2], value[p]);
> -                draw_text(s, out, &s->white, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
> +                draw_text(&s->draw, out, &s->white, xoff + x * C * 10 + 2, yoff + y * P * 12 + p * 10 + 2, text, 0);
>             }
>         }
>     }
> @@ -328,7 +328,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>             ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
>                               0, xmaxlen + y * P * 12 + (P + 1) * P - 2, ymaxlen, 10);
> 
> -            draw_text(s, out, &s->yellow, 2, xmaxlen + y * P * 12 + (P + 1) * P, text, 0);
> +            draw_text(&s->draw, out, &s->yellow, 2, xmaxlen + y * P * 12 + (P + 1) * P, text, 0);
>         }
> 
>         for (x = 0; x < X; x++) {
> @@ -337,7 +337,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
>             ff_fill_rectangle(&s->draw, &s->gray, out->data, out->linesize,
>                               ymaxlen + x * C * 10 + 2 * C - 2, 0, 10, xmaxlen);
> 
> -            draw_text(s, out, &s->yellow, ymaxlen + x * C * 10 + 2 * C, 2, text, 1);
> +            draw_text(&s->draw, out, &s->yellow, ymaxlen + x * C * 10 + 2 * C, 2, text, 1);
>         }
>     }
> 
> @@ -419,3 +419,207 @@ AVFilter ff_vf_datascope = {
>     .outputs       = outputs,
>     .flags         = AVFILTER_FLAG_SLICE_THREADS,
> };
> +
> +typedef struct PixscopeContext {
> +    const AVClass *class;
> +
> +    int x, y;
> +    int w, h;
> +    float o;
> +
> +    int nb_planes;
> +    int nb_comps;
> +    int is_rgb;
> +    uint8_t rgba_map[4];
> +    FFDrawContext draw;
> +    FFDrawColor   dark;
> +    FFDrawColor   black;
> +    FFDrawColor   white;
> +    FFDrawColor   green;
> +    FFDrawColor   blue;
> +    FFDrawColor   red;
> +
> +    void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value);
> +} PixscopeContext;
> +
> +#define POFFSET(x) offsetof(PixscopeContext, x)
> +#define PFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
> +
> +static const AVOption pixscope_options[] = {
> +    { "x",    "set scope x offset", POFFSET(x),    AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, PFLAGS },
> +    { "y",    "set scope y offset", POFFSET(y),    AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, PFLAGS },
> +    { "w",    "set scope width",    POFFSET(w),    AV_OPT_TYPE_INT, {.i64=1}, 1,      20, PFLAGS },
> +    { "h",    "set scope height",   POFFSET(h),    AV_OPT_TYPE_INT, {.i64=1}, 1,      20, PFLAGS },
> +    { "o",    "set scope opacity",  POFFSET(o),    AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0,   1, PFLAGS },
> +    { NULL }
> +};
> +
> +AVFILTER_DEFINE_CLASS(pixscope);
> +
> +static int pixscope_config_input(AVFilterLink *inlink)
> +{
> +    PixscopeContext *s = inlink->dst->priv;
> +
> +    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
> +    ff_draw_init(&s->draw, inlink->format, 0);
> +    ff_draw_color(&s->draw, &s->dark,  (uint8_t[]){ 0, 0, 0, s->o * 255} );
> +    ff_draw_color(&s->draw, &s->black, (uint8_t[]){ 0, 0, 0, 255} );
> +    ff_draw_color(&s->draw, &s->white, (uint8_t[]){ 255, 255, 255, 255} );
> +    ff_draw_color(&s->draw, &s->green, (uint8_t[]){   0, 255,   0, 255} );
> +    ff_draw_color(&s->draw, &s->blue,  (uint8_t[]){   0,   0, 255, 255} );
> +    ff_draw_color(&s->draw, &s->red,   (uint8_t[]){ 255,   0,   0, 255} );
> +    s->nb_comps = s->draw.desc->nb_components;
> +    s->is_rgb   = s->draw.desc->flags & AV_PIX_FMT_FLAG_RGB;
> +
> +    if (s->is_rgb) {
> +        ff_fill_rgba_map(s->rgba_map, inlink->format);
> +    } else {
> +        s->rgba_map[0] = 0;
> +        s->rgba_map[1] = 1;
> +        s->rgba_map[2] = 2;
> +        s->rgba_map[3] = 3;
> +    }
> +
> +    if (s->draw.desc->comp[0].depth <= 8) {
> +        s->pick_color = pick_color8;
> +    } else {
> +        s->pick_color = pick_color16;
> +    }
> +
> +    if (inlink->w < 640 || inlink->h < 480) {
> +        av_log(inlink->dst, AV_LOG_ERROR, "min supported resolution is 640x480\n");
> +        return AVERROR(EINVAL);
> +    }
> +
> +    if (s->x + s->w >= inlink->w || s->y + s->h >= inlink->h) {
> +        av_log(inlink->dst, AV_LOG_WARNING, "scope position is out of range, clipping\n");
> +        s->x = FFMIN(s->x, inlink->w - s->w);
> +        s->y = FFMIN(s->y, inlink->h - s->h);
> +    }
> +
> +    return 0;
> +}
> +
> +static int pixscope_filter_frame(AVFilterLink *inlink, AVFrame *in)
> +{
> +    AVFilterContext *ctx  = inlink->dst;
> +    PixscopeContext *s = ctx->priv;
> +    AVFilterLink *outlink = ctx->outputs[0];
> +    float average[4] = { 0 };
> +    int max[4] = { 0 };
> +    int min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
> +    const char rgba[4] = { 'R', 'G', 'B', 'A' };
> +    const char yuva[4] = { 'Y', 'U', 'V', 'A' };
> +    int x, y, X, Y, i, w, h;
> +    char text[128];
> +
> +    w = 200 / s->w;
> +    h = 200 / s->h;
> +
> +    if (s->x <= 210 && s->y <= 300) {
> +        X = in->width - 210;
> +        Y = in->height - 300;
> +    } else {
> +        X = 0;
> +        Y = 0;
> +    }
> +
> +    ff_blend_rectangle(&s->draw, &s->dark, in->data, in->linesize,
> +                       in->width, in->height,
> +                       X,
> +                       Y,
> +                       210,
> +                       300);
> +
> +    ff_blend_rectangle(&s->draw, &s->black, in->data, in->linesize,
> +                       in->width, in->height,
> +                       s->x - 2, s->y - 2, s->w + 4, 1);
> +
> +    ff_blend_rectangle(&s->draw, &s->white, in->data, in->linesize,
> +                       in->width, in->height,
> +                       s->x - 1, s->y - 1, s->w + 2, 1);
> +
> +    ff_blend_rectangle(&s->draw, &s->white, in->data, in->linesize,
> +                       in->width, in->height,
> +                       s->x - 1, s->y - 1, 1, s->h + 2);
> +
> +    ff_blend_rectangle(&s->draw, &s->black, in->data, in->linesize,
> +                       in->width, in->height,
> +                       s->x - 2, s->y - 2, 1, s->h + 4);
> +
> +    ff_blend_rectangle(&s->draw, &s->white, in->data, in->linesize,
> +                       in->width, in->height,
> +                       s->x - 1, s->y + 1 + s->h, s->w + 3, 1);
> +
> +    ff_blend_rectangle(&s->draw, &s->black, in->data, in->linesize,
> +                       in->width, in->height,
> +                       s->x - 2, s->y + 2 + s->h, s->w + 4, 1);
> +
> +    ff_blend_rectangle(&s->draw, &s->white, in->data, in->linesize,
> +                       in->width, in->height,
> +                       s->x + 1 + s->w, s->y - 1, 1, s->h + 2);
> +
> +    ff_blend_rectangle(&s->draw, &s->black, in->data, in->linesize,
> +                       in->width, in->height,
> +                       s->x + 2 + s->w, s->y - 2, 1, s->h + 5);
> +
> +    for (y = 0; y < s->h; y++) {
> +        for (x = 0; x < s->w; x++) {
> +            FFDrawColor color = { { 0 } };
> +            int value[4] = { 0 };
> +
> +            s->pick_color(&s->draw, &color, in, x + s->x, y + s->y, value);
> +            ff_fill_rectangle(&s->draw, &color, in->data, in->linesize,
> +                              x * w + (210 - (s->w * w)) / 2 + X, y * h + 2 + Y, w, h);
> +            for (i = 0; i < 4; i++) {
> +                average[i] += value[i];
> +                min[i]      = FFMIN(min[i], value[i]);
> +                max[i]      = FFMAX(max[i], value[i]);
> +            }
> +        }
> +    }
> +
> +    for (i = 0; i < 4; i++) {
> +        average[i] /= s->w * s->h;
> +    }
> +
> +    snprintf(text, sizeof(text), "CH  AVG     MIN    MAX\n");
> +    draw_text(&s->draw, in, &s->white,                         X + 18, Y + 30 + 200 +  2, text, 0);
> +    snprintf(text, sizeof(text), "%c  %07.1f %05d %05d\n", s->is_rgb ? rgba[0] : yuva[0], average[s->rgba_map[0]], min[s->rgba_map[0]], max[s->rgba_map[0]]);
> +    draw_text(&s->draw, in, s->is_rgb ? &s->red   : &s->white, X + 18, Y + 30 + 200 + 15, text, 0);
> +    snprintf(text, sizeof(text), "%c  %07.1f %05d %05d\n", s->is_rgb ? rgba[1] : yuva[1], average[s->rgba_map[1]], min[s->rgba_map[1]], max[s->rgba_map[1]]);
> +    draw_text(&s->draw, in, s->is_rgb ? &s->green : &s->blue,  X + 18, Y + 30 + 200 + 30, text, 0);
> +    snprintf(text, sizeof(text), "%c  %07.1f %05d %05d\n", s->is_rgb ? rgba[2] : yuva[2], average[s->rgba_map[2]], min[s->rgba_map[2]], max[s->rgba_map[2]]);
> +    draw_text(&s->draw, in, s->is_rgb ? &s->blue  : &s->red,   X + 18, Y + 30 + 200 + 45, text, 0);
> +
> +    return ff_filter_frame(outlink, in);
> +}
> +
> +static const AVFilterPad pixscope_inputs[] = {
> +    {
> +        .name           = "default",
> +        .type           = AVMEDIA_TYPE_VIDEO,
> +        .filter_frame   = pixscope_filter_frame,
> +        .config_props   = pixscope_config_input,
> +        .needs_writable = 1,
> +    },
> +    { NULL }
> +};
> +
> +static const AVFilterPad pixscope_outputs[] = {
> +    {
> +        .name         = "default",
> +        .type         = AVMEDIA_TYPE_VIDEO,
> +    },
> +    { NULL }
> +};
> +
> +AVFilter ff_vf_pixscope = {
> +    .name          = "pixscope",
> +    .description   = NULL_IF_CONFIG_SMALL("Pixel data analysis."),
> +    .priv_size     = sizeof(PixscopeContext),
> +    .priv_class    = &pixscope_class,
> +    .query_formats = query_formats,
> +    .inputs        = pixscope_inputs,
> +    .outputs       = pixscope_outputs,
> +};
> -- 
> 2.9.3

I tested this filter and find it very useful, but could it be adjusted to handle full range video. Currently when I test with a sample such as:

./ffplay -f lavfi -i color=c=white:s=640x480,format=yuv444p,lutyuv=y=255:u=248:v=10,pixscope

The outputs list Y=235, U=240, V=16.

Dave Rice



More information about the ffmpeg-devel mailing list