PR #26332 opened by Pu Cheng (GeekQAQ) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26332 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26332.patch
Signed-off-by: Pu Cheng <[email protected]> >From 618b654ef98f2b682a4716aab7a6155b028c58aa Mon Sep 17 00:00:00 2001 From: Pu Cheng <[email protected]> Date: Wed, 25 Nov 2026 02:51:27 +0800 Subject: [PATCH 1/2] swscale/utils: do not inherit a changed dither in sws_getCachedContext() sws_init_context() changes the dither option: auto becomes bayer or error diffusion for BGR8, RGB8, BGR4_BYTE and RGB4_BYTE output and error diffusion with SWS_ERROR_DIFFUSION, and bayer becomes error diffusion for these formats with SWS_FULL_CHR_H_INT. sws_getCachedContext() copies the options of the old context to the new one, so it took the changed value over. After yuv420p to bgr8 a context for yuv444p to rgb24 had bayer dither and did not use full chroma interpolation, unlike a new context for the same parameters. Remember the value passed to sws_init_context() and copy this one, so that a dither mode set by the caller is still kept. This is a regression since ed5dd675624c, which added the copy of the options. Signed-off-by: Pu Cheng <[email protected]> --- libswscale/swscale_internal.h | 4 ++++ libswscale/utils.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/libswscale/swscale_internal.h b/libswscale/swscale_internal.h index cb13f9a7344..66f78fda389 100644 --- a/libswscale/swscale_internal.h +++ b/libswscale/swscale_internal.h @@ -704,6 +704,10 @@ struct SwsInternal { atomic_int data_unaligned_warned; int color_conversion_warned; + /* sws->dither as passed to sws_init_context(), which may change it; only + * valid if is_legacy_init is set. */ + SwsDither init_dither; + Half2FloatTables *h2f_tables; // Hardware specific private data diff --git a/libswscale/utils.c b/libswscale/utils.c index 0cd80ede650..4a6dfdc83c6 100644 --- a/libswscale/utils.c +++ b/libswscale/utils.c @@ -1980,6 +1980,7 @@ av_cold int sws_init_context(SwsContext *sws, SwsFilter *srcFilter, c->is_legacy_init = 1; c->frame_src = av_frame_alloc(); c->frame_dst = av_frame_alloc(); + c->init_dither = sws->dither; if (!c->frame_src || !c->frame_dst) return AVERROR(ENOMEM); @@ -2449,6 +2450,9 @@ SwsContext *sws_getCachedContext(SwsContext *prev, int srcW, } if (prev) { + /* sws_init_context() may have changed it for the old parameters */ + if (sws_internal(prev)->is_legacy_init) + prev->dither = sws_internal(prev)->init_dither; av_opt_copy(sws, prev); sws_free_context(&prev); } -- 2.54.1 >From c96fd710f775ed536f638a9be0aa6087bdd91eea Mon Sep 17 00:00:00 2001 From: Pu Cheng <[email protected]> Date: Wed, 25 Nov 2026 02:51:27 +0800 Subject: [PATCH 2/2] swscale/tests: add a test for the dither of cached contexts Signed-off-by: Pu Cheng <[email protected]> --- libswscale/Makefile | 1 + libswscale/tests/.gitignore | 1 + libswscale/tests/rebuilt_context.c | 185 +++++++++++++++++++++++++++++ tests/fate/libswscale.mak | 4 + tests/ref/fate/sws-rebuilt-context | 7 ++ 5 files changed, 198 insertions(+) create mode 100644 libswscale/tests/rebuilt_context.c create mode 100644 tests/ref/fate/sws-rebuilt-context diff --git a/libswscale/Makefile b/libswscale/Makefile index 4bd5ff551be..a01c65b199e 100644 --- a/libswscale/Makefile +++ b/libswscale/Makefile @@ -51,6 +51,7 @@ TESTPROGS = colorspace \ pixdesc_query \ range_convert \ rational64 \ + rebuilt_context \ swscale \ sws_ops \ sws_ops_aarch64 \ diff --git a/libswscale/tests/.gitignore b/libswscale/tests/.gitignore index b358351617c..3ce98d4ee51 100644 --- a/libswscale/tests/.gitignore +++ b/libswscale/tests/.gitignore @@ -3,6 +3,7 @@ /pixdesc_query /range_convert /rational64 +/rebuilt_context /swscale /sws_ops /sws_ops_aarch64 diff --git a/libswscale/tests/rebuilt_context.c b/libswscale/tests/rebuilt_context.c new file mode 100644 index 00000000000..5012d8a946e --- /dev/null +++ b/libswscale/tests/rebuilt_context.c @@ -0,0 +1,185 @@ +/* + * 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 + */ + +/* + * Compare contexts made by sws_getCachedContext() from an old one, for which + * sws_init_context() changed the dither option, with new ones for the same + * parameters. + */ + +#include <stdio.h> +#include <string.h> + +#include "libavutil/frame.h" +#include "libavutil/imgutils.h" +#include "libavutil/log.h" +#include "libavutil/macros.h" +#include "libavutil/pixdesc.h" +#include "libswscale/swscale.h" + +#define SRC_W 32 +#define SRC_H 16 +#define DST_W 16 +#define DST_H 8 +#define FLAGS (SWS_BICUBIC | SWS_ACCURATE_RND | SWS_BITEXACT) + +static AVFrame *alloc_frame(enum AVPixelFormat format, int width, int height) +{ + AVFrame *frame = av_frame_alloc(); + + if (!frame) + return NULL; + frame->format = format; + frame->width = width; + frame->height = height; + if (av_frame_get_buffer(frame, 0) < 0) + av_frame_free(&frame); + return frame; +} + +static int plane_height(const AVFrame *frame, int plane) +{ + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format); + return AV_CEIL_RSHIFT(frame->height, plane == 1 || plane == 2 ? desc->log2_chroma_h : 0); +} + +static SwsContext *new_context(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, + unsigned flags, SwsDither dither) +{ + SwsContext *sws = sws_alloc_context(); + + if (!sws) + return NULL; + sws->src_w = SRC_W; + sws->src_h = SRC_H; + sws->src_format = src_fmt; + sws->dst_w = DST_W; + sws->dst_h = DST_H; + sws->dst_format = dst_fmt; + sws->flags = flags; + sws->dither = dither; + if (sws_init_context(sws, NULL, NULL) < 0) + sws_free_context(&sws); + return sws; +} + +static AVFrame *convert(SwsContext *sws, enum AVPixelFormat src_fmt, + enum AVPixelFormat dst_fmt) +{ + AVFrame *src = alloc_frame(src_fmt, SRC_W, SRC_H); + AVFrame *dst = alloc_frame(dst_fmt, DST_W, DST_H); + + if (!src || !dst) + goto fail; + for (int p = 0; p < 4 && src->data[p]; p++) { + const int w = av_image_get_linesize(src_fmt, src->width, p); + for (int y = 0; y < plane_height(src, p); y++) + for (int x = 0; x < w; x++) + src->data[p][y * src->linesize[p] + x] = + 64 + ((x * 37 + y * 73 + x * y * 11 + p * 59) & 127); + } + if (sws_scale(sws, (const uint8_t * const *)src->data, src->linesize, 0, SRC_H, + dst->data, dst->linesize) < 0) + goto fail; + av_frame_free(&src); + return dst; +fail: + av_frame_free(&src); + av_frame_free(&dst); + return NULL; +} + +/* An option set by the caller in the first context is expected in the second. */ +static int test(enum AVPixelFormat src_fmt1, enum AVPixelFormat dst_fmt1, unsigned flags1, + enum AVPixelFormat src_fmt2, enum AVPixelFormat dst_fmt2, SwsDither dither) +{ + SwsContext *cached = new_context(src_fmt1, dst_fmt1, flags1, dither); + SwsContext *ref = new_context(src_fmt2, dst_fmt2, FLAGS, dither); + AVFrame *f1 = NULL, *f2 = NULL, *f_ref = NULL; + unsigned mismatches = 0; + int ret = 1; + + if (!cached || !ref) + goto end; + + f1 = convert(cached, src_fmt1, dst_fmt1); + cached = sws_getCachedContext(cached, SRC_W, SRC_H, src_fmt2, DST_W, DST_H, dst_fmt2, + FLAGS, NULL, NULL, NULL); + if (!f1 || !cached) + goto end; + f2 = convert(cached, src_fmt2, dst_fmt2); + f_ref = convert(ref, src_fmt2, dst_fmt2); + if (!f2 || !f_ref) + goto end; + + for (int p = 0; p < 4 && f2->data[p]; p++) { + const int w = av_image_get_linesize(dst_fmt2, f2->width, p); + for (int y = 0; y < plane_height(f2, p); y++) + mismatches += !!memcmp(f2->data[p] + y * f2->linesize[p], + f_ref->data[p] + y * f_ref->linesize[p], w); + } + printf("%s -> %s%s%s, then %s -> %s, dither %d: dither %s, flags %s, %u mismatching rows\n", + av_get_pix_fmt_name(src_fmt1), av_get_pix_fmt_name(dst_fmt1), + flags1 & SWS_FULL_CHR_H_INT ? " (full_chroma_int)" : "", + flags1 & SWS_ERROR_DIFFUSION ? " (error_diffusion)" : "", + av_get_pix_fmt_name(src_fmt2), av_get_pix_fmt_name(dst_fmt2), dither, + cached->dither == ref->dither ? "ok" : "differs", + cached->flags == ref->flags ? "ok" : "differ", mismatches); + ret = 0; +end: + av_frame_free(&f1); + av_frame_free(&f2); + av_frame_free(&f_ref); + sws_free_context(&cached); + sws_free_context(&ref); + return ret; +} + +int main(void) +{ + static const struct { + enum AVPixelFormat src_fmt1, dst_fmt1; + unsigned flags1; + enum AVPixelFormat src_fmt2, dst_fmt2; + SwsDither dither; + } tests[] = { + /* dither is set to bayer, which prevents full chroma interpolation */ + { AV_PIX_FMT_YUV420P, AV_PIX_FMT_BGR8, 0, AV_PIX_FMT_YUV444P, AV_PIX_FMT_RGB24, SWS_DITHER_AUTO }, + { AV_PIX_FMT_YUV420P, AV_PIX_FMT_RGB4_BYTE, 0, AV_PIX_FMT_RGB24, AV_PIX_FMT_BGRA, SWS_DITHER_AUTO }, + /* dither is set to error diffusion, because of the flags */ + { AV_PIX_FMT_YUV420P, AV_PIX_FMT_BGR8, SWS_FULL_CHR_H_INT, + AV_PIX_FMT_YUV420P, AV_PIX_FMT_BGR8, SWS_DITHER_AUTO }, + { AV_PIX_FMT_YUV420P, AV_PIX_FMT_MONOBLACK, SWS_ERROR_DIFFUSION, + AV_PIX_FMT_YUV420P, AV_PIX_FMT_MONOBLACK, SWS_DITHER_AUTO }, + /* the caller's bayer is changed to error diffusion */ + { AV_PIX_FMT_YUV420P, AV_PIX_FMT_BGR8, SWS_FULL_CHR_H_INT, + AV_PIX_FMT_YUV420P, AV_PIX_FMT_MONOBLACK, SWS_DITHER_BAYER }, + /* dither is not changed */ + { AV_PIX_FMT_YUV420P, AV_PIX_FMT_RGB24, 0, AV_PIX_FMT_YUV420P, AV_PIX_FMT_MONOBLACK, SWS_DITHER_ED }, + { AV_PIX_FMT_YUV420P, AV_PIX_FMT_RGB24, 0, AV_PIX_FMT_YUV444P, AV_PIX_FMT_RGB24, SWS_DITHER_AUTO }, + }; + int ret = 0; + + av_log_set_level(AV_LOG_ERROR); + + for (int i = 0; i < FF_ARRAY_ELEMS(tests); i++) + ret |= test(tests[i].src_fmt1, tests[i].dst_fmt1, FLAGS | tests[i].flags1, + tests[i].src_fmt2, tests[i].dst_fmt2, tests[i].dither); + + return ret; +} diff --git a/tests/fate/libswscale.mak b/tests/fate/libswscale.mak index 7e6a6712cb2..f9cec5ba94a 100644 --- a/tests/fate/libswscale.mak +++ b/tests/fate/libswscale.mak @@ -187,6 +187,10 @@ fate-sws-rational64: libswscale/tests/rational64$(EXESUF) fate-sws-rational64: CMD = run libswscale/tests/rational64$(EXESUF) fate-sws-rational64: CMP = null +FATE_LIBSWSCALE += fate-sws-rebuilt-context +fate-sws-rebuilt-context: libswscale/tests/rebuilt_context$(EXESUF) +fate-sws-rebuilt-context: CMD = run libswscale/tests/rebuilt_context$(EXESUF) + FATE_LIBSWSCALE += fate-sws-ops-entries-aarch64 fate-sws-ops-entries-aarch64: libswscale/tests/sws_ops_aarch64$(EXESUF) fate-sws-ops-entries-aarch64: REF = $(SRC_PATH)/libswscale/aarch64/ops_entries.c diff --git a/tests/ref/fate/sws-rebuilt-context b/tests/ref/fate/sws-rebuilt-context new file mode 100644 index 00000000000..d82b646a91c --- /dev/null +++ b/tests/ref/fate/sws-rebuilt-context @@ -0,0 +1,7 @@ +yuv420p -> bgr8, then yuv444p -> rgb24, dither 1: dither ok, flags ok, 0 mismatching rows +yuv420p -> rgb4_byte, then rgb24 -> bgra, dither 1: dither ok, flags ok, 0 mismatching rows +yuv420p -> bgr8 (full_chroma_int), then yuv420p -> bgr8, dither 1: dither ok, flags ok, 0 mismatching rows +yuv420p -> monob (error_diffusion), then yuv420p -> monob, dither 1: dither ok, flags ok, 0 mismatching rows +yuv420p -> bgr8 (full_chroma_int), then yuv420p -> monob, dither 2: dither ok, flags ok, 0 mismatching rows +yuv420p -> rgb24, then yuv420p -> monob, dither 3: dither ok, flags ok, 0 mismatching rows +yuv420p -> rgb24, then yuv444p -> rgb24, dither 1: dither ok, flags ok, 0 mismatching rows -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
