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

With `setsar=2,crop=12:16:keep_aspect=1` on 24×16 input, crop advertises SAR 
4:1 on its output link but forwards frames carrying SAR 2:1. Since 0c3afc7e5643 
removed the filter core’s unconditional SAR overwrite, this also produces 
incorrect encoded/container aspect ratios. Adding `scale=w=-2:h=16:reset_sar=1` 
produces 24×16 instead of 48×16.

Adjust each frame’s own SAR using its input dimensions and the crop dimensions 
before changing dimensions or hardware crop offsets. This preserves per-frame 
SAR changes and leaves SAR untouched when `keep_aspect` is disabled. The 
arithmetic follows the existing bounded rational calculation used for crop’s 
link configuration.

Add a sample-free FATE test covering anamorphic input, changing and unspecified 
SAR, disabled keep_aspect, chroma-rounded and exact crops, and runtime width 
changes. Actual frame/link metadata is compared with a reference file.

Validation on AArch64 Linux: the new `fate-filter-crop-sar` fails without the 
fix and passes with it; existing crop, crop_scale, crop_scale_vflip, crop_vflip 
and setsar FATE tests pass. The reported NUT output changes to SAR 4:1 / DAR 
3:1, and downstream scale produces 48×16 with SAR 1:1. Hardware surfaces were 
not exercised. `git diff --check` passes.

Fixes #26103.


From 9a4cfbf1c682f66f4e05be3b01c514e9f334db2c Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Wed, 18 Nov 2026 15:22:58 +0000
Subject: [PATCH] avfilter/crop: preserve frame display aspect ratio with
 keep_aspect

keep_aspect updates the output link SAR but leaves the input SAR on
output frames. Since 0c3afc7e56434a5e4ee16cf277113ddd3eac7cb4 removed
the filter core's unconditional frame SAR assignment, this also gives
encoded output the wrong aspect ratio.

Adjust each frame's SAR using its input dimensions and the crop size,
before changing dimensions or hardware crop offsets. Use the frame's
own SAR rather than the cached output link value to preserve per-frame
aspect ratio changes. Leave SAR untouched when keep_aspect is disabled.

Add a sample-free FATE test for anamorphic input, changing and unknown
SAR, disabled keep_aspect, chroma rounding, exact crops and runtime
width changes. The test prints actual frame and link metadata for
comparison with a reference file.

Fixes #26103.

Assisted-by: Fairy
---
 libavfilter/Makefile           |   1 +
 libavfilter/tests/.gitignore   |   1 +
 libavfilter/tests/crop.c       | 111 +++++++++++++++++++++++++++++++++
 libavfilter/vf_crop.c          |   7 +++
 tests/fate/filter-video.mak    |   4 ++
 tests/ref/fate/filter-crop-sar |  20 ++++++
 6 files changed, 144 insertions(+)
 create mode 100644 libavfilter/tests/crop.c
 create mode 100644 tests/ref/fate/filter-crop-sar

diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index 677e7be8603..b0d1d7faccc 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -692,6 +692,7 @@ SKIPHEADERS-$(CONFIG_SCALE_CUDA_FILTER)      += 
vf_scale_cuda.h
 TOOLS     = graph2dot
 TESTPROGS = drawutils filtfmts formats integral
 
+TESTPROGS-$(CONFIG_CROP_FILTER) += crop
 TESTPROGS-$(CONFIG_DRAWVG_FILTER) += drawvg
 
 TOOLS-$(CONFIG_LIBZMQ) += zmqsend
diff --git a/libavfilter/tests/.gitignore b/libavfilter/tests/.gitignore
index 18018055732..632fdef9f15 100644
--- a/libavfilter/tests/.gitignore
+++ b/libavfilter/tests/.gitignore
@@ -1,3 +1,4 @@
+/crop
 /dnn-layer-conv2d
 /dnn-layer-depth2space
 /dnn-layer-maximum
diff --git a/libavfilter/tests/crop.c b/libavfilter/tests/crop.c
new file mode 100644
index 00000000000..6dde8d519b8
--- /dev/null
+++ b/libavfilter/tests/crop.c
@@ -0,0 +1,111 @@
+/*
+ * 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 "libavutil/common.h"
+#include "libavutil/error.h"
+#include "libavutil/frame.h"
+#include "libavutil/log.h"
+#include "libavfilter/avfilter.h"
+#include "libavfilter/buffersink.h"
+#include "libavfilter/buffersrc.h"
+
+static int test_crop(const char *options)
+{
+    static const AVRational input_sar[] = { { 2, 1 }, { 3, 2 }, { 0, 1 }, { 1, 
1 } };
+    AVFilterGraph *graph = avfilter_graph_alloc();
+    AVFilterContext *src, *crop, *sink;
+    AVFrame *frame = av_frame_alloc();
+    int ret = AVERROR(ENOMEM);
+
+    if (!graph || !frame)
+        goto end;
+    graph->nb_threads = 1;
+
+    ret = avfilter_graph_create_filter(&src, avfilter_get_by_name("buffer"), 
"src",
+                                      
"video_size=24x16:pix_fmt=yuv420p:time_base=1/25:pixel_aspect=2/1",
+                                      NULL, graph);
+    if (ret < 0)
+        goto end;
+    ret = avfilter_graph_create_filter(&crop, avfilter_get_by_name("crop"), 
"crop",
+                                      options, NULL, graph);
+    if (ret < 0)
+        goto end;
+    ret = avfilter_graph_create_filter(&sink, 
avfilter_get_by_name("buffersink"), "sink",
+                                      NULL, NULL, graph);
+    if (ret < 0)
+        goto end;
+    if ((ret = avfilter_link(src, 0, crop, 0)) < 0 ||
+        (ret = avfilter_link(crop, 0, sink, 0)) < 0 ||
+        (ret = avfilter_graph_config(graph, NULL)) < 0)
+        goto end;
+
+    printf("%s\n", options);
+    for (int i = 0; i < FF_ARRAY_ELEMS(input_sar); i++) {
+        AVRational sar;
+
+        /* Exercise runtime crop reconfiguration on the last frame. */
+        if (i == 3) {
+            ret = avfilter_process_command(crop, "w", "8", NULL, 0, 0);
+            if (ret < 0)
+                goto end;
+        }
+        frame->format = AV_PIX_FMT_YUV420P;
+        frame->width = 24;
+        frame->height = 16;
+        frame->sample_aspect_ratio = input_sar[i];
+        frame->pts = i;
+        if ((ret = av_frame_get_buffer(frame, 0)) < 0 ||
+            (ret = av_buffersrc_add_frame(src, frame)) < 0 ||
+            (ret = av_buffersink_get_frame(sink, frame)) < 0)
+            goto end;
+
+        sar = av_buffersink_get_sample_aspect_ratio(sink);
+        printf("frame %d: %dx%d sar %d/%d, link sar %d/%d\n",
+               i, frame->width, frame->height,
+               frame->sample_aspect_ratio.num, frame->sample_aspect_ratio.den,
+               sar.num, sar.den);
+        av_frame_unref(frame);
+    }
+
+end:
+    av_frame_free(&frame);
+    avfilter_graph_free(&graph);
+    return ret;
+}
+
+int main(void)
+{
+    static const char *const options[] = {
+        "w=12:h=16:keep_aspect=1",
+        "w=12:h=16:keep_aspect=0",
+        "w=13:h=9:keep_aspect=1",
+        "w=13:h=9:keep_aspect=1:exact=1",
+    };
+
+    av_log_set_level(AV_LOG_ERROR);
+    for (int i = 0; i < FF_ARRAY_ELEMS(options); i++) {
+        int ret = test_crop(options[i]);
+        if (ret < 0) {
+            fprintf(stderr, "Crop test failed: %s\n", av_err2str(ret));
+            return 1;
+        }
+    }
+    return 0;
+}
diff --git a/libavfilter/vf_crop.c b/libavfilter/vf_crop.c
index f25e0564aa7..2eac6ba5491 100644
--- a/libavfilter/vf_crop.c
+++ b/libavfilter/vf_crop.c
@@ -308,6 +308,13 @@ static int filter_frame(AVFilterLink *link, AVFrame *frame)
             (int)s->var_values[VAR_N], s->var_values[VAR_T],
             s->x, s->y, s->x+s->w, s->y+s->h);
 
+    if (s->keep_aspect) {
+        AVRational dar = av_mul_q(frame->sample_aspect_ratio,
+                                 (AVRational){ frame->width, frame->height });
+        av_reduce(&frame->sample_aspect_ratio.num, 
&frame->sample_aspect_ratio.den,
+                  (int64_t)dar.num * s->h, (int64_t)dar.den * s->w, INT_MAX);
+    }
+
     if (desc->flags & AV_PIX_FMT_FLAG_HWACCEL) {
         frame->crop_top   += s->y;
         frame->crop_left  += s->x;
diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index 7862b54981b..1770c8313b9 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -580,6 +580,10 @@ FATE_FILTER_VSYNTH_PGMYUV-$(call ALLYES, SCALE_FILTER 
FORMAT_FILTER SPLIT_FILTER
 $(FATE_FILTER_ALPHAEXTRACT_ALPHAMERGE): fate-filter-alphaextract_alphamerge_%: 
tests/data/filtergraphs/alphamerge_alphaextract_%
 $(FATE_FILTER_ALPHAEXTRACT_ALPHAMERGE): CMD = framecrc 
-auto_conversion_filters -c:v pgmyuv -i $(SRC) -/filter_complex 
$(TARGET_PATH)/tests/data/filtergraphs/alphamerge_alphaextract$(@:fate-filter-alphaextract_alphamerge%=%)
 
+FATE_LIBAVFILTER-$(CONFIG_CROP_FILTER) += fate-filter-crop-sar
+fate-filter-crop-sar: libavfilter/tests/crop$(EXESUF)
+fate-filter-crop-sar: CMD = run libavfilter/tests/crop$(EXESUF)
+
 FATE_FILTER_VSYNTH_VIDEO_FILTER-$(CONFIG_CROP_FILTER) += fate-filter-crop
 fate-filter-crop: CMD = video_filter "crop=iw-100:ih-100:100:100"
 
diff --git a/tests/ref/fate/filter-crop-sar b/tests/ref/fate/filter-crop-sar
new file mode 100644
index 00000000000..70e774fcfb9
--- /dev/null
+++ b/tests/ref/fate/filter-crop-sar
@@ -0,0 +1,20 @@
+w=12:h=16:keep_aspect=1
+frame 0: 12x16 sar 4/1, link sar 4/1
+frame 1: 12x16 sar 3/1, link sar 4/1
+frame 2: 12x16 sar 0/1, link sar 4/1
+frame 3: 8x16 sar 3/1, link sar 6/1
+w=12:h=16:keep_aspect=0
+frame 0: 12x16 sar 2/1, link sar 2/1
+frame 1: 12x16 sar 3/2, link sar 2/1
+frame 2: 12x16 sar 0/1, link sar 2/1
+frame 3: 8x16 sar 1/1, link sar 2/1
+w=13:h=9:keep_aspect=1
+frame 0: 12x8 sar 2/1, link sar 2/1
+frame 1: 12x8 sar 3/2, link sar 2/1
+frame 2: 12x8 sar 0/1, link sar 2/1
+frame 3: 8x8 sar 3/2, link sar 3/1
+w=13:h=9:keep_aspect=1:exact=1
+frame 0: 13x9 sar 27/13, link sar 27/13
+frame 1: 13x9 sar 81/52, link sar 27/13
+frame 2: 13x9 sar 0/1, link sar 27/13
+frame 3: 8x9 sar 27/16, link sar 27/8
-- 
2.54.0

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

Reply via email to