Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: "Clément Bœsch" <u@pkh.me>
To: ffmpeg-devel@ffmpeg.org
Cc: "Clément Bœsch" <u@pkh.me>
Subject: [FFmpeg-devel] [PATCH 13/15] avfilter/palettegen: use variance per-axis instead of the range
Date: Sat,  5 Nov 2022 16:26:15 +0100
Message-ID: <20221105152617.1809282-14-u@pkh.me> (raw)
In-Reply-To: <20221105152617.1809282-1-u@pkh.me>

The split decision is now based on the per-axis variance instead of how
wide they are.
---
 libavfilter/vf_palettegen.c        | 61 ++++++++++++++++--------------
 tests/ref/fate/filter-palettegen-1 |  2 +-
 tests/ref/fate/filter-palettegen-2 |  2 +-
 3 files changed, 35 insertions(+), 30 deletions(-)

diff --git a/libavfilter/vf_palettegen.c b/libavfilter/vf_palettegen.c
index 4c2bcba7f7..2976012512 100644
--- a/libavfilter/vf_palettegen.c
+++ b/libavfilter/vf_palettegen.c
@@ -45,6 +45,7 @@ struct color_ref {
 struct range_box {
     uint32_t color;     // average color
     double variance;    // overall variance of the box (how much the colors are spread)
+    double axis_variance[3]; // axis specific variance
     int start;          // index in PaletteGenContext->refs
     int len;            // number of referenced colors
     int sorted_by;      // whether range of colors is sorted by red (0), green (1) or blue (2)
@@ -136,24 +137,29 @@ static int cmp_color(const void *a, const void *b)
     return FFDIFFSIGN(box1->color , box2->color);
 }
 
-static av_always_inline float diff(const uint32_t a, const uint32_t b)
-{
-    const struct Lab lab0 = ff_srgb_u8_to_oklab(a);
-    const struct Lab lab1 = ff_srgb_u8_to_oklab(b);
-    const float dL = lab0.L - lab1.L;
-    const float da = lab0.a - lab1.a;
-    const float db = lab0.b - lab1.b;
-    return dL*dL + da*da + db*db;
-}
-
 static void compute_box_variance(PaletteGenContext *s, struct range_box *box)
 {
     double variance = 0.0;
 
     for (int i = 0; i < box->len; i++) {
         const struct color_ref *ref = s->refs[box->start + i];
-        variance += diff(ref->color, box->color) * ref->count;
+        const struct Lab lab0 = ff_srgb_u8_to_oklab(ref->color);
+        const struct Lab lab1 = ff_srgb_u8_to_oklab(box->color);
+        const float dL = lab0.L - lab1.L;
+        const float da = lab0.a - lab1.a;
+        const float db = lab0.b - lab1.b;
+
+        variance += (dL*dL + da*da + db*db) * ref->count;
+
+        /*
+         * No need to normalize the per-axis variances since they are compared
+         * only locally within the box and thus share the same weight.
+         */
+        box->axis_variance[0] += dL*dL * ref->count;
+        box->axis_variance[1] += da*da * ref->count;
+        box->axis_variance[2] += db*db * ref->count;
     }
+
     /*
      * The variance is computed as a Mean Squared Error of the distance of the
      * current color to the box color average, with an important difference:
@@ -198,6 +204,7 @@ static int get_next_box_id_to_split(PaletteGenContext *s)
             }
         } else {
             box->variance = -1.0;
+            memset(box->axis_variance, 0, sizeof(box->axis_variance));
         }
     }
     return best_box_id;
@@ -249,6 +256,8 @@ static void split_box(PaletteGenContext *s, struct range_box *box, int n)
     new_box->color = get_avg_color(s->refs, new_box);
     box->variance     = -1.0;
     new_box->variance = -1.0;
+    memset(box->axis_variance, 0, sizeof(box->axis_variance));
+    memset(new_box->axis_variance, 0, sizeof(new_box->axis_variance));
 }
 
 /**
@@ -346,38 +355,34 @@ static AVFrame *get_palette_frame(AVFilterContext *ctx)
     box->sorted_by = -1;
     box->color = get_avg_color(s->refs, box);
     box->variance = -1.0;
+    memset(box->axis_variance, 0, sizeof(box->axis_variance));
+    compute_box_variance(s, box);
     s->nb_boxes = 1;
 
     while (box && box->len > 1) {
         int i, longest;
-        double Lr, ar, br;
+        double Lv, av, bv;
         uint64_t median, box_weight = 0;
 
         /* compute the box weight (sum all the weights of the colors in the
-         * range) and its boundings */
-        float min[3] = {FLT_MAX, FLT_MAX, FLT_MAX};
-        float max[3] = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
+         * range) */
         for (i = box->start; i < box->start + box->len; i++) {
             const struct color_ref *ref = s->refs[i];
-            const struct Lab lab = ref->lab;
-            min[0] = FFMIN(lab.L, min[0]), max[0] = FFMAX(lab.L, max[0]);
-            min[1] = FFMIN(lab.a, min[1]), max[1] = FFMAX(lab.a, max[1]);
-            min[2] = FFMIN(lab.b, min[2]), max[2] = FFMAX(lab.b, max[2]);
             box_weight += ref->count;
         }
 
-        /* define the axis to sort by according to the widest range of colors */
-        Lr = max[0] - min[0];
-        ar = max[1] - min[1];
-        br = max[2] - min[2];
+        /* pick the axis with the biggest variance */
+        Lv = box->axis_variance[0];
+        av = box->axis_variance[1];
+        bv = box->axis_variance[2];
         longest = 0;
-        if (br >= Lr && br >= ar) longest = 2;
-        if (ar >= Lr && ar >= br) longest = 1;
-        if (Lr >= ar && Lr >= br) longest = 0;
+        if (bv >= Lv && bv >= av) longest = 2;
+        if (av >= Lv && av >= bv) longest = 1;
+        if (Lv >= av && Lv >= bv) longest = 0;
 
-        ff_dlog(ctx, "box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" ranges:[%.3f %.3f %.3f] sort by %c (already sorted:%c) ",
+        ff_dlog(ctx, "box #%02X [%6d..%-6d] (%6d) w:%-6"PRIu64" var:[%.3f %.3f %.3f] sort by %c (already sorted:%c) ",
                 box_id, box->start, box->start + box->len - 1, box->len, box_weight,
-                Lr, ar, br, "Lab"[longest], box->sorted_by == longest ? 'y':'n');
+                Lv, av, bv, "Lab"[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) {
diff --git a/tests/ref/fate/filter-palettegen-1 b/tests/ref/fate/filter-palettegen-1
index 7b7ce98b76..35730a659f 100644
--- a/tests/ref/fate/filter-palettegen-1
+++ b/tests/ref/fate/filter-palettegen-1
@@ -3,4 +3,4 @@
 #codec_id 0: rawvideo
 #dimensions 0: 16x16
 #sar 0: 1/1
-0,          0,          0,        1,     1024, 0xf1fb64c1
+0,          0,          0,        1,     1024, 0xd8fd2c22
diff --git a/tests/ref/fate/filter-palettegen-2 b/tests/ref/fate/filter-palettegen-2
index b856a79273..548902fed0 100644
--- a/tests/ref/fate/filter-palettegen-2
+++ b/tests/ref/fate/filter-palettegen-2
@@ -3,4 +3,4 @@
 #codec_id 0: rawvideo
 #dimensions 0: 16x16
 #sar 0: 1/1
-0,          0,          0,        1,     1024, 0xe84a671a
+0,          0,          0,        1,     1024, 0xd1f29072
-- 
2.38.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  parent reply	other threads:[~2022-11-05 15:28 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-05 15:26 [FFmpeg-devel] Rework color quantization in palette{gen,use} Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 01/15] Revert "avfilter/vf_palette(gen|use): support palettes with alpha" Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 02/15] avfilter: add palette utils Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 03/15] avfilter/palette{use, gen}: simplify a few alpha masks Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 04/15] avfilter/paletteuse: switch from u8[4] to u32 for color code Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 05/15] avfilter/paletteuse: name target color arg consistently in colormap functions Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 06/15] avfilter/paletteuse: remove unused alpha split dimension Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 07/15] avfilter/paletteuse: remove redundant alpha condition Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 08/15] avfilter/paletteuse: switch to a perceptual model Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 09/15] avfilter/palettegen: average color in linear space Clément Bœsch
2022-11-05 15:39   ` Paul B Mahol
2022-11-05 18:50     ` Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 10/15] avfilter/palettegen: move box variance computation in a dedicated function Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 11/15] avfilter/palettegen: comment on the unnormalized variance Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 12/15] avfilter/palettegen: base split decision on a perceptual model Clément Bœsch
2022-11-05 19:07   ` Andreas Rheinhardt
2022-11-08 21:09     ` Clément Bœsch
2022-12-27 23:20     ` Clément Bœsch
2022-11-05 15:26 ` Clément Bœsch [this message]
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 14/15] avfilter/palettegen: rename longest to split_axis Clément Bœsch
2022-11-05 15:26 ` [FFmpeg-devel] [PATCH 15/15] avfilter/palette{use, gen}: update Copyright after recent changes Clément Bœsch
2022-11-05 15:44 ` [FFmpeg-devel] Rework color quantization in palette{gen,use} Paul B Mahol
2022-11-05 18:54   ` Clément Bœsch
2022-11-06 13:19     ` Ronald S. Bultje
2022-11-08 21:22       ` Clément Bœsch
2022-11-05 21:52 ` Soft Works
2022-11-06 17:09 ` Michael Niedermayer
2022-11-06 17:30   ` Michael Niedermayer
2022-11-08 21:14     ` Clément Bœsch
2022-12-31 12:11     ` Clément Bœsch
2023-01-02 21:57       ` Michael Niedermayer
2023-01-02 23:05         ` Clément Bœsch
2023-01-03 18:50           ` Michael Niedermayer
2022-11-06 19:46 ` Soft Works
2022-11-08 21:07   ` Clément Bœsch
2022-11-08 22:37     ` Soft Works
2022-12-27 23:31       ` Clément Bœsch
2022-12-27 23:17 ` [FFmpeg-devel] New iteration for the color quantization in palette{gen, use} Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 01/32] avfilter/palettegen: allow a minimum of 2 colors Clément Bœsch
2022-12-28 21:04     ` Tomas Härdin
2022-12-28 21:23       ` Clément Bœsch
2023-01-03 18:59         ` Tomas Härdin
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 02/32] avfilter/palette{gen, use}: revert support palettes with alpha Clément Bœsch
2023-01-03 19:11     ` Paul B Mahol
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 03/32] avfilter/palette{gen, use}: simplify a few alpha masks Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 04/32] avfilter/palette{gen, use}: add palette utils Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 05/32] avfilter/paletteuse: switch from u8[4] to u32 for color code Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 06/32] avfilter/paletteuse: name target color arg consistently in colormap functions Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 07/32] avfilter/paletteuse: remove unused alpha split dimension Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 08/32] avfilter/paletteuse: remove redundant alpha condition Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 09/32] avfilter/paletteuse: switch to a perceptual model Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 10/32] avfilter/palettegen: move box stats computation to a dedicated function Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 11/32] avfilter/palettegen: define the best axis to cut using the squared error Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 12/32] avfilter/palettegen: use box->major_axis without intermediate variable Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 13/32] avfilter/palettegen: always compute the box variance Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 14/32] avfilter/palettegen: rename variance to cut_score Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 15/32] avfilter/palettegen: change cut score from ∑e² to max e² Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 16/32] avfilter/palettegen: compute average color within compute_box_stats() Clément Bœsch
2022-12-27 23:17   ` [FFmpeg-devel] [PATCH v2 17/32] avfilter/palettegen: misc cosmetics Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 18/32] avfilter/palettegen: rename local variable box_weight to weight Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 19/32] avfilter/palettegen: switch to signed arithmetic Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 20/32] avfilter/palettegen: base box split decision on a perceptual model Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 21/32] avfilter/palettegen: add a warning about supporting only sRGB Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 22/32] avfilter/palettegen: make refs order deterministic Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 23/32] avfilter/palettegen: use libc qsort Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 24/32] avfilter/palette{gen, use}: update Copyright after recent changes Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 25/32] avfilter/palette: add lowbias32 hashing Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 26/32] avfilter/palettegen: use lowbias32 for color hashing Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 27/32] avfilter/paletteuse: " Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 28/32] avfilter/paletteuse: switch to recursive method Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 29/32] avfilter/paletteuse: remove alternative search methods Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 30/32] avfilter/paletteuse: remove mean error tool Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 31/32] avfilter/paletteuse: move r, g, b computation in a more local scope Clément Bœsch
2022-12-27 23:18   ` [FFmpeg-devel] [PATCH v2 32/32] avfilter/palette{gen, use}: misc for-loop cosmetics Clément Bœsch
2023-01-03 16:28   ` [FFmpeg-devel] New iteration for the color quantization in palette{gen, use} Clément Bœsch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221105152617.1809282-14-u@pkh.me \
    --to=u@pkh.me \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git