From patchwork Fri May 14 08:47:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Fu, Ting" X-Patchwork-Id: 27771 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:b214:0:0:0:0:0 with SMTP id b20csp276745iof; Fri, 14 May 2021 01:57:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6Ed7OW9bBPGsw1bFW7H+QWWrplPKzwML94xEdT5p3DOWyOyZlqRbqhAXmatOHtGAn8KOw X-Received: by 2002:a17:907:7355:: with SMTP id dq21mr47784747ejc.157.1620982622041; Fri, 14 May 2021 01:57:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620982622; cv=none; d=google.com; s=arc-20160816; b=rs/ij6gmrcqILgbHFs0qxsSDLzKjMgP4ezSbY3fzT0eoZgFt8OnM7ZcMEd9RVZUimz aGHpJG4mIlPjhM2xNbEsyzgsgdey47ELvBxt7i5AbO8VH3lQOaB7YlLwZuNRMyhjTgAf 0TJDQ1B5C1d3r1WXID9ikMcyVj1DypG9WyXdedseVpedKmZDRrSYcbM1XN9IufnPioEl xSQKIVHw9jF3rUtyNEbLTpeNoHPhNMSw3M1EJKWtoZ8YCoPyYKul0Jw3YRAu7EeF/Hha oC675u6Ye4zQ5MpvxMJMKH0kedKJKdzI+nn/APcHqUUltzMITEcYV15yzCbcEUdxbn1z wtPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:ironport-sdr :ironport-sdr:delivered-to; bh=07rBTz2ZY5F82JuT1CnIXMw24CsFvc0yRanXnZomBLU=; b=P4TUPG125HjdwfrRdyTIdqjES2idkftzPhUJHtjxDL3lY3D5FYtzKxGUdLidGxymfj PyTaEl+sRpgzfAcgauUOJQDG29X046+OX/LTz1XY8bIeMfGEx5DOBqAOWcz9cUaqY+At Exv3aozamNW4T1Pzngn3jDLMoy8Wq2ZdS+yNumfwbAiswgPo1Ozo7mlx1d3rlZYwEY2E mXREjNkKMsZ9LDhXgVxfbIQV6oXg2hPChtLmQ2AEcScj3TgELvY8XqhyBdDu0REq1OqB BOQeFD8Hr7WMXEmKYEaMQMyOu3oT682owqvGcz6bNsQrFlHM0Pq+cgjWUygzb4Lw0xAz uc9A== ARC-Authentication-Results: i=1; mx.google.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=intel.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id sb14si5669561ejb.322.2021.05.14.01.57.01; Fri, 14 May 2021 01:57:02 -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; 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=intel.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CF3876880D3; Fri, 14 May 2021 11:56:57 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DBD23687F2A for ; Fri, 14 May 2021 11:56:50 +0300 (EEST) IronPort-SDR: KfS2pc2OLX2ZscxD7+m1Mywr0AbSvMLJSxsUlnnh/AhHVBxKBoW2saJpxOTQf3Ia4O4GqiQUa7 wAQ3VYWF2lrA== X-IronPort-AV: E=McAfee;i="6200,9189,9983"; a="199831917" X-IronPort-AV: E=Sophos;i="5.82,299,1613462400"; d="scan'208";a="199831917" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 May 2021 01:56:41 -0700 IronPort-SDR: nCikOa1hga1tXeSpIy5Eeu2D96QPGiII54n5ktsaFSM7yUkGQ2CeeBHllG2p6kmVMQuWXEVuI8 H3KCgkZYW9DQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,299,1613462400"; d="scan'208";a="393561439" Received: from semmer-ubuntu.sh.intel.com ([10.239.159.83]) by orsmga006.jf.intel.com with ESMTP; 14 May 2021 01:56:40 -0700 From: Ting Fu To: ffmpeg-devel@ffmpeg.org Date: Fri, 14 May 2021 16:47:00 +0800 Message-Id: <20210514084702.21273-1-ting.fu@intel.com> X-Mailer: git-send-email 2.17.1 Subject: [FFmpeg-devel] [PATCH 1/3] lavfi/drawbox: refine code X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: JtdD+0fnGi9r Extract common code of filter_frame() and drawgrid_filter_frame() to draw_region(). Signed-off-by: Ting Fu --- libavfilter/vf_drawbox.c | 160 ++++++++++++++------------------------- 1 file changed, 58 insertions(+), 102 deletions(-) diff --git a/libavfilter/vf_drawbox.c b/libavfilter/vf_drawbox.c index 2794fc2520..95e26191bd 100644 --- a/libavfilter/vf_drawbox.c +++ b/libavfilter/vf_drawbox.c @@ -85,6 +85,61 @@ typedef struct DrawBoxContext { static const int NUM_EXPR_EVALS = 5; +typedef int (*PixelBelongsToRegion)(DrawBoxContext *s, int x, int y); + +#define ASSIGN_THREE_CHANNELS \ + row[0] = frame->data[0] + y * frame->linesize[0]; \ + row[1] = frame->data[1] + (y >> ctx->vsub) * frame->linesize[1]; \ + row[2] = frame->data[2] + (y >> ctx->vsub) * frame->linesize[2]; + +#define ASSIGN_FOUR_CHANNELS \ + ASSIGN_THREE_CHANNELS \ + row[3] = frame->data[3] + y * frame->linesize[3]; + +static void draw_region(AVFrame *frame, DrawBoxContext *ctx, int left, int top, int right, int down, + PixelBelongsToRegion pixel_belongs_to_region) +{ + unsigned char *row[4]; + int x, y; + if (ctx->have_alpha && ctx->replace) { + for (y = top; y < down; y++) { + ASSIGN_FOUR_CHANNELS + if (ctx->invert_color) { + for (x = left; x < right; x++) + if (pixel_belongs_to_region(ctx, x, y)) + row[0][x] = 0xff - row[0][x]; + } else { + for (x = left; x < right; x++) { + if (pixel_belongs_to_region(ctx, x, y)) { + row[0][x ] = ctx->yuv_color[Y]; + row[1][x >> ctx->hsub] = ctx->yuv_color[U]; + row[2][x >> ctx->hsub] = ctx->yuv_color[V]; + row[3][x ] = ctx->yuv_color[A]; + } + } + } + } + } else { + for (y = top; y < down; y++) { + ASSIGN_THREE_CHANNELS + if (ctx->invert_color) { + if (pixel_belongs_to_region(ctx, x, y)) + row[0][x] = 0xff - row[0][x]; + } else { + for (x = left; x < right; x++) { + double alpha = (double)ctx->yuv_color[A] / 255; + + if (pixel_belongs_to_region(ctx, x, y)) { + row[0][x ] = (1 - alpha) * row[0][x ] + alpha * ctx->yuv_color[Y]; + row[1][x >> ctx->hsub] = (1 - alpha) * row[1][x >> ctx->hsub] + alpha * ctx->yuv_color[U]; + row[2][x >> ctx->hsub] = (1 - alpha) * row[2][x >> ctx->hsub] + alpha * ctx->yuv_color[V]; + } + } + } + } + } +} + static av_cold int init(AVFilterContext *ctx) { DrawBoxContext *s = ctx->priv; @@ -217,58 +272,9 @@ static av_pure av_always_inline int pixel_belongs_to_box(DrawBoxContext *s, int static int filter_frame(AVFilterLink *inlink, AVFrame *frame) { DrawBoxContext *s = inlink->dst->priv; - int plane, x, y, xb = s->x, yb = s->y; - unsigned char *row[4]; - - if (s->have_alpha && s->replace) { - for (y = FFMAX(yb, 0); y < frame->height && y < (yb + s->h); y++) { - row[0] = frame->data[0] + y * frame->linesize[0]; - row[3] = frame->data[3] + y * frame->linesize[3]; - - for (plane = 1; plane < 3; plane++) - row[plane] = frame->data[plane] + - frame->linesize[plane] * (y >> s->vsub); - - if (s->invert_color) { - for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) - if (pixel_belongs_to_box(s, x, y)) - row[0][x] = 0xff - row[0][x]; - } else { - for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) { - if (pixel_belongs_to_box(s, x, y)) { - row[0][x ] = s->yuv_color[Y]; - row[1][x >> s->hsub] = s->yuv_color[U]; - row[2][x >> s->hsub] = s->yuv_color[V]; - row[3][x ] = s->yuv_color[A]; - } - } - } - } - } else { - for (y = FFMAX(yb, 0); y < frame->height && y < (yb + s->h); y++) { - row[0] = frame->data[0] + y * frame->linesize[0]; - for (plane = 1; plane < 3; plane++) - row[plane] = frame->data[plane] + - frame->linesize[plane] * (y >> s->vsub); - - if (s->invert_color) { - for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) - if (pixel_belongs_to_box(s, x, y)) - row[0][x] = 0xff - row[0][x]; - } else { - for (x = FFMAX(xb, 0); x < xb + s->w && x < frame->width; x++) { - double alpha = (double)s->yuv_color[A] / 255; - - if (pixel_belongs_to_box(s, x, y)) { - row[0][x ] = (1 - alpha) * row[0][x ] + alpha * s->yuv_color[Y]; - row[1][x >> s->hsub] = (1 - alpha) * row[1][x >> s->hsub] + alpha * s->yuv_color[U]; - row[2][x >> s->hsub] = (1 - alpha) * row[2][x >> s->hsub] + alpha * s->yuv_color[V]; - } - } - } - } - } + draw_region(frame, s, FFMAX(s->x, 0), FFMAX(s->y, 0), FFMIN(s->x + s->w, frame->width), + FFMIN(s->y + s->h, frame->height), pixel_belongs_to_box); return ff_filter_frame(inlink->dst->outputs[0], frame); } @@ -389,58 +395,8 @@ static av_pure av_always_inline int pixel_belongs_to_grid(DrawBoxContext *drawgr static int drawgrid_filter_frame(AVFilterLink *inlink, AVFrame *frame) { DrawBoxContext *drawgrid = inlink->dst->priv; - int plane, x, y; - uint8_t *row[4]; - - if (drawgrid->have_alpha && drawgrid->replace) { - for (y = 0; y < frame->height; y++) { - row[0] = frame->data[0] + y * frame->linesize[0]; - row[3] = frame->data[3] + y * frame->linesize[3]; - - for (plane = 1; plane < 3; plane++) - row[plane] = frame->data[plane] + - frame->linesize[plane] * (y >> drawgrid->vsub); - - if (drawgrid->invert_color) { - for (x = 0; x < frame->width; x++) - if (pixel_belongs_to_grid(drawgrid, x, y)) - row[0][x] = 0xff - row[0][x]; - } else { - for (x = 0; x < frame->width; x++) { - if (pixel_belongs_to_grid(drawgrid, x, y)) { - row[0][x ] = drawgrid->yuv_color[Y]; - row[1][x >> drawgrid->hsub] = drawgrid->yuv_color[U]; - row[2][x >> drawgrid->hsub] = drawgrid->yuv_color[V]; - row[3][x ] = drawgrid->yuv_color[A]; - } - } - } - } - } else { - for (y = 0; y < frame->height; y++) { - row[0] = frame->data[0] + y * frame->linesize[0]; - for (plane = 1; plane < 3; plane++) - row[plane] = frame->data[plane] + - frame->linesize[plane] * (y >> drawgrid->vsub); - - if (drawgrid->invert_color) { - for (x = 0; x < frame->width; x++) - if (pixel_belongs_to_grid(drawgrid, x, y)) - row[0][x] = 0xff - row[0][x]; - } else { - for (x = 0; x < frame->width; x++) { - double alpha = (double)drawgrid->yuv_color[A] / 255; - - if (pixel_belongs_to_grid(drawgrid, x, y)) { - row[0][x ] = (1 - alpha) * row[0][x ] + alpha * drawgrid->yuv_color[Y]; - row[1][x >> drawgrid->hsub] = (1 - alpha) * row[1][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[U]; - row[2][x >> drawgrid->hsub] = (1 - alpha) * row[2][x >> drawgrid->hsub] + alpha * drawgrid->yuv_color[V]; - } - } - } - } - } + draw_region(frame, drawgrid, 0, 0, frame->width, frame->height, pixel_belongs_to_grid); return ff_filter_frame(inlink->dst->outputs[0], frame); }