PR #23024 opened by Niklas Haas (haasn)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23024
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23024.patch

In an op list like:

  [ u8 +XXX] SWS_OP_READ         : 1 elem(s) planar >> 3
  [ u8 .XXX] SWS_OP_FILTER_V     : 256 -> 320 bilinear (2 taps)
  [f32 .XXX] SWS_OP_SCALE        : * 65535
  [f32 +XXX] SWS_OP_CONVERT      : f32 -> u16
  [u16 zXXX] SWS_OP_SWAP_BYTES
  [u16 zzzX] SWS_OP_SWIZZLE      : 0003
  [u16 zzz+] SWS_OP_CLEAR        : {_ _ _ 65535}
  [u16 XXXX] SWS_OP_WRITE        : 4 elem(s) packed >> 0

The current version of the code would happily push the SWS_OP_SCALE past
the truncating conversion, leading to degenerate loss of information. (In
this case, the result was quite extreme)

Incidentally, this does not change the output of the sws_ops test because that
test does not currently include split scaling passes.

Signed-off-by: Niklas Haas <[email protected]>


>From 423b0eb1ecb2339c1d07348d998e4f114a8095dd Mon Sep 17 00:00:00 2001
From: Niklas Haas <[email protected]>
Date: Tue, 5 May 2026 20:11:25 +0200
Subject: [PATCH] swscale/ops_optimizer: don't push scale past truncating
 conversions

In an op list like:

  [ u8 +XXX] SWS_OP_READ         : 1 elem(s) planar >> 3
  [ u8 .XXX] SWS_OP_FILTER_V     : 256 -> 320 bilinear (2 taps)
  [f32 .XXX] SWS_OP_SCALE        : * 65535
  [f32 +XXX] SWS_OP_CONVERT      : f32 -> u16
  [u16 zXXX] SWS_OP_SWAP_BYTES
  [u16 zzzX] SWS_OP_SWIZZLE      : 0003
  [u16 zzz+] SWS_OP_CLEAR        : {_ _ _ 65535}
  [u16 XXXX] SWS_OP_WRITE        : 4 elem(s) packed >> 0

The current version of the code would happily push the SWS_OP_SCALE past
the truncating conversion, leading to degenerate loss of information. (In
this case, the result was quite extreme)

Incidentally, this does not change the output of the sws_ops test because that
test does not currently include split scaling passes.

Signed-off-by: Niklas Haas <[email protected]>
---
 libswscale/ops_optimizer.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/libswscale/ops_optimizer.c b/libswscale/ops_optimizer.c
index c7972e471f..619c6cf42b 100644
--- a/libswscale/ops_optimizer.c
+++ b/libswscale/ops_optimizer.c
@@ -337,6 +337,16 @@ static bool extract_swizzle(SwsLinearOp *op, SwsComps 
prev, SwsSwizzleOp *out_sw
     return true;
 }
 
+static int op_result_is_exact(const SwsOp *op)
+{
+    for (int i = 0; i < 4; i++) {
+        if (SWS_OP_NEEDED(op, i) && !(op->comps.flags[i] & SWS_COMP_EXACT))
+            return false;
+    }
+
+    return true;
+}
+
 int ff_sws_op_list_optimize(SwsOpList *ops)
 {
     int ret;
@@ -769,9 +779,10 @@ retry:
         }
 
         case SWS_OP_SCALE:
-            /* Scaling by integer before conversion to int */
+            /* Exact integer multiplication */
             if (op->scale.factor.den == 1 && next->op == SWS_OP_CONVERT &&
-                ff_sws_pixel_type_is_int(next->convert.to))
+                ff_sws_pixel_type_is_int(next->convert.to) &&
+                op_result_is_exact(op))
             {
                 op->type = next->convert.to;
                 FFSWAP(SwsOp, *op, *next);
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to