diff mbox series

[FFmpeg-devel,10/15] avfilter/palettegen: move box variance computation in a dedicated function

Message ID 20221105152617.1809282-11-u@pkh.me
State New
Headers show
Series [FFmpeg-devel,01/15] Revert "avfilter/vf_palette(gen|use): support palettes with alpha" | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Clément Bœsch Nov. 5, 2022, 3:26 p.m. UTC
---
 libavfilter/vf_palettegen.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
diff mbox series

Patch

diff --git a/libavfilter/vf_palettegen.c b/libavfilter/vf_palettegen.c
index 00bc323d17..2b412cdb55 100644
--- a/libavfilter/vf_palettegen.c
+++ b/libavfilter/vf_palettegen.c
@@ -144,12 +144,23 @@  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_variance(PaletteGenContext *s, struct range_box *box)
+{
+    int64_t variance = 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;
+    }
+    box->variance = variance;
+}
+
 /**
  * Find the next box to split: pick the one with the highest variance
  */
 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)
@@ -159,16 +170,8 @@  static int get_next_box_id_to_split(PaletteGenContext *s)
         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 == -1)
+                compute_box_variance(s, box);
             if (box->variance > max_variance) {
                 best_box_id = box_id;
                 max_variance = box->variance;