From patchwork Fri Apr 28 12:31:23 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: 3509 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.3.129 with SMTP id 123csp223460vsd; Fri, 28 Apr 2017 05:39:16 -0700 (PDT) X-Received: by 10.28.111.213 with SMTP id c82mr5462154wmi.11.1493383156546; Fri, 28 Apr 2017 05:39:16 -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 z4si8102080wmd.120.2017.04.28.05.39.16; Fri, 28 Apr 2017 05:39:16 -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 91A2C6898B1; Fri, 28 Apr 2017 15:39:11 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4E3DE689892 for ; Fri, 28 Apr 2017 15:39:05 +0300 (EEST) Received: by mail-wm0-f66.google.com with SMTP id y10so10701472wmh.0 for ; Fri, 28 Apr 2017 05:39:07 -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=TBfV3Z8bMC9n3ERY+2lZnsnd3vq+WBGyVW9s3j1CsVU=; b=cKoaMLiwrYB38o2IkKlto5SxGgwI3u4CKmEmuTT163L7ZXIGWJhxFXAhPIJn/Pkk/D 5EZUbjvjNLvxAWwqv5NZ/HSGu0SVllTxEXVnqxBJ1rHtJcgG0K/Af5buhbVxDplaCRYV FZ1J0vGJtjNWRoBNIno66xMni0+O4VlyO8iyEJ44VTeetnqcZjqNaliK/qk1RqdcP9m+ KHH2DyPyt9v5j9Rr+/0I8MESEpeZIq0w4H6g+ceomASffTJaLZ46Ud0rtl420AumM0Qv iwcQTStQbG4EjLDw4TE8G79OrgrcyGRTi+Jo59iR6wFFEGN0ZpPPRkRLIP76jC0Kbvc0 ucxA== 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=TBfV3Z8bMC9n3ERY+2lZnsnd3vq+WBGyVW9s3j1CsVU=; b=uQXvSULhmCcJEPIPyKI6AofQ9VAA4L90opY/y57BF+PJj3KBEufHefa027Q3EPq78M j3NPXTMPkxsym4Ki5rMvwcx+SmRCCDZJ++6D3FC/O/2EEJgFW/pI/Q3WsHS8kKMIu4l0 G6u1M7e/0Yu1Qijt/i6VpZl+4cRQ/UXLZsVePZHFTS86z/aUVYtNJ2TgitVVfAgtgGw1 a65OxjRwg1l7xancpjzDHeZouSbWQe68mhfExv+bQr5AkG6vOdF1o34wgiwQgrO32AaZ KiLMmm1a/K5RjIAWCSe4mE7kI5pbIGC8X7bysTGfBmgeH4W4h5fCJOBJz9GZH8KBW0R2 Q3kQ== X-Gm-Message-State: AN3rC/5w4hU+HrNrvI6RRJohfLAOpbA4UDXaY2zsoCWp3FU0CLKv0q+A Z2B9N50IqVZgp2hg X-Received: by 10.28.155.203 with SMTP id d194mr5451599wme.73.1493382706203; Fri, 28 Apr 2017 05:31:46 -0700 (PDT) Received: from localhost.localdomain ([94.250.174.60]) by smtp.gmail.com with ESMTPSA id l45sm4410340wre.2.2017.04.28.05.31.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 28 Apr 2017 05:31:44 -0700 (PDT) From: Paul B Mahol To: ffmpeg-devel@ffmpeg.org Date: Fri, 28 Apr 2017 14:31:23 +0200 Message-Id: <20170428123124.4057-1-onemda@gmail.com> X-Mailer: git-send-email 2.9.3 Subject: [FFmpeg-devel] [PATCH 1/2] 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 | 225 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 220 insertions(+), 7 deletions(-) diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 074c690..e40c6fe 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 c69f79e..0852b54 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/vf_datascope.c b/libavfilter/vf_datascope.c index 01a5d99..5ad4bb8 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,214 @@ 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; + 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) +#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, 40, PFLAGS }, + { "h", "set scope height", POFFSET(h), AV_OPT_TYPE_INT, {.i64=1}, 1, 40, PFLAGS }, + { "o", "set 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) { + 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); + } + + 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 + 220 + 2, text, 0); + for (i = 0; i < s->nb_comps; i++) { + snprintf(text, sizeof(text), "%c %07.1f %05d %05d\n", s->is_rgb ? rgba[i] : yuva[i], average[s->rgba_map[i]], min[s->rgba_map[i]], max[s->rgba_map[i]]); + draw_text(&s->draw, in, s->colors[i], X + 18, Y + 220 + 15 * (i + 1), 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, +};