PR #25968 opened by JonRC URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25968 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25968.patch
# Summary of changes Fixes #25911. On x86, 4:2:0 YUV to bgra, bgr24, rgb555 or rgb565 with `accurate_rnd` goes through the MMXEXT `*_X_ar` writers, and they turn neutral grey slightly magenta: green comes out 2 codes below red and blue. Two separate problems add up to that, one per commit: - `ff_updateMMXDitherTables` packs each pair of vertical coefficients for `pmaddwd` as `c0 + c1 * 65536`. When `c0` is negative the addition borrows from the upper half, so `pmaddwd` uses `c1 - 1` and the filter sum comes out short. Cb = Cr = 128 gives 1023 instead of 1024 (8 * 128). The non-accurate branch right below already casts `c0` to `uint16_t`; the accurate one now does the same. - `YSCALEYUV2PACKEDX_ACCURATE_UV` adds VROUNDER (4) to U and V. Only the YUYV writer needs that, for its `>> 3`. The RGB code uses U and V unshifted, so neutral chroma reaches `pmulhw` as +4, and with the negative green coefficients each green term rounds down to -1. The rounder now sits in `yuv2yuyv422_X_ar`, right before its shift, so that writer gives the same output as with the old macro. Either change alone still leaves grey tinted (table below). Only the `*_X_ar` writers are affected. The non-accurate MMX writers, the 1-tap and 2-tap paths and anything with `bitexact` give the same bytes as before, so no existing FATE reference changes. ## Before and after Built on x86-64 Linux with nasm and inline asm (`--enable-gpl --disable-doc --disable-ffplay`), from master 23c4540053 and from this branch. "Commit 1 only" is the first commit of this PR on its own, "rounder only" is the second change without the first. ``` ffmpeg -f lavfi -i "color=c=black:s=64x64:d=0.04,format=yuv420p,geq=lum=235:cb=128:cr=128,setparams=range=tv:colorspace=bt709" \ -vf "scale=flags=accurate_rnd,format=bgr24" -frames:v 1 -f rawvideo - | od -An -tu1 -j 6240 -N3 ``` Before: ``` 255 253 255 ``` After: ``` 255 255 255 ``` Flat grey, Cb = Cr = 128, bgr24 with `accurate_rnd`, centre pixel, B,G,R. Exact is (Y-16)*255/219. bgra gives the same B,G,R with A = 255 in every case. | Y | 16 | 64 | 96 | 128 | 180 | 235 | |---|---|---|---|---|---|---| | exact | 0 | 55.89 | 93.15 | 130.41 | 190.96 | 255 | | before | 0,0,0 | 56,54,56 | 93,91,93 | 130,128,130 | 191,189,191 | 255,253,255 | | commit 1 only | 1,0,0 | 57,54,56 | 94,91,93 | 131,128,130 | 192,189,191 | 255,253,255 | | rounder only | 0,0,0 | 55,56,55 | 92,93,92 | 129,130,129 | 190,191,190 | 254,255,254 | | after | 0,0,0 | 56,56,56 | 93,93,93 | 130,130,130 | 191,191,191 | 255,255,255 | The two FATE graphs added here (grey ramp Y = 16..235, see Tests), counted as pixels where |G-R| or |G-B| is not zero: | | bgra, 220x16 | bgr24, 220x16 to 220x12 | |---|---|---| | before | 3066 / 3068 of 3520, max 3 | 2190 / 2191 of 2640, max 3 | | commit 1 only | 3066 / 3080, max 3 | 2190 / 2200, max 3 | | rounder only | 2628 / 2628, max 1 | 1971 / 1971, max 1 | | after | 0 / 0 | 0 / 0 | yuyv422 with `accurate_rnd` from testsrc2 (320x240 yuv420p), MMXEXT output compared with the same build's C output (`-cpuflags 0`). Commit 1 is what changes this; commit 2 leaves it alone: | | same size | scaled to 320x180 | |---|---|---| | before | 258 of 153600 bytes differ, max 1 | 803 of 115200 bytes differ, max 1 | | after | 0 differ | 0 differ | Colours, not only grey: 256 flat 32x32 blocks with mixed Y, Cb and Cr (512x512 yuv420p to bgra, `accurate_rnd`, same size), block centres compared with exact BT.709 limited range. Mean signed error / mean absolute error per channel: | | R | G | B | |---|---|---|---| | before | +0.162 / 0.246 | -1.037 / 1.037 | +0.212 / 0.263 | | after | -0.268 / 0.312 | -0.791 / 0.796 | -0.230 / 0.242 | Green improves the most. R and B now lean slightly low instead of slightly high, because the chroma products are floored without the +4 in front of them. The total absolute error goes from 1.546 to 1.350. Not changed: with `bitexact` or `-cpuflags 0` the C writer gives 253,253,253 for Y = 235. That is the C table offset in #25681, a separate issue. The non-accurate MMX writers also show a green offset when scaling vertically (default flags, 64x64 to 64x48 gives 255,253,255); there `pmulhw` truncates each tap on its own, which this PR does not touch. ## Tests New FATE tests `fate-sws-grey-bgra-accurate-rnd` and `fate-sws-grey-bgr24-accurate-rnd-vscale`. Each converts a yuv420p grey ramp through the `accurate_rnd` packed RGB path and outputs |G-R| stacked over |G-B| as gray, which has to be all zero. The reference is therefore the same on every CPU, and platforms that use the C writer pass as well. Run against each build with the same `make` targets and `TARGET_PATH` pointing at that build's `ffmpeg`: - master 23c4540053: both fail - commit 1 only: both fail - rounder only: both fail - this branch: both pass Failure on master: ``` TEST sws-grey-bgra-accurate-rnd --- ./tests/ref/fate/sws-grey-bgra-accurate-rnd +++ tests/data/fate/sws-grey-bgra-accurate-rnd @@ -3,4 +3,4 @@ #codec_id 0: rawvideo #dimensions 0: 220x32 #sar 0: 1/1 -0, 0, 0, 1, 7040, 0x00000000 +0, 0, 0, 1, 7040, 0xa36b3182 Test sws-grey-bgra-accurate-rnd failed. TEST sws-grey-bgr24-accurate-rnd-vscale --- ./tests/ref/fate/sws-grey-bgr24-accurate-rnd-vscale +++ tests/data/fate/sws-grey-bgr24-accurate-rnd-vscale @@ -3,4 +3,4 @@ #codec_id 0: rawvideo #dimensions 0: 220x24 #sar 0: 3/4 -0, 0, 0, 1, 5280, 0x00000000 +0, 0, 0, 1, 5280, 0xeda822ff Test sws-grey-bgr24-accurate-rnd-vscale failed. ``` Also run on this branch, all passing: - `make fate-libswscale`: all 42 `sws-*` tests, the two new ones included (the two `sws-slice-*` ones with their samples from fate-suite) - the swscale checkasm group: `fate-checkasm-sw_gbrp`, `sw_ops`, `sw_range_convert`, `sw_rgb`, `sw_scale`, `sw_xyz2rgb`, `sw_yuv2rgb`, `sw_yuv2yuv` - the scale filter tests: `fate-filter-pixfmts-scale`, `filter-scale200`, `filter-scale500`, `filter-scale-premultiply`, `filter-scalechroma`, `filter-scalechroma-downscale`, `filter-scalechroma-yuyv422`, `filter-crop_scale`, `filter-crop_scale_vflip`, `filter-scale-fast-bilinear-wide-edge`, `filter-scale-fast-bilinear-wide-wrap` - `tools/check_commit_msg.sh master..HEAD`: 2/2 OK checkasm does not reach these writers: `check_yuv2packedX` in tests/checkasm/sw_rgb.c creates its context with `SWS_ACCURATE_RND | SWS_BITEXACT`, and `bitexact` turns the `*_X_ar` writers off. The RGB ones are also not bit-exact with C (the #25681 table offset), so a checkasm comparison would not fit them. >From 59fb8c9a3618de71b20b6af310a3702bf9b83c00 Mon Sep 17 00:00:00 2001 From: JonRC <[email protected]> Date: Wed, 11 Nov 2026 07:33:22 -0300 Subject: [PATCH 1/2] swscale/x86/swscale: fix accurate_rnd vertical coefficient pairs with a negative first tap With SWS_ACCURATE_RND the MMX vertical filter packs two 16-bit coefficients into one 32-bit word for pmaddwd as c0 + c1 * 65536. When c0 is negative, the addition borrows from the upper half and pmaddwd sees c1 - 1. Every pair whose first tap is negative then loses one unit of the 4096 sum, so the filtered luma and chroma come out slightly low, and flat input is not kept flat: Cb = Cr = 128 gives 1023 instead of 1024 (8 * 128). Cast c0 to uint16_t, as the non-accurate branch below already does. This is used only by the *_X_ar MMXEXT writers (rgb32, bgr24, rgb555, rgb565, yuyv422), which are never used with bitexact, so no FATE reference changes. yuv420p to yuyv422 with accurate_rnd on testsrc2, same size and scaled to 320x180, now matches the C output byte for byte; before it differed in 258 and 803 bytes. Signed-off-by: JonRC <[email protected]> --- libswscale/x86/swscale.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libswscale/x86/swscale.c b/libswscale/x86/swscale.c index 013d7374d73..f78851107ca 100644 --- a/libswscale/x86/swscale.c +++ b/libswscale/x86/swscale.c @@ -135,7 +135,7 @@ void ff_updateMMXDitherTables(SwsInternal *c, int dstY) *(const void**)&lumMmxFilter[s*i ]= lumSrcPtr[i ]; *(const void**)&lumMmxFilter[s*i+APCK_PTR2/4 ]= lumSrcPtr[i+(vLumFilterSize>1)]; lumMmxFilter[s*i+APCK_COEF/4 ]= - lumMmxFilter[s*i+APCK_COEF/4+1]= vLumFilter[dstY*vLumFilterSize + i ] + lumMmxFilter[s*i+APCK_COEF/4+1]= (uint16_t)vLumFilter[dstY*vLumFilterSize + i ] + (vLumFilterSize>1 ? vLumFilter[dstY*vLumFilterSize + i + 1] * (1 << 16) : 0); if (CONFIG_SWSCALE_ALPHA && hasAlpha) { *(const void**)&alpMmxFilter[s*i ]= alpSrcPtr[i ]; @@ -148,7 +148,7 @@ void ff_updateMMXDitherTables(SwsInternal *c, int dstY) *(const void**)&chrMmxFilter[s*i ]= chrUSrcPtr[i ]; *(const void**)&chrMmxFilter[s*i+APCK_PTR2/4 ]= chrUSrcPtr[i+(vChrFilterSize>1)]; chrMmxFilter[s*i+APCK_COEF/4 ]= - chrMmxFilter[s*i+APCK_COEF/4+1]= vChrFilter[chrDstY*vChrFilterSize + i ] + chrMmxFilter[s*i+APCK_COEF/4+1]= (uint16_t)vChrFilter[chrDstY*vChrFilterSize + i ] + (vChrFilterSize>1 ? vChrFilter[chrDstY*vChrFilterSize + i + 1] * (1 << 16) : 0); } } else { -- 2.54.0 >From 77b8d4b71ed9c71966f19eda43b0353bad9a047c Mon Sep 17 00:00:00 2001 From: JonRC <[email protected]> Date: Wed, 11 Nov 2026 07:33:22 -0300 Subject: [PATCH 2/2] swscale/x86/swscale_template: keep the vertical rounder out of accurate_rnd RGB chroma YSCALEYUV2PACKEDX_ACCURATE_UV adds VROUNDER (4) to U and V. That rounder is meant for a later >> 3, which only the YUYV writer does. YSCALEYUV2RGBX uses U and V unshifted and only subtracts U_OFFSET/V_OFFSET (8 * 128), so neutral chroma reaches the coefficient multiplies as +4 instead of 0. pmulhw rounds down, and with the negative green coefficients each green term becomes -1, so green ends up 2 codes below red and blue and neutral grey turns slightly magenta. Add the rounder in yuv2yuyv422_X_ar instead, right before its shift, which keeps that writer's output unchanged. Y=235, Cb=Cr=128 to bgr24 with accurate_rnd gave B,G,R 255,253,255 and now gives 255,255,255. Add two FATE tests that convert neutral grey through the accurate_rnd packed RGB writers and check that |G-R| and |G-B| are zero, so the result is the same on every CPU. They fail on x86 without this commit or without the previous one. Fixes: #25911 Signed-off-by: JonRC <[email protected]> --- libswscale/x86/swscale_template.c | 5 ++--- tests/fate/libswscale.mak | 9 +++++++++ tests/ref/fate/sws-grey-bgr24-accurate-rnd-vscale | 6 ++++++ tests/ref/fate/sws-grey-bgra-accurate-rnd | 6 ++++++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 tests/ref/fate/sws-grey-bgr24-accurate-rnd-vscale create mode 100644 tests/ref/fate/sws-grey-bgra-accurate-rnd diff --git a/libswscale/x86/swscale_template.c b/libswscale/x86/swscale_template.c index 85ac7b1689c..713b579d64d 100644 --- a/libswscale/x86/swscale_template.c +++ b/libswscale/x86/swscale_template.c @@ -130,11 +130,8 @@ "psrad $16, %%mm5 \n\t"\ "psrad $16, %%mm6 \n\t"\ "psrad $16, %%mm7 \n\t"\ - "movq "VROUNDER_OFFSET"(%0), %%mm0 \n\t"\ "packssdw %%mm5, %%mm4 \n\t"\ "packssdw %%mm7, %%mm6 \n\t"\ - "paddw %%mm0, %%mm4 \n\t"\ - "paddw %%mm0, %%mm6 \n\t"\ "movq %%mm4, "U_TEMP"(%0) \n\t"\ "movq %%mm6, "V_TEMP"(%0) \n\t"\ @@ -616,6 +613,8 @@ static void RENAME(yuv2yuyv422_X_ar)(SwsInternal *c, const int16_t *lumFilter, YSCALEYUV2PACKEDX_ACCURATE /* mm2=B, %%mm4=G, %%mm5=R, %%mm7=0 */ + "paddw "VROUNDER_OFFSET"(%0), %%mm3 \n\t" /* round U and V for the >> 3 */ + "paddw "VROUNDER_OFFSET"(%0), %%mm4 \n\t" "psraw $3, %%mm3 \n\t" "psraw $3, %%mm4 \n\t" "psraw $3, %%mm1 \n\t" diff --git a/tests/fate/libswscale.mak b/tests/fate/libswscale.mak index 7e6a6712cb2..008483f4067 100644 --- a/tests/fate/libswscale.mak +++ b/tests/fate/libswscale.mak @@ -66,6 +66,15 @@ $(FATE_SWS_YUV2RGB_ODD): DST_FMT = $(word 6,$(subst -, ,$(@))) $(FATE_SWS_YUV2RGB_ODD): CMD = framecrc -lavfi testsrc2=s=40x8:r=1,format=$(SRC_FMT),crop=39:8:0:0:exact=1,scale,format=$(DST_FMT) -frames:v 1 fate-sws-yuv2rgb-odd: $(FATE_SWS_YUV2RGB_ODD) +# Neutral grey must stay neutral in the accurate_rnd packed RGB output paths. +# The output is |G-R| stacked over |G-B|, which must be all zero on any CPU. +SWS_GREY_SRC = color=black:s=220x16:r=1,format=yuv420p,geq=lum=16+X:cb=128:cr=128,setparams=range=tv:colorspace=bt709 +SWS_GREY_DIFF = extractplanes=r+g+b[r][g][b];[g]split[g1][g2];[g1][r]blend=all_mode=difference[gr];[g2][b]blend=all_mode=difference[gb];[gr][gb]vstack +FATE_LIBSWSCALE_FFMPEG-$(call FILTERFRAMECRC, COLOR FORMAT GEQ SETPARAMS SCALE EXTRACTPLANES SPLIT BLEND VSTACK) += fate-sws-grey-bgra-accurate-rnd \ + fate-sws-grey-bgr24-accurate-rnd-vscale +fate-sws-grey-bgra-accurate-rnd: CMD = framecrc -lavfi "$(SWS_GREY_SRC),scale=flags=accurate_rnd,format=bgra,$(SWS_GREY_DIFF)" -frames:v 1 +fate-sws-grey-bgr24-accurate-rnd-vscale: CMD = framecrc -lavfi "$(SWS_GREY_SRC),scale=220:12:flags=accurate_rnd,format=bgr24,$(SWS_GREY_DIFF)" -frames:v 1 + SWS_UNSTABLE_BACKENDS-yes += c memcpy ifeq ($(ARCH_X86_64),yes) SWS_UNSTABLE_BACKENDS-$(HAVE_X86ASM) += x86 diff --git a/tests/ref/fate/sws-grey-bgr24-accurate-rnd-vscale b/tests/ref/fate/sws-grey-bgr24-accurate-rnd-vscale new file mode 100644 index 00000000000..212f1ef9a02 --- /dev/null +++ b/tests/ref/fate/sws-grey-bgr24-accurate-rnd-vscale @@ -0,0 +1,6 @@ +#tb 0: 1/1 +#media_type 0: video +#codec_id 0: rawvideo +#dimensions 0: 220x24 +#sar 0: 3/4 +0, 0, 0, 1, 5280, 0x00000000 diff --git a/tests/ref/fate/sws-grey-bgra-accurate-rnd b/tests/ref/fate/sws-grey-bgra-accurate-rnd new file mode 100644 index 00000000000..df05fc72742 --- /dev/null +++ b/tests/ref/fate/sws-grey-bgra-accurate-rnd @@ -0,0 +1,6 @@ +#tb 0: 1/1 +#media_type 0: video +#codec_id 0: rawvideo +#dimensions 0: 220x32 +#sar 0: 1/1 +0, 0, 0, 1, 7040, 0x00000000 -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
