From patchwork Wed Jun 12 10:57:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lance Wang X-Patchwork-Id: 13511 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 3A82C449AA3 for ; Wed, 12 Jun 2019 13:59:41 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0F4B7689D68; Wed, 12 Jun 2019 13:59:41 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id ACEC06807E1 for ; Wed, 12 Jun 2019 13:59:34 +0300 (EEST) Received: by mail-pg1-f196.google.com with SMTP id 20so8750466pgr.4 for ; Wed, 12 Jun 2019 03:59:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=p+we9Hz6hfDf7vNPauoZkRLPVeF1khEOVyp/h93g9KI=; b=N0pfH6EvkvTlX9W6HrShXSghtyZagV1/ybO4i0tQwwn9z0KgaJnZ5YcG2Ct3nHBGJy Kpr1BtOjcS8RR9MwKG2vJw4TmrPkPhMPl4aS2fDLkMbghl6uHCyWECIj7a38Puprix/U yEKYlsmDoYAKgHFbqOFxm1Pf5dlEGf7QhmhBdI2TvR1ERO27PFIl3eP7BUNxE7S8wgNM /cZqOsrMg8NP8FULTKF5Cxb3ipvHMO1AEGYyiTRHueIvLs5/3bpWAlx2wSHGKZK/7jB1 qY107G6gdv/Sc1JDIQSIj4imh55b71zkJPl/GFHausj3LlcWBwzqtgtj8FCovtluYdm+ x29w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=p+we9Hz6hfDf7vNPauoZkRLPVeF1khEOVyp/h93g9KI=; b=a1p0YVN1dHpOYHQ2bmS0/FJVWcTZSM9EN29WLjfiscbf+ZJSSpJGM9FyST97dEE+PA tOIPzZDI0WF6ELoXEjcrgieV3ZggqTx3L24OWeap7tsel4BdgVmVUVhs594EQDRiSgRK JP0fkUkusG76glCKXhLvCmPSO1h9yIxX1fDpp78o/CQa93hljz+G0yKsCttyGmijYR2u Fx0YNIBWK4nP5tkhFAlVR8oGkv6CsVtIbrr7MxenBLKq63McU1ZgQsS/wZkSg5KR5yoi slg90GjVUTAVIs232qYMrPqmvv3J56e30u7q38gVfXjvI5Bbh26vcIQ6mCH95DhlTG7s 6nNg== X-Gm-Message-State: APjAAAXLSMb/YPRnJj5PWcqV99zclX/f8q6dPcwU3TXnmufAFVVBWfso rM7ICYp/fZzWzW697W0CIjPKuWZBA2k= X-Google-Smtp-Source: APXvYqx8ZJVANRu74kqulSfj/HvlOa86qjYJAzuNKtKXRA0vW/dQIzbJ32WM4Dy/Vry/822j/qpoog== X-Received: by 2002:a17:90a:ad86:: with SMTP id s6mr7589906pjq.42.1560337172836; Wed, 12 Jun 2019 03:59:32 -0700 (PDT) Received: from localhost.localdomain ([47.90.99.151]) by smtp.gmail.com with ESMTPSA id e6sm15518868pfi.42.2019.06.12.03.59.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 12 Jun 2019 03:59:32 -0700 (PDT) From: lance.lmwang@gmail.com To: ffmpeg-devel@ffmpeg.org Date: Wed, 12 Jun 2019 18:57:30 +0800 Message-Id: <20190612105731.43622-2-lance.lmwang@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190612105731.43622-1-lance.lmwang@gmail.com> References: <20190611060730.21868-1-lance.lmwang@gmail.com> <20190612105731.43622-1-lance.lmwang@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 2/3] vf_find_rect.c: use the optimized sad function to improve the find performance 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 Cc: Limin Wang Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" From: Limin Wang benchmark on x86_64: 6.4 -> 16 with below command: ./ffmpeg -i 1920x1080.mp4 -vf find_rect=./find.tif,cover_rect=./cover.jpg:mode=cover -f null - 6.4 fps -> 16fps Signed-off-by: Limin Wang --- libavfilter/vf_find_rect.c | 53 +++++++++++++++----------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/libavfilter/vf_find_rect.c b/libavfilter/vf_find_rect.c index ee6c3f4b45..ed15885bc2 100644 --- a/libavfilter/vf_find_rect.c +++ b/libavfilter/vf_find_rect.c @@ -26,6 +26,7 @@ #include "libavutil/imgutils.h" #include "libavutil/opt.h" #include "internal.h" +#include "scene_sad.h" #include "lavfutils.h" #include "lswsutils.h" @@ -36,6 +37,8 @@ typedef struct FOCContext { AVClass *class; float threshold; int mipmaps; + ff_scene_sad_fn sad; + int bitdepth; int xmin, ymin, xmax, ymax; char *obj_filename; int last_x, last_y; @@ -103,54 +106,40 @@ static AVFrame *downscale(AVFrame *in) return frame; } -static float compare(const AVFrame *haystack, const AVFrame *obj, int offx, int offy) +static float compare_sad(FOCContext *foc, AVFrame *haystack, AVFrame *obj, int offx, int offy) { - int x,y; - int o_sum_v = 0; - int h_sum_v = 0; - int64_t oo_sum_v = 0; - int64_t hh_sum_v = 0; - int64_t oh_sum_v = 0; - float c; + uint64_t sad = 0; int n = obj->height * obj->width; - const uint8_t *odat = obj ->data[0]; + double mafd; + const uint8_t *odat = obj->data[0]; const uint8_t *hdat = haystack->data[0] + offx + offy * haystack->linesize[0]; - int64_t o_sigma, h_sigma; - - for(y = 0; y < obj->height; y++) { - for(x = 0; x < obj->width; x++) { - int o_v = odat[x]; - int h_v = hdat[x]; - o_sum_v += o_v; - h_sum_v += h_v; - oo_sum_v += o_v * o_v; - hh_sum_v += h_v * h_v; - oh_sum_v += o_v * h_v; - } - odat += obj->linesize[0]; - hdat += haystack->linesize[0]; - } - o_sigma = n*oo_sum_v - o_sum_v*(int64_t)o_sum_v; - h_sigma = n*hh_sum_v - h_sum_v*(int64_t)h_sum_v; - if (o_sigma == 0 || h_sigma == 0) - return 1.0; + foc->sad(hdat, haystack->linesize[0], odat, obj->linesize[0], + obj->width, obj->height, &sad); + emms_c(); + mafd = (double)sad / n / (1ULL << foc->bitdepth); - c = (n*oh_sum_v - o_sum_v*(int64_t)h_sum_v) / (sqrt(o_sigma)*sqrt(h_sigma)); - - return 1 - fabs(c); + return mafd; } static int config_input(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; FOCContext *foc = ctx->priv; + const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format); if (foc->xmax <= 0) foc->xmax = inlink->w - foc->obj_frame->width; if (foc->ymax <= 0) foc->ymax = inlink->h - foc->obj_frame->height; + foc->bitdepth = pix_desc->comp[0].depth; + + foc->sad = ff_scene_sad_get_fn(foc->bitdepth == 8 ? 8 : 16); + if (!foc->sad) + return AVERROR(EINVAL); + + return 0; } @@ -169,7 +158,7 @@ static float search(FOCContext *foc, int pass, int maxpass, int xmin, int xmax, for (y = ymin; y <= ymax; y++) { for (x = xmin; x <= xmax; x++) { - float score = compare(foc->haystack_frame[pass], foc->needle_frame[pass], x, y); + float score = compare_sad(foc, foc->haystack_frame[pass], foc->needle_frame[pass], x, y); av_assert0(score != 0); if (score < best_score) { best_score = score;