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

Clément Bœsch u at pkh.me
Sat Nov 5 17:26:12 EET 2022


---
 libavfilter/vf_palettegen.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

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;
-- 
2.38.1



More information about the ffmpeg-devel mailing list