From patchwork Tue Dec 27 23:17:55 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: 39777 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bc95:b0:ad:ade2:bfd2 with SMTP id fx21csp3761556pzb; Tue, 27 Dec 2022 15:20:00 -0800 (PST) X-Google-Smtp-Source: AMrXdXvgqNkMtD1tlODendMgwByOIvrBPOAY9h4jvyAClLWDcGaqzza6+A8rSPE471PUASl7TAzT X-Received: by 2002:aa7:d448:0:b0:467:b851:6066 with SMTP id q8-20020aa7d448000000b00467b8516066mr26298523edr.6.1672183199489; Tue, 27 Dec 2022 15:19:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672183199; cv=none; d=google.com; s=arc-20160816; b=ajLBeuoKU3T6Shf4OoULsHBi/U3MO7UG+bP91efqfvqhMqtubjqj/+OA6pI2+fihHj fTzsAEoDxGNP+1sQcPNV2hCXTMZHGsb1W8AOHmS4WXb6cab2HcZnTd5nT3ijz0uFj7ff H5D9qPEX13Z9FaTFKPJiWxQpN124Ukn1JkAqUWdYaI9wtYEhvDcSRFmCt6s051NqLkb1 XKaD5YtvQcrDG3ylcCgHBgf35d8yIAMBXapgOmK3fJAmv3Rad8dzqkUtpiyXHP10zGVY A1S+7XBWAYnn2qT9/jyKG7uniJ9eYOC6k73ATkuLtMPoNqMDeeFo3MCsd+a8365EuN3z 1VEQ== 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=YE8SZaTcSLPCpihJP8w7z/Uu4TtWETy+nYzXve0XJFE=; b=0pZC5EPSYi08JqybcLSL6cPHUgrV5Yk1xMgWhJkaJnTuhN0GKhxCMUM4zJ+qJcvkon Jk4BEhwlANAIr7f4CvrrnXswisidBDzIkBo5Xkj3FWneYHmmebME+Zc3II0xvSo3FaN4 i3Cf0QHthk04MXFJslomhLPhsI4HpRZHc9r7c6unjHBGbsTpJpKZlYl73iJgSHtIjJhf BCSj1X5RGiLIFGVuCZfMuYlzvH67URbEYyXEfBUJkSM0D9v2QscVlxiO0acItgNdb2t/ P93MV+4a4QISZ5aZ8DCFTVGAhAo79unnKIym3u4EX2wjBoUvwlGAYopkR3pSZ8E5NVKV Az5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@pkh.me header.s=selector1 header.b=Jwad3Exm; 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 sb25-20020a1709076d9900b007c4f78e6104si11805003ejc.408.2022.12.27.15.19.59; Tue, 27 Dec 2022 15:19:59 -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=Jwad3Exm; 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 4509F68BCC1; Wed, 28 Dec 2022 01:18:39 +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 5716D68BCB9 for ; Wed, 28 Dec 2022 01:18:33 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pkh.me; s=selector1; t=1672183098; 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=aK7EU/6HgPSE+H00gmAjOInHxXg8YH0aBja11/B5Lp0=; b=Jwad3Exm1D3jnHcd54MrlDuLSa3BfGQ3TNpbyiB87bpARc+yyT/i+89C9bWNARc25Exa8+ t3JMlO0qoczA5Wlz5guBuwkxstupNb5/qirCikI9aj5esMc3ioYB2nl+UXVRdBdR99R4A1 jFqip57+3QaYv6lfZmXwFkA4WO9igEU= Received: from localhost (ssq0.pkh.me [local]) by ssq0.pkh.me (OpenSMTPD) with ESMTPA id e8f4c611; Tue, 27 Dec 2022 23:18:18 +0000 (UTC) From: =?utf-8?b?Q2zDqW1lbnQgQsWTc2No?= To: ffmpeg-devel@ffmpeg.org Date: Wed, 28 Dec 2022 00:17:55 +0100 Message-Id: <20221227231814.2520181-14-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 13/32] avfilter/palettegen: always compute the box variance 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: vfR7Kw5TV1Z6 The variance computation is simple enough now (since we can use the axis squared errors) that it doesn't need to have a complex lazy computation logic. --- libavfilter/vf_palettegen.c | 42 ++++++++----------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/libavfilter/vf_palettegen.c b/libavfilter/vf_palettegen.c index aa0c8fdc5b..ca1e02444c 100644 --- a/libavfilter/vf_palettegen.c +++ b/libavfilter/vf_palettegen.c @@ -135,16 +135,6 @@ static int cmp_color(const void *a, const void *b) return FFDIFFSIGN(box1->color , box2->color); } -static av_always_inline int diff(const uint32_t a, const uint32_t b) -{ - const uint8_t c1[] = {a >> 16 & 0xff, a >> 8 & 0xff, a & 0xff}; - const uint8_t c2[] = {b >> 16 & 0xff, b >> 8 & 0xff, b & 0xff}; - const int dr = c1[0] - c2[0]; - const int dg = c1[1] - c2[1]; - const int db = c1[2] - c2[2]; - return dr*dr + dg*dg + db*db; -} - static void compute_box_stats(PaletteGenContext *s, struct range_box *box) { int avg[3]; @@ -180,6 +170,8 @@ static void compute_box_stats(PaletteGenContext *s, struct range_box *box) if (er2[2] >= er2[0] && er2[2] >= er2[1]) box->major_axis = 2; if (er2[0] >= er2[1] && er2[0] >= er2[2]) box->major_axis = 0; if (er2[1] >= er2[0] && er2[1] >= er2[2]) box->major_axis = 1; // prefer green again + + box->variance = er2[0] + er2[1] + er2[2]; } /** @@ -187,7 +179,7 @@ static void compute_box_stats(PaletteGenContext *s, struct range_box *box) */ static int get_next_box_id_to_split(PaletteGenContext *s) { - int box_id, i, best_box_id = -1; + int box_id, best_box_id = -1; int64_t max_variance = -1; if (s->nb_boxes == s->max_colors - s->reserve_transparent) @@ -195,24 +187,9 @@ static int get_next_box_id_to_split(PaletteGenContext *s) for (box_id = 0; box_id < s->nb_boxes; box_id++) { struct range_box *box = &s->boxes[box_id]; - - if (s->boxes[box_id].len >= 2) { - - if (box->variance == -1) { - int64_t variance = 0; - - for (i = 0; i < box->len; i++) { - const struct color_ref *ref = s->refs[box->start + i]; - variance += diff(ref->color, box->color) * ref->count; - } - box->variance = variance; - } - if (box->variance > max_variance) { - best_box_id = box_id; - max_variance = box->variance; - } - } else { - box->variance = -1; + if (s->boxes[box_id].len >= 2 && box->variance > max_variance) { + best_box_id = box_id; + max_variance = box->variance; } } return best_box_id; @@ -261,8 +238,8 @@ static void split_box(PaletteGenContext *s, struct range_box *box, int n) box->color = get_avg_color(s->refs, box); new_box->color = get_avg_color(s->refs, new_box); - box->variance = -1; - new_box->variance = -1; + compute_box_stats(s, box); + compute_box_stats(s, new_box); } /** @@ -359,14 +336,13 @@ static AVFrame *get_palette_frame(AVFilterContext *ctx) box->len = s->nb_refs; box->sorted_by = -1; box->color = get_avg_color(s->refs, box); - box->variance = -1; + compute_box_stats(s, box); s->nb_boxes = 1; while (box && box->len > 1) { int i; uint64_t median, box_weight; - compute_box_stats(s, box); box_weight = box->weight; ff_dlog(ctx, "box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" sort by %c (already sorted:%c) ",