[FFmpeg-devel] [PATCH] vf_paletteuse: fix color cache lookup for Bayer dithering mode.

Rudolf Polzer rpolzer at google.com
Mon Jan 10 20:25:31 EET 2022


To trigger this bug, use `paletteuse=dither=bayer:bayer_scale=0`; you will see
that adjacent pixel lines will use the same dither pattern, instead of being
shifted from each other by 32 units (0x20).

One way to demostrate the bug is:

$ convert -size 64x256 gradient:black-white -rotate 270 grad.png
$ echo 'P2 2 1 255 0 255' > bw.pnm
$ ffmpeg -i grad.png -filter_complex 'movie=bw.pnm,scale=256x1[bw]; [0:v][bw]paletteuse=dither=bayer:bayer_scale=0' gradbw.png

Previously: https://www.rm.cloudns.org/img/uploaded/0bd152c11b9cd99e5945115534b1bdde.png
Now:        https://www.rm.cloudns.org/img/uploaded/89caaa5e36c38bc2c01755b30811f969.png

This was caused by passing inconsistent color vs (a,r,g,b) parameters to
color_get(), and NBITS being 5 meaning actually hitting the same cache node
does happen in this case, but ONLY if bayer_scale is zero.

The fix is passing the correct color value to color_get().

Also added a previous-failing FATE test; image comparison of the first frame:

Previously: https://www.rm.cloudns.org/img/uploaded/d0ff9db8d8a7d8a3b8b88bbe92bf5fed.png
Now:        https://www.rm.cloudns.org/img/uploaded/a72389707e719b5cd1c58916a9e79ca8.png

(on this less synthetic test image, the bug basically causes noise from cache
 hits vs misses)

Tested: FATE passes, which exercises this filter but at the default bayer_scale.
---
 libavfilter/vf_paletteuse.c             |  3 +-
 tests/fate/filter-video.mak             |  3 +
 tests/ref/fate/filter-paletteuse-bayer0 | 76 +++++++++++++++++++++++++
 3 files changed, 81 insertions(+), 1 deletion(-)
 create mode 100644 tests/ref/fate/filter-paletteuse-bayer0

diff --git a/libavfilter/vf_paletteuse.c b/libavfilter/vf_paletteuse.c
index 5b300be5..e57882a6 100644
--- a/libavfilter/vf_paletteuse.c
+++ b/libavfilter/vf_paletteuse.c
@@ -423,7 +423,8 @@ static av_always_inline int set_frame(PaletteUseContext *s, AVFrame *out, AVFram
                 const uint8_t r = av_clip_uint8(r8 + d);
                 const uint8_t g = av_clip_uint8(g8 + d);
                 const uint8_t b = av_clip_uint8(b8 + d);
-                const int color = color_get(s, src[x], a8, r, g, b, search_method);
+                const uint32_t color_new = (unsigned)(a8) << 24 | r << 16 | g << 8 | b;
+                const int color = color_get(s, color_new, a8, r, g, b, search_method);

                 if (color < 0)
                     return color;
diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index 7df79c70..510bb3ff 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -59,6 +59,9 @@ fate-filter-paletteuse-nodither: CMD = framecrc -auto_conversion_filters -i $(TA
 FATE_FILTER_PALETTEUSE += fate-filter-paletteuse-bayer
 fate-filter-paletteuse-bayer: CMD = framecrc -auto_conversion_filters -i $(TARGET_SAMPLES)/filter/anim.mkv -i $(TARGET_SAMPLES)/filter/anim-palette.png -lavfi paletteuse=bayer -pix_fmt bgra

+FATE_FILTER_PALETTEUSE += fate-filter-paletteuse-bayer0
+fate-filter-paletteuse-bayer0: CMD = framecrc -auto_conversion_filters -i $(TARGET_SAMPLES)/filter/anim.mkv -i $(TARGET_SAMPLES)/filter/anim-palette.png -lavfi paletteuse=bayer:bayer_scale=0 -pix_fmt bgra
+
 FATE_FILTER_PALETTEUSE += fate-filter-paletteuse-sierra2_4a
 fate-filter-paletteuse-sierra2_4a: CMD = framecrc -auto_conversion_filters -i $(TARGET_SAMPLES)/filter/anim.mkv -i $(TARGET_SAMPLES)/filter/anim-palette.png -lavfi paletteuse=sierra2_4a:diff_mode=rectangle -pix_fmt bgra

diff --git a/tests/ref/fate/filter-paletteuse-bayer0 b/tests/ref/fate/filter-paletteuse-bayer0
new file mode 100644
index 00000000..85b3832f
--- /dev/null
+++ b/tests/ref/fate/filter-paletteuse-bayer0
@@ -0,0 +1,76 @@
+#tb 0: 1001/24000
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 320x180
+#sar 0: 1/1
+0,          0,          0,        1,   230400, 0xfb6042d2
+0,          1,          1,        1,   230400, 0x1c193c09
+0,          2,          2,        1,   230400, 0x183442f8
+0,          3,          3,        1,   230400, 0xa9634084
+0,          4,          4,        1,   230400, 0x90df3d2f
+0,          5,          5,        1,   230400, 0x59d7389f
+0,          6,          6,        1,   230400, 0xb9bd3a30
+0,          7,          7,        1,   230400, 0x9874ee38
+0,          8,          8,        1,   230400, 0xf661f01f
+0,          9,          9,        1,   230400, 0xacbcedbd
+0,         10,         10,        1,   230400, 0x05f02d59
+0,         11,         11,        1,   230400, 0xc54c2cc8
+0,         12,         12,        1,   230400, 0x19c92d61
+0,         13,         13,        1,   230400, 0x14902fb2
+0,         14,         14,        1,   230400, 0x99b62fb6
+0,         15,         15,        1,   230400, 0x3fc63293
+0,         16,         16,        1,   230400, 0x1eed4b38
+0,         17,         17,        1,   230400, 0xe9d747e0
+0,         18,         18,        1,   230400, 0x9825496f
+0,         19,         19,        1,   230400, 0x94625411
+0,         20,         20,        1,   230400, 0xed7052a3
+0,         21,         21,        1,   230400, 0x80d552dc
+0,         22,         22,        1,   230400, 0x89b360bb
+0,         23,         23,        1,   230400, 0xee9a616a
+0,         24,         24,        1,   230400, 0x30bb5f86
+0,         25,         25,        1,   230400, 0x5ec15eae
+0,         26,         26,        1,   230400, 0x0956633e
+0,         27,         27,        1,   230400, 0x72df62fa
+0,         28,         28,        1,   230400, 0xbafd61d0
+0,         29,         29,        1,   230400, 0x393f81f3
+0,         30,         30,        1,   230400, 0xba6a848c
+0,         31,         31,        1,   230400, 0x502ba0d9
+0,         32,         32,        1,   230400, 0xc81ba71d
+0,         33,         33,        1,   230400, 0x54cdf270
+0,         34,         34,        1,   230400, 0xe951f3e2
+0,         35,         35,        1,   230400, 0xbf15baa1
+0,         36,         36,        1,   230400, 0xbf96bb12
+0,         37,         37,        1,   230400, 0xcdd5cafe
+0,         38,         38,        1,   230400, 0x97b1cbb4
+0,         39,         39,        1,   230400, 0x955ae28f
+0,         40,         40,        1,   230400, 0x6a8dd28f
+0,         41,         41,        1,   230400, 0x8f02d268
+0,         42,         42,        1,   230400, 0x3075d269
+0,         43,         43,        1,   230400, 0x29e8b910
+0,         44,         44,        1,   230400, 0xb35ab888
+0,         45,         45,        1,   230400, 0xc3afb942
+0,         46,         46,        1,   230400, 0xeba8b860
+0,         47,         47,        1,   230400, 0x5de8b7ab
+0,         48,         48,        1,   230400, 0x90233679
+0,         49,         49,        1,   230400, 0x5fbc3abb
+0,         50,         50,        1,   230400, 0xeaa73b87
+0,         51,         51,        1,   230400, 0xbd0a3c4b
+0,         52,         52,        1,   230400, 0xeddb39ba
+0,         53,         53,        1,   230400, 0x269d4131
+0,         54,         54,        1,   230400, 0xae3e3e8c
+0,         55,         55,        1,   230400, 0x65f54056
+0,         56,         56,        1,   230400, 0xf2173c5b
+0,         57,         57,        1,   230400, 0xbd714477
+0,         58,         58,        1,   230400, 0xb60c42ed
+0,         59,         59,        1,   230400, 0x8def43a5
+0,         60,         60,        1,   230400, 0xe6a73f05
+0,         61,         61,        1,   230400, 0xedfe4430
+0,         62,         62,        1,   230400, 0x76c5505a
+0,         63,         63,        1,   230400, 0xf48d4d04
+0,         64,         64,        1,   230400, 0xa49950b5
+0,         65,         65,        1,   230400, 0xc64d51d8
+0,         66,         66,        1,   230400, 0xa08253ec
+0,         67,         67,        1,   230400, 0xd6ef4609
+0,         68,         68,        1,   230400, 0x27a241e7
+0,         69,         69,        1,   230400, 0xe5f74b4a
+0,         70,         70,        1,   230400, 0xb0194751
--
2.34.1.575.g55b058a8bb-goog



More information about the ffmpeg-devel mailing list