From patchwork Tue Dec 27 23:17:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgQsWTc2No?= X-Patchwork-Id: 39774 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp3761355pzb; Tue, 27 Dec 2022 15:19:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXttQqIv1tPnd73fh7wnm2mIlDIw4qNk1I7UFqLNWDZGAsb42SlhHLedB8fNDMya0R9GmoWB X-Received: by 2002:a17:906:2349:b0:837:3ddb:7e97 with SMTP id m9-20020a170906234900b008373ddb7e97mr24705050eja.61.1672183176026; Tue, 27 Dec 2022 15:19:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672183176; cv=none; d=google.com; s=arc-20160816; b=LXhshzZJl9CKGmPYOcb6I0s59sfGSPuz8FyK43mDyH4IL2J/cSykogLy/PU4GrQwM3 sHRJEXVs47PRL17Aic29EDqrm2A65f+33piaBCT4FRb1oA/ZKZkYf9itT1QKQsraocPJ 5ZQfP+HKDK3zmlW4gr04KdUiWOz7yFjbampB+PVnneadXn7IPitsP7NmhGX7LAqBw6gm JVXYe/20kxwEEcraiuN8RK+xej2a/HkQfkBReR3Pe0KcfBOJrkCDf5VyI5vqVhO7enib zWF21ue9zetzRzWzHvC0qya6+hBO4SMykeGK9/VKBPqGyKsapzIQH+6YPgO7JfscHrek GRwg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=8KMfBIeGkJV+p5RzdmPFQu2ZnMaJjIeXFDSlrlThGBc=; b=MM5W2KCJO5mIFzPeS2XaBgv04EsCE2rMseoBFcIj/dbFwZ1KVc6huITot+MFoXfh9h bjsaRJ7pGgMBgfY4Byv0F65sC0zjEP0X47gxSxg1NGvyhjcOSj7+leWb1Hs7QFqyk9in 7fR4IE1Cvm6Dh1bWIXolL4gLRGFPTEcD9tRKAJRQ608OUM8J1V2SCQSsbtTvHFSvf1sb bsv0kZyLkIPtYaI51J6cVdR0+K60bay62S3ndt9/iaJg4RXIzrtzpwI+2aPD3gPDLrBp /2TwrQX+Swlyd/670TKKCVtiqoPzLyioby679IT3YjMngGu6F1ZvD+NNSaTdmaKB5jE0 //5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@pkh.me header.s=selector1 header.b=qa2UT8cB; 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=pkh.me Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id q17-20020a170906679100b007c4fcbfaf8esi9205211ejp.954.2022.12.27.15.19.35; Tue, 27 Dec 2022 15:19:36 -0800 (PST) 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=@pkh.me header.s=selector1 header.b=qa2UT8cB; 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=pkh.me Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C1CE368BD0F; Wed, 28 Dec 2022 01:18:36 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from ssq0.pkh.me (laubervilliers-656-1-228-164.w92-154.abo.wanadoo.fr [92.154.28.164]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BA15468BCA6 for ; Wed, 28 Dec 2022 01:18:32 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pkh.me; s=selector1; t=1672183097; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8nwyPurxtq2m1JGNlCh68RfQlDs3V9MH54q6So7iq+Y=; b=qa2UT8cBYaX0E/OdOevP5onaFQKUh/NsV/2Nm5teGsh9AFIawC3rDTPwa8sjtphuzMfYLZ MUVoyJZ8pmCnkfoC0tPoWLeMsIEZCouYckgsUFcQWvagOOFoiyUesTXdQnSoXtvIUUnU6g f/rzLG96kOzaw7NxeVOdP94Ot95h+rY= Received: from localhost (ssq0.pkh.me [local]) by ssq0.pkh.me (OpenSMTPD) with ESMTPA id c99eb23c; Tue, 27 Dec 2022 23:18:17 +0000 (UTC) From: =?utf-8?b?Q2zDqW1lbnQgQsWTc2No?= To: ffmpeg-devel@ffmpeg.org Date: Wed, 28 Dec 2022 00:17:52 +0100 Message-Id: <20221227231814.2520181-11-u@pkh.me> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221227231814.2520181-1-u@pkh.me> References: <20221105152617.1809282-1-u@pkh.me> <20221227231814.2520181-1-u@pkh.me> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 10/32] avfilter/palettegen: move box stats computation to a dedicated function 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 Cc: =?utf-8?b?Q2zDqW1lbnQgQsWTc2No?= Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 5gj5+p30pUe4 --- libavfilter/vf_palettegen.c | 64 ++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/libavfilter/vf_palettegen.c b/libavfilter/vf_palettegen.c index bea3292796..a047c75599 100644 --- a/libavfilter/vf_palettegen.c +++ b/libavfilter/vf_palettegen.c @@ -40,6 +40,8 @@ struct color_ref { /* Store a range of colors */ struct range_box { uint32_t color; // average color + int major_axis; // best axis candidate for cutting the box + uint64_t weight; // sum of all the weights of the colors int64_t variance; // overall variance of the box (how much the colors are spread) int start; // index in PaletteGenContext->refs int len; // number of referenced colors @@ -143,6 +145,35 @@ static av_always_inline int diff(const uint32_t a, const uint32_t b) return dr*dr + dg*dg + db*db; } +static void compute_box_stats(PaletteGenContext *s, struct range_box *box) +{ + int rr, gr, br; + + /* compute the box weight (sum all the weights of the colors in the + * range) and its boundings */ + uint8_t min[3] = {0xff, 0xff, 0xff}; + uint8_t max[3] = {0x00, 0x00, 0x00}; + box->weight = 0; + for (int i = box->start; i < box->start + box->len; i++) { + const struct color_ref *ref = s->refs[i]; + const uint32_t rgb = ref->color; + const uint8_t r = rgb >> 16 & 0xff, g = rgb >> 8 & 0xff, b = rgb & 0xff; + min[0] = FFMIN(r, min[0]), max[0] = FFMAX(r, max[0]); + min[1] = FFMIN(g, min[1]), max[1] = FFMAX(g, max[1]); + min[2] = FFMIN(b, min[2]), max[2] = FFMAX(b, max[2]); + box->weight += ref->count; + } + + /* define the axis to sort by according to the widest range of colors */ + rr = max[0] - min[0]; + gr = max[1] - min[1]; + br = max[2] - min[2]; + box->major_axis = 1; // pick green by default (the color the eye is the most sensitive to) + if (br >= rr && br >= gr) box->major_axis = 2; + if (rr >= gr && rr >= br) box->major_axis = 0; + if (gr >= rr && gr >= br) box->major_axis = 1; // prefer green again +} + /** * Find the next box to split: pick the one with the highest variance */ @@ -324,35 +355,16 @@ static AVFrame *get_palette_frame(AVFilterContext *ctx) s->nb_boxes = 1; while (box && box->len > 1) { - int i, rr, gr, br, longest; - uint64_t median, box_weight = 0; - - /* compute the box weight (sum all the weights of the colors in the - * range) and its boundings */ - uint8_t min[3] = {0xff, 0xff, 0xff}; - uint8_t max[3] = {0x00, 0x00, 0x00}; - for (i = box->start; i < box->start + box->len; i++) { - const struct color_ref *ref = s->refs[i]; - const uint32_t rgb = ref->color; - const uint8_t r = rgb >> 16 & 0xff, g = rgb >> 8 & 0xff, b = rgb & 0xff; - min[0] = FFMIN(r, min[0]), max[0] = FFMAX(r, max[0]); - min[1] = FFMIN(g, min[1]), max[1] = FFMAX(g, max[1]); - min[2] = FFMIN(b, min[2]), max[2] = FFMAX(b, max[2]); - box_weight += ref->count; - } + int i, longest; + uint64_t median, box_weight; - /* define the axis to sort by according to the widest range of colors */ - rr = max[0] - min[0]; - gr = max[1] - min[1]; - br = max[2] - min[2]; - longest = 1; // pick green by default (the color the eye is the most sensitive to) - if (br >= rr && br >= gr) longest = 2; - if (rr >= gr && rr >= br) longest = 0; - if (gr >= rr && gr >= br) longest = 1; // prefer green again + compute_box_stats(s, box); + longest = box->major_axis; + box_weight = box->weight; - ff_dlog(ctx, "box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" ranges:[%2x %2x %2x] sort by %c (already sorted:%c) ", + ff_dlog(ctx, "box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" sort by %c (already sorted:%c) ", box_id, box->start, box->start + box->len - 1, box->len, box_weight, - rr, gr, br, "rgb"[longest], box->sorted_by == longest ? 'y':'n'); + "rgb"[longest], box->sorted_by == longest ? 'y':'n'); /* sort the range by its longest axis if it's not already sorted */ if (box->sorted_by != longest) {