[FFmpeg-devel] [PATCH 1/2] swscale/unscaled: correctly round yuv2yuv when not dithering

Niklas Haas ffmpeg at haasn.xyz
Tue Dec 17 16:27:40 EET 2024


From: Niklas Haas <git at haasn.dev>

We should at least bias towards the nearest integer, instead of always
rounding down, when not dithering. This is a bit more correct.

Signed-off-by: Niklas Haas <git at haasn.dev>
Sponsored-by: Sovereign Tech Fund
---
 libswscale/swscale_unscaled.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c
index c7ad6b014a..ea6fb038bc 100644
--- a/libswscale/swscale_unscaled.c
+++ b/libswscale/swscale_unscaled.c
@@ -2075,17 +2075,18 @@ static int packedCopyWrapper(SwsInternal *c, const uint8_t *const src[],
 
 #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
     unsigned shift= src_depth-dst_depth, tmp;\
+    unsigned bias = 1 << (shift - 1);\
     if (c->opts.dither == SWS_DITHER_NONE) {\
         for (i = 0; i < height; i++) {\
             for (j = 0; j < length-7; j+=8) {\
-                dst[j+0] = dbswap(bswap(src[j+0])>>shift);\
-                dst[j+1] = dbswap(bswap(src[j+1])>>shift);\
-                dst[j+2] = dbswap(bswap(src[j+2])>>shift);\
-                dst[j+3] = dbswap(bswap(src[j+3])>>shift);\
-                dst[j+4] = dbswap(bswap(src[j+4])>>shift);\
-                dst[j+5] = dbswap(bswap(src[j+5])>>shift);\
-                dst[j+6] = dbswap(bswap(src[j+6])>>shift);\
-                dst[j+7] = dbswap(bswap(src[j+7])>>shift);\
+                dst[j+0] = dbswap(bswap(src[j+0] + bias))>>shift);\
+                dst[j+1] = dbswap(bswap(src[j+1] + bias))>>shift);\
+                dst[j+2] = dbswap(bswap(src[j+2] + bias))>>shift);\
+                dst[j+3] = dbswap(bswap(src[j+3] + bias))>>shift);\
+                dst[j+4] = dbswap(bswap(src[j+4] + bias))>>shift);\
+                dst[j+5] = dbswap(bswap(src[j+5] + bias))>>shift);\
+                dst[j+6] = dbswap(bswap(src[j+6] + bias))>>shift);\
+                dst[j+7] = dbswap(bswap(src[j+7] + bias))>>shift);\
             }\
             for (; j < length; j++) {\
                 dst[j] = dbswap(bswap(src[j])>>shift);\
@@ -2169,6 +2170,7 @@ static int planarCopyWrapper(SwsInternal *c, const uint8_t *const src[],
                 uint16_t *dstPtr2 = (uint16_t*)dstPtr;
 
                 if (dst_depth == 8) {
+                    av_assert1(src_depth > 8);
                     if(isBE(c->opts.src_format) == HAVE_BIGENDIAN){
                         DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
                     } else {
@@ -2248,7 +2250,7 @@ static int planarCopyWrapper(SwsInternal *c, const uint8_t *const src[],
                         dstPtr2 += dstStride[plane]/2;
                         srcPtr2 += srcStride[plane]/2;
                     }
-                } else {
+                } else { /* src_depth > dst_depth */
                     if(isBE(c->opts.src_format) == HAVE_BIGENDIAN){
                         if(isBE(c->opts.dst_format) == HAVE_BIGENDIAN){
                             DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
-- 
2.47.0



More information about the ffmpeg-devel mailing list