[FFmpeg-devel] [PATCH v2 10/16] swscale/range_convert: saturate output instead of limiting input

Ramiro Polla ramiro.polla at gmail.com
Fri Sep 27 15:52:35 EEST 2024


For bit depths <= 14, the result is saturated to 15 bits.
For bit depths > 14, the result is saturated to 19 bits.

x86_64:
chrRangeFromJpeg8_1920_c:    5827.4   5804.5  ( 1.00x)
chrRangeFromJpeg16_1920_c:   5793.2   5792.8  ( 1.00x)
chrRangeToJpeg8_1920_c:     11726.2   9388.6  ( 1.25x)
chrRangeToJpeg16_1920_c:    10610.8   5796.5  ( 1.83x)
lumRangeFromJpeg8_1920_c:    4165.7   4147.9  ( 1.00x)
lumRangeFromJpeg16_1920_c:   4530.0   4529.0  ( 1.00x)
lumRangeToJpeg8_1920_c:      6044.8   5694.1  ( 1.06x)
lumRangeToJpeg16_1920_c:     5343.6   5334.2  ( 1.00x)

aarch64 A55:
chrRangeFromJpeg8_1920_c:   28839.3  28833.8  ( 1.00x)
chrRangeFromJpeg16_1920_c:  28843.8  28842.8  ( 1.00x)
chrRangeToJpeg8_1920_c:     44196.1  23070.6  ( 1.92x)
chrRangeToJpeg16_1920_c:    36526.7  17313.8  ( 2.11x)
lumRangeFromJpeg8_1920_c:   15384.3  15388.1  ( 1.00x)
lumRangeFromJpeg16_1920_c:  15390.1  15388.0  ( 1.00x)
lumRangeToJpeg8_1920_c:     23066.7  19226.2  ( 1.20x)
lumRangeToJpeg16_1920_c:    19224.6  19225.5  ( 1.00x)

aarch64 A76:
chrRangeFromJpeg8_1920_c:    6316.2   6317.8  ( 1.00x)
chrRangeFromJpeg16_1920_c:   6321.9   6322.9  ( 1.00x)
chrRangeToJpeg8_1920_c:     11389.3   9287.1  ( 1.23x)
chrRangeToJpeg16_1920_c:     9514.4   6104.9  ( 1.56x)
lumRangeFromJpeg8_1920_c:    4376.0   4359.1  ( 1.00x)
lumRangeFromJpeg16_1920_c:   4437.9   4358.8  ( 1.02x)
lumRangeToJpeg8_1920_c:      6667.0   5957.2  ( 1.12x)
lumRangeToJpeg16_1920_c:     6062.5   6072.5  ( 1.00x)

NOTE: all simd optimizations for range_convert have been disabled
      except for x86, which already had the same behaviour.
      they will be re-enabled when they are fixed for each architecture.
---
 libswscale/aarch64/swscale.c                  |  5 +++++
 libswscale/loongarch/swscale_init_loongarch.c |  5 +++++
 libswscale/riscv/swscale.c                    |  5 +++++
 libswscale/swscale.c                          | 21 ++++++++++++-------
 libswscale/x86/range_convert.asm              |  3 ---
 5 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/libswscale/aarch64/swscale.c b/libswscale/aarch64/swscale.c
index 653144dbca..94059cec51 100644
--- a/libswscale/aarch64/swscale.c
+++ b/libswscale/aarch64/swscale.c
@@ -225,6 +225,10 @@ void ff_chrRangeToJpeg_neon(int16_t *dstU, int16_t *dstV, int width);
 
 av_cold void ff_sws_init_range_convert_aarch64(SwsContext *c)
 {
+    /* This code is currently disabled because of changes in the base
+     * implementation of these functions. This code should be enabled
+     * again once those changes are ported to this architecture. */
+#if 0
     int cpu_flags = av_get_cpu_flags();
 
     if (have_neon(cpu_flags)) {
@@ -238,6 +242,7 @@ av_cold void ff_sws_init_range_convert_aarch64(SwsContext *c)
             }
         }
     }
+#endif
 }
 
 av_cold void ff_sws_init_swscale_aarch64(SwsContext *c)
diff --git a/libswscale/loongarch/swscale_init_loongarch.c b/libswscale/loongarch/swscale_init_loongarch.c
index de75ffc6dc..c0212dceb9 100644
--- a/libswscale/loongarch/swscale_init_loongarch.c
+++ b/libswscale/loongarch/swscale_init_loongarch.c
@@ -26,6 +26,10 @@
 
 av_cold void ff_sws_init_range_convert_loongarch(SwsContext *c)
 {
+    /* This code is currently disabled because of changes in the base
+     * implementation of these functions. This code should be enabled
+     * again once those changes are ported to this architecture. */
+#if 0
     int cpu_flags = av_get_cpu_flags();
 
     if (have_lsx(cpu_flags)) {
@@ -52,6 +56,7 @@ av_cold void ff_sws_init_range_convert_loongarch(SwsContext *c)
         }
     }
 #endif // #if HAVE_LASX
+#endif
 }
 
 av_cold void ff_sws_init_swscale_loongarch(SwsContext *c)
diff --git a/libswscale/riscv/swscale.c b/libswscale/riscv/swscale.c
index fc592c39d5..059b9b29ad 100644
--- a/libswscale/riscv/swscale.c
+++ b/libswscale/riscv/swscale.c
@@ -28,6 +28,10 @@ void ff_range_chr_from_jpeg_16_rvv(int16_t *, int16_t *, int);
 
 av_cold void ff_sws_init_range_convert_riscv(SwsContext *c)
 {
+    /* This code is currently disabled because of changes in the base
+     * implementation of these functions. This code should be enabled
+     * again once those changes are ported to this architecture. */
+#if 0
 #if HAVE_RVV
     int flags = av_get_cpu_flags();
 
@@ -47,6 +51,7 @@ av_cold void ff_sws_init_range_convert_riscv(SwsContext *c)
         c->chrConvertRange = convs[from].chr;
     }
 #endif
+#endif
 }
 
 #define RVV_INPUT(name) \
diff --git a/libswscale/swscale.c b/libswscale/swscale.c
index 18e213b21b..d39e4e0872 100644
--- a/libswscale/swscale.c
+++ b/libswscale/swscale.c
@@ -160,8 +160,10 @@ static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
 {
     int i;
     for (i = 0; i < width; i++) {
-        dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
-        dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
+        int U = (dstU[i] * 4663 - 9289992) >> 12; // -264
+        int V = (dstV[i] * 4663 - 9289992) >> 12; // -264
+        dstU[i] = FFMIN(U, (1 << 15) - 1);
+        dstV[i] = FFMIN(V, (1 << 15) - 1);
     }
 }
 
@@ -177,8 +179,10 @@ static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
 static void lumRangeToJpeg_c(int16_t *dst, int width)
 {
     int i;
-    for (i = 0; i < width; i++)
-        dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
+    for (i = 0; i < width; i++) {
+        int Y = (dst[i] * 19077 - 39057361) >> 14;
+        dst[i] = FFMIN(Y, (1 << 15) - 1);
+    }
 }
 
 static void lumRangeFromJpeg_c(int16_t *dst, int width)
@@ -194,8 +198,10 @@ static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
     int32_t *dstU = (int32_t *) _dstU;
     int32_t *dstV = (int32_t *) _dstV;
     for (i = 0; i < width; i++) {
-        dstU[i] = (FFMIN(dstU[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
-        dstV[i] = (FFMIN(dstV[i], 30775 << 4) * 4663 - (9289992 << 4)) >> 12; // -264
+        int U = (dstU[i] * 4663 - (9289992 << 4)) >> 12; // -264
+        int V = (dstV[i] * 4663 - (9289992 << 4)) >> 12; // -264
+        dstU[i] = FFMIN(U, (1 << 19) - 1);
+        dstV[i] = FFMIN(V, (1 << 19) - 1);
     }
 }
 
@@ -215,7 +221,8 @@ static void lumRangeToJpeg16_c(int16_t *_dst, int width)
     int i;
     int32_t *dst = (int32_t *) _dst;
     for (i = 0; i < width; i++) {
-        dst[i] = ((int)(FFMIN(dst[i], 30189 << 4) * 4769U - (39057361 << 2))) >> 12;
+        int Y = ((int)(FFMIN(dst[i], 30189 << 4) * 4769U - (39057361 << 2))) >> 12;
+        dst[i] = FFMIN(Y, (1 << 19) - 1);
     }
 }
 
diff --git a/libswscale/x86/range_convert.asm b/libswscale/x86/range_convert.asm
index 97c7525448..ffda009c4e 100644
--- a/libswscale/x86/range_convert.asm
+++ b/libswscale/x86/range_convert.asm
@@ -40,9 +40,6 @@ lum_from_offset:    times 4 dd 33561947
 
 SECTION .text
 
-; NOTE: there is no need to clamp the input when converting to jpeg range
-;       (like we do in the C code) because packssdw will saturate the output.
-
 ;-----------------------------------------------------------------------------
 ; lumConvertRange
 ;
-- 
2.30.2



More information about the ffmpeg-devel mailing list