PR #26324 opened by Pu Cheng (GeekQAQ) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26324 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26324.patch
Signed-off-by: Pu Cheng <[email protected]> >From 5690d02ba0f581c2f4918bfa27cda9826361e616 Mon Sep 17 00:00:00 2001 From: Pu Cheng <[email protected]> Date: Wed, 25 Nov 2026 00:37:57 +0800 Subject: [PATCH 1/2] swscale/utils: compare the requested formats in sws_getCachedContext() sws_init_context() changes the formats it was called with: YUVJ formats become YUV, 0RGB and the like become ARGB, and XYZ12 becomes RGB48. It may also add to the flags. sws_getCachedContext() compared the requested formats and flags with these changed ones. So a context made for BGR0 was returned for BGRA and ignored the alpha of the new source, one for XYZ12 was returned for RGB48, and one for YUVJ was returned for YUV and used the wrong range. The other way round, a repeated request for a YUVJ format got a new context every time. Remember the formats and flags passed to sws_init_context() and compare with these. Signed-off-by: Pu Cheng <[email protected]> --- libswscale/swscale_internal.h | 6 ++++++ libswscale/utils.c | 24 ++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/libswscale/swscale_internal.h b/libswscale/swscale_internal.h index cb13f9a7344..8157e1ac1ef 100644 --- a/libswscale/swscale_internal.h +++ b/libswscale/swscale_internal.h @@ -711,6 +711,12 @@ struct SwsInternal { int is_legacy_init; + /* The fields of the same name as passed to sws_init_context(), which may + * change them; only valid if is_legacy_init is set. */ + int init_src_format; + int init_dst_format; + unsigned init_flags; + FFFramePool frame_pool; /* for sws_scale_frame() data allocations */ }; //FIXME check init (where 0) diff --git a/libswscale/utils.c b/libswscale/utils.c index 0cd80ede650..d15f7688e80 100644 --- a/libswscale/utils.c +++ b/libswscale/utils.c @@ -1977,7 +1977,10 @@ av_cold int sws_init_context(SwsContext *sws, SwsFilter *srcFilter, enum AVPixelFormat src_format, dst_format; int ret; - c->is_legacy_init = 1; + c->is_legacy_init = 1; + c->init_src_format = sws->src_format; + c->init_dst_format = sws->dst_format; + c->init_flags = sws->flags; c->frame_src = av_frame_alloc(); c->frame_dst = av_frame_alloc(); if (!c->frame_src || !c->frame_dst) @@ -2416,6 +2419,21 @@ void sws_free_context(SwsContext **pctx) *pctx = NULL; } +static int same_format_and_flags(const SwsContext *sws, int src_format, + int dst_format, unsigned flags) +{ + const SwsInternal *c = sws_internal(sws); + + if (c->is_legacy_init) + return c->init_src_format == src_format && + c->init_dst_format == dst_format && + c->init_flags == flags; + + return sws->src_format == src_format && + sws->dst_format == dst_format && + sws->flags == flags; +} + SwsContext *sws_getCachedContext(SwsContext *prev, int srcW, int srcH, enum AVPixelFormat srcFormat, int dstW, int dstH, @@ -2433,11 +2451,9 @@ SwsContext *sws_getCachedContext(SwsContext *prev, int srcW, if (prev && (prev->src_w == srcW && prev->src_h == srcH && - prev->src_format == srcFormat && prev->dst_w == dstW && prev->dst_h == dstH && - prev->dst_format == dstFormat && - prev->flags == flags && + same_format_and_flags(prev, srcFormat, dstFormat, flags) && !memcmp(prev->scaler_params, param, sizeof(prev->scaler_params)))) { return prev; -- 2.54.1 >From 3c1c30885e7c296d29529c50b7a9f969e326790b Mon Sep 17 00:00:00 2001 From: Pu Cheng <[email protected]> Date: Wed, 25 Nov 2026 00:37:57 +0800 Subject: [PATCH 2/2] swscale/tests: add a sws_getCachedContext() test Signed-off-by: Pu Cheng <[email protected]> --- libswscale/Makefile | 3 +- libswscale/tests/.gitignore | 1 + libswscale/tests/cached_context.c | 175 ++++++++++++++++++++++++++++++ tests/fate/libswscale.mak | 4 + tests/ref/fate/sws-cached-context | 7 ++ 5 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 libswscale/tests/cached_context.c create mode 100644 tests/ref/fate/sws-cached-context diff --git a/libswscale/Makefile b/libswscale/Makefile index 4bd5ff551be..de23a4ae038 100644 --- a/libswscale/Makefile +++ b/libswscale/Makefile @@ -46,7 +46,8 @@ SHLIBOBJS += framepool.o log2_tab.o half2float.o # Windows resource file SHLIBOBJS-$(HAVE_GNU_WINDRES) += swscaleres.o -TESTPROGS = colorspace \ +TESTPROGS = cached_context \ + colorspace \ floatimg_cmp \ pixdesc_query \ range_convert \ diff --git a/libswscale/tests/.gitignore b/libswscale/tests/.gitignore index b358351617c..b2286f7512f 100644 --- a/libswscale/tests/.gitignore +++ b/libswscale/tests/.gitignore @@ -1,3 +1,4 @@ +/cached_context /colorspace /floatimg_cmp /pixdesc_query diff --git a/libswscale/tests/cached_context.c b/libswscale/tests/cached_context.c new file mode 100644 index 00000000000..5ef8487718c --- /dev/null +++ b/libswscale/tests/cached_context.c @@ -0,0 +1,175 @@ +/* + * 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 returned by sws_getCachedContext() with new ones for the + * same parameters, for formats and flags that sws_init_context() changes. + */ + +#include <stdio.h> +#include <string.h> + +#include "libavutil/error.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 *get_context(SwsContext *prev, enum AVPixelFormat src_fmt, + enum AVPixelFormat dst_fmt) +{ + return sws_getCachedContext(prev, SRC_W, SRC_H, src_fmt, DST_W, DST_H, dst_fmt, + FLAGS, NULL, NULL, NULL); +} + +static int set_saturation(SwsContext *sws, int value) +{ + int *inv_table, *table, src_range, dst_range, brightness, contrast, saturation; + int ret = sws_getColorspaceDetails(sws, &inv_table, &src_range, &table, &dst_range, + &brightness, &contrast, &saturation); + if (ret < 0) + return ret; + return sws_setColorspaceDetails(sws, inv_table, src_range, table, dst_range, + brightness, contrast, value); +} + +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] = + (x * 37 + y * 73 + x * y * 11 + p * 59) & 255; + } + 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; +} + +/* With saturation >= 0, set it in the first context and expect it in the second. */ +static int test(enum AVPixelFormat src_fmt1, enum AVPixelFormat dst_fmt1, + enum AVPixelFormat src_fmt2, enum AVPixelFormat dst_fmt2, + int saturation) +{ + SwsContext *cached = get_context(NULL, src_fmt1, dst_fmt1); + SwsContext *ref = get_context(NULL, src_fmt2, dst_fmt2); + AVFrame *f1 = NULL, *f2 = NULL, *f_ref = NULL; + unsigned mismatches = 0; + int ret = 1; + + if (!cached || !ref) + goto end; + if (saturation >= 0 && + (set_saturation(cached, saturation) < 0 || set_saturation(ref, saturation) < 0)) + goto end; + + f1 = convert(cached, src_fmt1, dst_fmt1); + cached = get_context(cached, src_fmt2, dst_fmt2); + 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, then %s -> %s%s: %u mismatching rows\n", + av_get_pix_fmt_name(src_fmt1), av_get_pix_fmt_name(dst_fmt1), + av_get_pix_fmt_name(src_fmt2), av_get_pix_fmt_name(dst_fmt2), + saturation >= 0 ? ", saturation set" : "", 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 enum AVPixelFormat tests[][4] = { + { AV_PIX_FMT_BGR0, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_BGRA, AV_PIX_FMT_YUVA420P }, + { AV_PIX_FMT_0RGB, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_ARGB, AV_PIX_FMT_YUVA420P }, + { AV_PIX_FMT_XYZ12LE, AV_PIX_FMT_YUV420P, AV_PIX_FMT_RGB48LE, AV_PIX_FMT_YUV420P }, + { AV_PIX_FMT_YUV420P, AV_PIX_FMT_XYZ12LE, AV_PIX_FMT_YUV420P, AV_PIX_FMT_RGB48LE }, + }; + /* sws_init_context() changes the format or the flags of the first two */ + static const enum AVPixelFormat same[][2] = { + { AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_RGB24 }, + { AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA }, + { AV_PIX_FMT_YUV420P, AV_PIX_FMT_RGB24 }, + }; + int ret = 0; + + av_log_set_level(AV_LOG_ERROR); + + for (int i = 0; i < FF_ARRAY_ELEMS(tests); i++) + ret |= test(tests[i][0], tests[i][1], tests[i][2], tests[i][3], -1); + for (int i = 0; i < FF_ARRAY_ELEMS(same); i++) + ret |= test(same[i][0], same[i][1], same[i][0], same[i][1], 0); + + return ret; +} diff --git a/tests/fate/libswscale.mak b/tests/fate/libswscale.mak index 7e6a6712cb2..a1303890a53 100644 --- a/tests/fate/libswscale.mak +++ b/tests/fate/libswscale.mak @@ -6,6 +6,10 @@ FATE_LIBSWSCALE += fate-sws-unscaled-threaded fate-sws-unscaled-threaded: libswscale/tests/unscaled_threaded$(EXESUF) fate-sws-unscaled-threaded: CMD = run libswscale/tests/unscaled_threaded$(EXESUF) +FATE_LIBSWSCALE += fate-sws-cached-context +fate-sws-cached-context: libswscale/tests/cached_context$(EXESUF) +fate-sws-cached-context: CMD = run libswscale/tests/cached_context$(EXESUF) + FATE_LIBSWSCALE += fate-sws-pixdesc-query fate-sws-pixdesc-query: libswscale/tests/pixdesc_query$(EXESUF) fate-sws-pixdesc-query: CMD = run libswscale/tests/pixdesc_query$(EXESUF) diff --git a/tests/ref/fate/sws-cached-context b/tests/ref/fate/sws-cached-context new file mode 100644 index 00000000000..10a4acd95b7 --- /dev/null +++ b/tests/ref/fate/sws-cached-context @@ -0,0 +1,7 @@ +bgr0 -> yuva420p, then bgra -> yuva420p: 0 mismatching rows +0rgb -> yuva420p, then argb -> yuva420p: 0 mismatching rows +xyz12le -> yuv420p, then rgb48le -> yuv420p: 0 mismatching rows +yuv420p -> xyz12le, then yuv420p -> rgb48le: 0 mismatching rows +yuvj420p -> rgb24, then yuvj420p -> rgb24, saturation set: 0 mismatching rows +rgb24 -> rgba, then rgb24 -> rgba, saturation set: 0 mismatching rows +yuv420p -> rgb24, then yuv420p -> rgb24, saturation set: 0 mismatching rows -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
