From patchwork Sat Apr 22 20:29:39 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul B Mahol X-Patchwork-Id: 3475 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.3.129 with SMTP id 123csp840949vsd; Sat, 22 Apr 2017 13:36:23 -0700 (PDT) X-Received: by 10.28.5.8 with SMTP id 8mr3768193wmf.70.1492893383615; Sat, 22 Apr 2017 13:36:23 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id y44si19975968wrc.257.2017.04.22.13.36.23; Sat, 22 Apr 2017 13:36:23 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CF852689758; Sat, 22 Apr 2017 23:36:19 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr0-f195.google.com (mail-wr0-f195.google.com [209.85.128.195]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C21CB6891DB for ; Sat, 22 Apr 2017 23:36:12 +0300 (EEST) Received: by mail-wr0-f195.google.com with SMTP id l44so16318911wrc.2 for ; Sat, 22 Apr 2017 13:36:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=Qr5IKi8WXqsIcGbDz4DO4fc5xGkJJ/vg89LDVU9TkbI=; b=WzO04haTdxjKbf0IpPz4IE8AUz2PMSI8rZC7jHi+AK7gyacgJiprQLtZUctqTu6Sjq QFOGMBTnC1CuzbmaMhYy0+ZCUdHQlnhsPYZw8uI0ivmCpCUyp8jIYzCqEtJajgL8PJ/K hLzSPZh4sLRJgyt0LAydywZJmQwATt9scO2Uxx6/lkRXLqmWCRXyZO45px2zOxH5zM+F Fl/Qn4zH9l1qCU7F+y+ByDzLv4KMyxide3wuPWXJ1UHzfNrN9ky74dKQrwKn7F/tCOeO lZUcXXb2Xs5ZkJXBMDXdleN5Khvqt40l0Oy5qByCyiAydMNjguZir8Z/SKP4l8N/PUR+ qT8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=Qr5IKi8WXqsIcGbDz4DO4fc5xGkJJ/vg89LDVU9TkbI=; b=spJY3+0504yGbesg3MJlcKFF+Sc0VrfzaI5tUbQ9bmAwgbSEoT8+fgFxV0bcZ6pf1B bIArXBkCBzDIX+fg393tO4u4dCY4Vu6nXtGKYfp+It4owEWashjzZlmuQsebtxdLfrAG fPz+8mP+hWSJyUg+eDt3ahZniVUwAnyf60pj9Db66fyGpPufEQ2pVbhQm/S/V1LdviSa 6KWHnqfFABPPJfOXKvl/HU19M0Nx7k0uL6fJZg0n+pTfY5U0Ua3K5TIGZofGCKbY8bfd qoH6s4XK8YExxKmBEE6yK5ncqImRvcxw/Jk8gQeGX5aoKogn8+935K8PWwHQepevU99N f3lQ== X-Gm-Message-State: AN3rC/4EQc1aagXDV8e5TteOqtoM58dswBX3fNzR56hdmwBhY5ldNStY CLBIshM/mLoE45Wm X-Received: by 10.223.136.71 with SMTP id e7mr2449568wre.73.1492892997958; Sat, 22 Apr 2017 13:29:57 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id c37sm3074985wra.16.2017.04.22.13.29.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 22 Apr 2017 13:29:56 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Sat, 22 Apr 2017 22:29:39 +0200 Message-Id: <20170422202939.3648-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH] avfilter: add pixscope filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Paul B Mahol --- 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, +};