diff mbox series

[FFmpeg-devel,11/15] avfilter/palettegen: comment on the unnormalized variance

Message ID 20221105152617.1809282-12-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 | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
diff mbox series

Patch

diff --git a/libavfilter/vf_palettegen.c b/libavfilter/vf_palettegen.c
index 2b412cdb55..b8e4463539 100644
--- a/libavfilter/vf_palettegen.c
+++ b/libavfilter/vf_palettegen.c
@@ -152,6 +152,24 @@  static void compute_box_variance(PaletteGenContext *s, struct range_box *box)
         const struct color_ref *ref = s->refs[box->start + i];
         variance += diff(ref->color, box->color) * 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:
+     * the final sum is not normalized using the total weight of the box (the
+     * sum of the ref->count).
+     *
+     * One may expect that in order to compare the variance of the boxes
+     * between each others a normalization makes sense. Unfortunately, the
+     * normalization has the side effect of taking the "size" of the box out of
+     * the equation. In practice, this has a tendency to cause dramatic banding
+     * effects (in particular without dithering). Typically a scene where the
+     * sky is omnipresent may get much less colors assigned than its
+     * surroundings.
+     *
+     * Not normalizing causes a bias towards boxes with many colors, but that's
+     * exactly what we want. The downside is that vivid/accent colors found in
+     * small quantities tend to disappear.
+     */
     box->variance = variance;
 }