PR #26173 opened by Forgejo_Fairy
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26173
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26173.patch

When aspect-ratio preservation and `force_divisible_by` are enabled together, 
proportional dimensions are calculated from bounds that can subsequently be 
rounded in the opposite direction. For a `24x16` input, 
`30x30:force_original_aspect_ratio=decrease:force_divisible_by=8:reset_sar=1` 
consequently produces `24x24` instead of `24x16`. With `increase`, bounds of 
`30x25` produce `40x32` instead of `48x32`.

Align the requested bounds before calculating proportional dimensions, rounding 
downward for `decrease` and upward for `increase`. Retain nearest-multiple 
rounding for the derived dimensions and the existing minimum/maximum 
constraints.

Fixes #26104. The reported decrease regression was verified at 179830108dbe and 
its parent 543a46b4b2f.

Adds a portable 60-case `fate-filter-scale-eval` test covering both modes and 
orientations, disabled adjustment, aligned and unaligned bounds, different 
divisors, non-square pixels, and negative dimension expressions. Expected 
results are stored in `tests/ref/fate/filter-scale-eval`.

Validation on Linux/AArch64: the new FATE test fails on unpatched master and 
passes with the fix; the original FFmpeg/FFprobe reproducer returns `24x16`, 
SAR `1:1`, DAR `3:2`; the additional increase case returns `48x32`. The 
standalone test also passes with AddressSanitizer and 
UndefinedBehaviorSanitizer instrumentation of the test and dimension-adjustment 
code.


>From ebdf7daba4b63b57d01f0786b1e8bfef97f3ce7b Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 18 Nov 2026 15:20:10 +0000
Subject: [PATCH] avfilter/scale_eval: align bounds before preserving aspect
 ratio

Derive proportional dimensions from the bounds after rounding them to 
force_divisible_by. Previously a dimension could be derived from an unaligned 
bound that was subsequently rounded in the opposite direction, unnecessarily 
distorting the aspect ratio.

For example, scaling 24x16 into 30x30 with decrease and a divisor of 8 produced 
24x24 instead of 24x16. With increase, a 30x25 target produced 40x32 instead of 
48x32. Keep nearest-multiple rounding for the proportional dimension.

Add a FATE test covering both modes and orientations, aligned and unaligned 
bounds, non-square pixels, negative dimensions, and disabled adjustment.

Fixes: 179830108dbeb1c6b73105ae2234cf04874728b4
Fixes: #26104
Assisted-by: Fairy
---
 libavfilter/Makefile             |  2 +-
 libavfilter/scale_eval.c         | 35 +++++++++--------
 libavfilter/tests/scale_eval.c   | 66 ++++++++++++++++++++++++++++++++
 tests/fate/filter-video.mak      |  4 ++
 tests/ref/fate/filter-scale-eval | 60 +++++++++++++++++++++++++++++
 5 files changed, 150 insertions(+), 17 deletions(-)
 create mode 100644 libavfilter/tests/scale_eval.c
 create mode 100644 tests/ref/fate/filter-scale-eval

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 677e7be8603..aba48cce618 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -690,7 +690,7 @@ SKIPHEADERS-$(CONFIG_VULKAN)                 += 
vulkan_filter.h
 SKIPHEADERS-$(CONFIG_SCALE_CUDA_FILTER)      += vf_scale_cuda.h
 
 TOOLS     = graph2dot
-TESTPROGS = drawutils filtfmts formats integral
+TESTPROGS = drawutils filtfmts formats integral scale_eval
 
 TESTPROGS-$(CONFIG_DRAWVG_FILTER) += drawvg
 
diff --git a/libavfilter/scale_eval.c b/libavfilter/scale_eval.c
index ef075748205..bed1325428c 100644
--- a/libavfilter/scale_eval.c
+++ b/libavfilter/scale_eval.c
@@ -161,28 +161,31 @@ int ff_scale_adjust_dimensions(AVFilterLink *inlink,
      * dimensions so that it is not divisible by the set factors anymore
      * unless force_divisible_by is defined as well */
     if (force_original_aspect_ratio != SCALE_FORCE_OAR_DISABLE) {
+        int64_t tmp_w, tmp_h;
+
+        if (force_divisible_by > 1) {
+            // Align the bounds before deriving either dimension from them.
+            if (force_original_aspect_ratio == SCALE_FORCE_OAR_DECREASE) {
+                w = w / force_divisible_by * force_divisible_by;
+                h = h / force_divisible_by * force_divisible_by;
+            } else {
+                w = (w + force_divisible_by - 1) / force_divisible_by * 
force_divisible_by;
+                h = (h + force_divisible_by - 1) / force_divisible_by * 
force_divisible_by;
+            }
+        }
+
         // Including force_divisible_by here rounds to the nearest multiple of 
it.
-        int64_t tmp_w = av_rescale(h, inlink->w * w_adj, inlink->h * 
(int64_t)force_divisible_by)
+        tmp_w = av_rescale(h, inlink->w * w_adj, inlink->h * 
(int64_t)force_divisible_by)
                     * force_divisible_by;
-        int64_t tmp_h = av_rescale(w, inlink->h, inlink->w * w_adj * 
(int64_t)force_divisible_by)
+        tmp_h = av_rescale(w, inlink->h, inlink->w * w_adj * 
(int64_t)force_divisible_by)
                     * force_divisible_by;
 
         if (force_original_aspect_ratio == SCALE_FORCE_OAR_DECREASE) {
-             w = FFMIN(tmp_w, w);
-             h = FFMIN(tmp_h, h);
-             if (force_divisible_by > 1) {
-                 // round down in case provided w or h is not divisible.
-                 w = w / force_divisible_by * force_divisible_by;
-                 h = h / force_divisible_by * force_divisible_by;
-             }
+            w = FFMIN(tmp_w, w);
+            h = FFMIN(tmp_h, h);
         } else { // SCALE_FORCE_OAR_INCREASE
-             w = FFMAX(tmp_w, w);
-             h = FFMAX(tmp_h, h);
-             if (force_divisible_by > 1) {
-                 // round up in case provided w or h is not divisible.
-                 w = (w + force_divisible_by - 1) / force_divisible_by * 
force_divisible_by;
-                 h = (h + force_divisible_by - 1) / force_divisible_by * 
force_divisible_by;
-             }
+            w = FFMAX(tmp_w, w);
+            h = FFMAX(tmp_h, h);
         }
     }
 
diff --git a/libavfilter/tests/scale_eval.c b/libavfilter/tests/scale_eval.c
new file mode 100644
index 00000000000..0d7105bf52f
--- /dev/null
+++ b/libavfilter/tests/scale_eval.c
@@ -0,0 +1,66 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+
+#include "libavfilter/scale_eval.c"
+
+static void print_dimensions(int iw, int ih, int w, int h, int mode,
+                             int divisor, double w_adj)
+{
+    AVFilterLink inlink = { .w = iw, .h = ih };
+    int out_w = w, out_h = h;
+    int ret = ff_scale_adjust_dimensions(&inlink, &out_w, &out_h,
+                                         mode, divisor, w_adj);
+
+    printf("%dx%d adj=%g box=%dx%d mode=%d divisor=%d: ",
+           iw, ih, w_adj, w, h, mode, divisor);
+    if (ret < 0)
+        printf("error\n");
+    else
+        printf("%dx%d\n", out_w, out_h);
+}
+
+int main(void)
+{
+    int mode, w;
+
+    for (mode = SCALE_FORCE_OAR_DISABLE; mode <= SCALE_FORCE_OAR_INCREASE; 
mode++) {
+        for (w = 28; w <= 32; w++) {
+            print_dimensions(24, 16, w, 30, mode, 8, 1);
+            print_dimensions(16, 24, 30, w, mode, 8, 1);
+        }
+        // Increase must derive from the rounded-up height (or width).
+        print_dimensions(24, 16, 30, 25, mode, 8, 1);
+        print_dimensions(16, 24, 25, 30, mode, 8, 1);
+        // Already aligned bounds and nearest-multiple rounding.
+        print_dimensions(24, 16, 32, 32, mode, 8, 1);
+        print_dimensions(24, 16, 40, 40, mode, 8, 1);
+        // No divisibility adjustment and a non-power-of-two divisor.
+        print_dimensions(24, 16, 30, 30, mode, 1, 1);
+        print_dimensions(24, 16, 23, 23, mode, 3, 1);
+        // Non-square input pixels, as used by reset_sar.
+        print_dimensions(16, 16, 30, 30, mode, 8, 1.5);
+        print_dimensions(32, 16, 30, 30, mode, 8, 0.75);
+        // Resolve a negative dimension before aligning the bounds.
+        print_dimensions(24, 16, -8, 30, mode, 8, 1);
+        print_dimensions(24, 16, 30, -8, mode, 8, 1);
+    }
+
+    return 0;
+}
diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index 7862b54981b..4cfa35297ef 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -549,6 +549,10 @@ FATE_FILTER-$(call FILTERFRAMECRC, FPS TESTSRC2 SETPTS 
SETTB) += fate-filter-fps
 fate-filter-fps-single-cross-start: CMD = framecrc -lavfi 
testsrc2=s=16x16:r=25/2:d=0.08,settb=1/25,setpts=PTS-1,fps=1:start_time=0
 fate-filter-fps-single-cross-start: REF = 
$(SRC_PATH)/tests/ref/fate/filter-fps-single
 
+FATE_LIBAVFILTER-yes += fate-filter-scale-eval
+fate-filter-scale-eval: libavfilter/tests/scale_eval$(EXESUF)
+fate-filter-scale-eval: CMD = run libavfilter/tests/scale_eval$(EXESUF)
+
 FATE_LIBAVFILTER-yes += fate-filter-drawutils
 fate-filter-drawutils: libavfilter/tests/drawutils$(EXESUF)
 fate-filter-drawutils: CMD = run libavfilter/tests/drawutils$(EXESUF)
diff --git a/tests/ref/fate/filter-scale-eval b/tests/ref/fate/filter-scale-eval
new file mode 100644
index 00000000000..9dbe60f9cf6
--- /dev/null
+++ b/tests/ref/fate/filter-scale-eval
@@ -0,0 +1,60 @@
+24x16 adj=1 box=28x30 mode=0 divisor=8: 28x30
+16x24 adj=1 box=30x28 mode=0 divisor=8: 30x28
+24x16 adj=1 box=29x30 mode=0 divisor=8: 29x30
+16x24 adj=1 box=30x29 mode=0 divisor=8: 30x29
+24x16 adj=1 box=30x30 mode=0 divisor=8: 30x30
+16x24 adj=1 box=30x30 mode=0 divisor=8: 30x30
+24x16 adj=1 box=31x30 mode=0 divisor=8: 31x30
+16x24 adj=1 box=30x31 mode=0 divisor=8: 30x31
+24x16 adj=1 box=32x30 mode=0 divisor=8: 32x30
+16x24 adj=1 box=30x32 mode=0 divisor=8: 30x32
+24x16 adj=1 box=30x25 mode=0 divisor=8: 30x25
+16x24 adj=1 box=25x30 mode=0 divisor=8: 25x30
+24x16 adj=1 box=32x32 mode=0 divisor=8: 32x32
+24x16 adj=1 box=40x40 mode=0 divisor=8: 40x40
+24x16 adj=1 box=30x30 mode=0 divisor=1: 30x30
+24x16 adj=1 box=23x23 mode=0 divisor=3: 23x23
+16x16 adj=1.5 box=30x30 mode=0 divisor=8: 30x30
+32x16 adj=0.75 box=30x30 mode=0 divisor=8: 30x30
+24x16 adj=1 box=-8x30 mode=0 divisor=8: 48x30
+24x16 adj=1 box=30x-8 mode=0 divisor=8: 30x24
+24x16 adj=1 box=28x30 mode=1 divisor=8: 24x16
+16x24 adj=1 box=30x28 mode=1 divisor=8: 16x24
+24x16 adj=1 box=29x30 mode=1 divisor=8: 24x16
+16x24 adj=1 box=30x29 mode=1 divisor=8: 16x24
+24x16 adj=1 box=30x30 mode=1 divisor=8: 24x16
+16x24 adj=1 box=30x30 mode=1 divisor=8: 16x24
+24x16 adj=1 box=31x30 mode=1 divisor=8: 24x16
+16x24 adj=1 box=30x31 mode=1 divisor=8: 16x24
+24x16 adj=1 box=32x30 mode=1 divisor=8: 32x24
+16x24 adj=1 box=30x32 mode=1 divisor=8: 24x32
+24x16 adj=1 box=30x25 mode=1 divisor=8: 24x16
+16x24 adj=1 box=25x30 mode=1 divisor=8: 16x24
+24x16 adj=1 box=32x32 mode=1 divisor=8: 32x24
+24x16 adj=1 box=40x40 mode=1 divisor=8: 40x24
+24x16 adj=1 box=30x30 mode=1 divisor=1: 30x20
+24x16 adj=1 box=23x23 mode=1 divisor=3: 21x15
+16x16 adj=1.5 box=30x30 mode=1 divisor=8: 24x16
+32x16 adj=0.75 box=30x30 mode=1 divisor=8: 24x16
+24x16 adj=1 box=-8x30 mode=1 divisor=8: 40x24
+24x16 adj=1 box=30x-8 mode=1 divisor=8: 24x16
+24x16 adj=1 box=28x30 mode=2 divisor=8: 48x32
+16x24 adj=1 box=30x28 mode=2 divisor=8: 32x48
+24x16 adj=1 box=29x30 mode=2 divisor=8: 48x32
+16x24 adj=1 box=30x29 mode=2 divisor=8: 32x48
+24x16 adj=1 box=30x30 mode=2 divisor=8: 48x32
+16x24 adj=1 box=30x30 mode=2 divisor=8: 32x48
+24x16 adj=1 box=31x30 mode=2 divisor=8: 48x32
+16x24 adj=1 box=30x31 mode=2 divisor=8: 32x48
+24x16 adj=1 box=32x30 mode=2 divisor=8: 48x32
+16x24 adj=1 box=30x32 mode=2 divisor=8: 32x48
+24x16 adj=1 box=30x25 mode=2 divisor=8: 48x32
+16x24 adj=1 box=25x30 mode=2 divisor=8: 32x48
+24x16 adj=1 box=32x32 mode=2 divisor=8: 48x32
+24x16 adj=1 box=40x40 mode=2 divisor=8: 64x40
+24x16 adj=1 box=30x30 mode=2 divisor=1: 45x30
+24x16 adj=1 box=23x23 mode=2 divisor=3: 36x24
+16x16 adj=1.5 box=30x30 mode=2 divisor=8: 48x32
+32x16 adj=0.75 box=30x30 mode=2 divisor=8: 48x32
+24x16 adj=1 box=-8x30 mode=2 divisor=8: 48x32
+24x16 adj=1 box=30x-8 mode=2 divisor=8: 40x24
-- 
2.54.0

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

Reply via email to