From 8341d0dd0e5fe5aecc2f4aca7e8447e6aeb7c124 Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Sat, 22 Apr 2017 22:02:49 +0200 Subject: [PATCH] avfilter: add pixscope filter Signed-off-by: Paul B Mahol --- doc/filters.texi | 23 ++++ libavfilter/Makefile | 1 + libavfilter/allfilters.c | 1 + libavfilter/version.h | 2 +- libavfilter/vf_datascope.c | 236 +++++++++++++++++++++++++++++++++++-- 5 files changed, 255 insertions(+), 8 deletions(-) diff --git a/doc/filters.texi b/doc/filters.texi index d49fec1264..773ab246b2 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -10925,6 +10925,29 @@ format=monow, pixdesctest can be used to test the monowhite pixel format descriptor definition. +@section pixscope + +Display sample values of color channels. Mainly useful for checking color and levels. + +The filters accept the following options: + +@table @option +@item x +Set scope X position, offset on X axis. + +@item y +Set scope Y position, offset on Y axis. + +@item w +Set scope width. + +@item h +Set scope height. + +@item o +Set window opacity. This window also holds statistics about pixel area. +@end table + @section pp Enable the specified chain of postprocessing subfilters using libpostproc. This diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 074c6907ef..e40c6fea32 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -245,6 +245,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 c69f79e6ac..0852b54e6f 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -255,6 +255,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/version.h b/libavfilter/version.h index 2fa8dbda95..fb232c8e8a 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -30,7 +30,7 @@ #include "libavutil/version.h" #define LIBAVFILTER_VERSION_MAJOR 6 -#define LIBAVFILTER_VERSION_MINOR 87 +#define LIBAVFILTER_VERSION_MINOR 88 #define LIBAVFILTER_VERSION_MICRO 100 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ diff --git a/libavfilter/vf_datascope.c b/libavfilter/vf_datascope.c index 01a5d992b0..37425b4508 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,225 @@ AVFilter ff_vf_datascope = { .outputs = outputs, .flags = AVFILTER_FLAG_SLICE_THREADS, }; + +typedef struct PixscopeContext { + const AVClass *class; + + float xpos, ypos; + int w, h; + float o; + + int x, y; + int ww, wh; + + 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; + FFDrawColor *colors[4]; + + void (*pick_color)(FFDrawContext *draw, FFDrawColor *color, AVFrame *in, int x, int y, int *value); +} PixscopeContext; + +#define POFFSET(x) offsetof(PixscopeContext, x) + +static const AVOption pixscope_options[] = { + { "x", "set scope x offset", POFFSET(xpos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS }, + { "y", "set scope y offset", POFFSET(ypos), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS }, + { "w", "set scope width", POFFSET(w), AV_OPT_TYPE_INT, {.i64=7}, 1, 80, FLAGS }, + { "h", "set scope height", POFFSET(h), AV_OPT_TYPE_INT, {.i64=7}, 1, 80, FLAGS }, + { "o", "set window opacity", POFFSET(o), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0, 1, FLAGS }, + { 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) { + s->colors[0] = &s->red; + s->colors[1] = &s->green; + s->colors[2] = &s->blue; + s->colors[3] = &s->white; + ff_fill_rgba_map(s->rgba_map, inlink->format); + } else { + s->colors[0] = &s->white; + s->colors[1] = &s->blue; + s->colors[2] = &s->red; + s->colors[3] = &s->white; + 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); + } + + s->ww = 300; + s->wh = 300 * 1.6180; + s->x = s->xpos * (inlink->w - 1); + s->y = s->ypos * (inlink->h - 1); + 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]; + int max[4] = { 0 }, min[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX }; + float average[4] = { 0 }; + double rms[4] = { 0 }; + 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 = s->ww / s->w; + h = s->ww / s->h; + + if (s->x <= s->ww && s->y <= s->wh) { + X = in->width - s->ww; + Y = in->height - s->wh; + } else { + X = 0; + Y = 0; + } + + ff_blend_rectangle(&s->draw, &s->dark, in->data, in->linesize, + in->width, in->height, + X, + Y, + s->ww, + s->wh); + + 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 + (s->ww - 4 - (s->w * w)) / 2 + X, y * h + 2 + Y, w, h); + for (i = 0; i < 4; i++) { + rms[i] += (double)value[i] * (double)value[i]; + average[i] += value[i]; + min[i] = FFMIN(min[i], value[i]); + max[i] = FFMAX(max[i], value[i]); + } + } + } + + 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 (i = 0; i < 4; i++) { + rms[i] /= s->w * s->h; + rms[i] = sqrt(rms[i]); + average[i] /= s->w * s->h; + } + + snprintf(text, sizeof(text), "CH AVG MIN MAX RMS\n"); + draw_text(&s->draw, in, &s->white, X + 28, Y + s->ww + 20, text, 0); + for (i = 0; i < s->nb_comps; i++) { + int c = s->rgba_map[i]; + + 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]); + draw_text(&s->draw, in, s->colors[i], X + 28, Y + s->ww + 20 * (i + 2), 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, +};