From patchwork Mon Jan 10 18:25:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rudolf Polzer X-Patchwork-Id: 33191 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp2978553iog; Mon, 10 Jan 2022 10:25:54 -0800 (PST) X-Google-Smtp-Source: ABdhPJxURO3Uqa86bL+h3O1NA9kJwdov8mZs0Dr9igFsuC1xEnBgr+2JP6AHItwTwcIHGy/yaAcL X-Received: by 2002:a17:906:9b92:: with SMTP id dd18mr767434ejc.290.1641839154532; Mon, 10 Jan 2022 10:25:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1641839154; cv=none; d=google.com; s=arc-20160816; b=L8Ic9MSYQUIvLX4PWLFQjZi9M7JqARdFadGbN0ehljHHhlbfViVBeXPQ7200bhr8zg ZxqaSm9l02IMbLMilQ21PegZW9DoIOLv7h5LYaaZ5veb/tBIzhdvygjSg3cfCXeGLqk7 NImfYTNPzknivNkP3Yt7CJDgyOR50ebJi/Snb66QxrEPBobEpixBjPesON2K+IxmPHsi KE93cDK1/n9m/MV1VH0asrAoTd+cwR+eycEZQtM7RXJjFv+TAeiql6IQ62RTBgxYuzKA NWoUudMdSe7fk0KDQqMmx2xF9yPRix5diwuJh5pBRmlNaTUDqZEO8CCXsTrFp6rZUZbh 4LRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:to:from:mime-version:message-id:date :dkim-signature:delivered-to; bh=mYGMxKU5glNYkY6IMAZ3YfeOTNm1ufqvutP18egrA90=; b=RnEGFl0k1DKOg3LUVWKIEkoltQAp3iL+9PGHUCp4U4GI2l6uWqjvZfbeRMUqr90lhG IeYbSzli62agkbgKEgLgTbUmgwNE/zJEaEJyNQRYT7zhJbMe7xuy4apwsoPRa2zEd3W/ fZBUnKJmpHWDlnfvbjxILUoMVrVLQxUAJ5UrRgedgJmnXUAfibH4vvaKWPbL1itJnWGl 0lTtgLm/auMTyfg1g/MnLg+Mo8nMGmR7IFCY48FjYwfCktvPtMIUwotCF16ba2R+xQQ9 1mv/XBBuH1puJoaGEKttlG0nbDTt1XvS0S5TiNtpQsQQrOWasuoUE8PLqoQ/tOaqbaz3 b9rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20210112 header.b=CkBielb8; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id x13si930686ejb.121.2022.01.10.10.25.54; Mon, 10 Jan 2022 10:25:54 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@google.com header.s=20210112 header.b=CkBielb8; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7351D68AE34; Mon, 10 Jan 2022 20:25:51 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BC68D6897E7 for ; Mon, 10 Jan 2022 20:25:44 +0200 (EET) Received: by mail-yb1-f202.google.com with SMTP id v48-20020a25abb3000000b006113ce63ed8so4320123ybi.22 for ; Mon, 10 Jan 2022 10:25:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:message-id:mime-version:subject:from:to:cc; bh=MgC5j11fnnrDyWKpqJUujZRlDVLJT5vuLzJdeZRkbhA=; b=CkBielb8cLYRXW4KFeZ5kXPEBNBelrusCBuKPCfzs++4YAXeC3GqOPRTICGrnEF/ka BZAKPiUZk4JtgNMpiX1LqSTNMhAmklqLVEPThgvdlsFOkEXdQU0uwmn6tvLTA7JPxOyH tnDIQAe4S0JIKdHnAuUNJfi7nnUMNDwjb/6m3Iv4ultMOfUDBDM48KwBtK1qaZD7+3Qo F5XkzKaKp+2F4kBrPFn+GITVJPYLEg2Y9H4D78r15XCIS6CXg2C53ey0+Z5pxviuZ0n0 nk0VeJhnHV/5zOaZPcJOIeTy6hxptB12l93X7o8fWC/kEnElP+dhrsNHboBCjEf2z+q9 r8lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=MgC5j11fnnrDyWKpqJUujZRlDVLJT5vuLzJdeZRkbhA=; b=pUGnknsEk+ojMUtOnY/K+sbQ9bYCkY6f+tiaqI1/HMZjlJwHqT8AUoio7Esi5M6OQj DOs7lfxXImi9EyRR85TqHmZXAnhTrTdHWse79PfwY+iQZMc01QykmXy2OfOBaVzW/vpC zX6fo4IEU7yTiiBtqAMX+BmqI42sZLvzIUd1FtvxsQ4F4CDxfIVmJv9aeL0+N+7bpMwM leuP4YNOhxEgXRxegbtLUHP9dgMwzeBX7yYqvzyzBEzACS1TFfcrhpC4IjvXZLW+qXUz Eea77dzISbQsC6hjmXQQFJx+bhwjOnqXmiQwFEouxPizn6accE0evkwDKfHzzLBZD4JO Bb8Q== X-Gm-Message-State: AOAM532y4zsD0pD127jDuuAbpw+4u8wDLxlKcf7ak5NHCasQhojIL4gr 7noqM5VrI1pnInlo9ni+2uFNXrn+9rtFmE8ENrZoInPs8nxgE7pj0BGPuhe9RxrMGgAkStq5kFm jDNFHSzHbxzN0XGOw1YzsEzUCaOF7dKwrDjoQDryFEoF5mUx2hVO2GuhemiF6t4WqKA== X-Received: from heisseluft.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:1749]) (user=rpolzer job=sendgmr) by 2002:a25:8b85:: with SMTP id j5mr1171945ybl.698.1641839142945; Mon, 10 Jan 2022 10:25:42 -0800 (PST) Date: Mon, 10 Jan 2022 10:25:31 -0800 Message-Id: <20220110182531.493081-1-rpolzer@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog From: Rudolf Polzer To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH] vf_paletteuse: fix color cache lookup for Bayer dithering mode. X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Rudolf Polzer Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 7rdfPulS5KwP 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 -- 2.34.1.575.g55b058a8bb-goog 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