PR #26077 opened by Forgejo_Fairy URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26077 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26077.patch
When hwmap negotiates VAAPI output while its destination device is Vulkan, av_hwframe_ctx_create_derived() can return a VAAPI-format frames context backed by a Vulkan device. Downstream consumers then interpret the device using the wrong type. Share the format validation used by ordinary frame-context initialization and apply it before derived-context creation, including the unmapping shortcut. Incompatible requests now return AVERROR(ENOSYS) with the existing unsupported-format diagnostic. Document explicit output-format selection for hwmap's derive_device option. Add a portable DRM-only FATE test covering invalid hardware/software formats, valid derivation, and invalid/valid unmapping. The test requires no GPU, device node, or media sample; expected output is stored in tests/ref/fate/hwframe. Validation: fate-hwframe fails on unpatched master and passes with this change; all 74 enabled fate-libavutil tests pass. On an NVIDIA RTX 5090, an API reproducer changes from successfully returning format=vaapi/device=vulkan to rejecting that request, while format=vulkan/device=vulkan still succeeds. A filter-level reproduction now fails during graph configuration with the format/device diagnostic. The reporter's exact Intel VAAPI pipeline was not available for testing. git diff --check passes. Fixes #26020. >From e8d1ddbce4fcbdf0de276829d3a44ae11dea18c8 Mon Sep 17 00:00:00 2001 From: Michael Niedermayer <[email protected]> Date: Sun, 15 Nov 2026 09:10:34 +0000 Subject: [PATCH] avutil/hwcontext: validate the format of derived frames Derived frame contexts bypass av_hwframe_ctx_init(), so the format passed to av_hwframe_ctx_create_derived() is not checked against the destination device's supported formats. For example, hwmap can create a VAAPI frames context backed by a Vulkan device when format negotiation selects VAAPI. Downstream consumers then interpret the device using the wrong type. Share the existing format check and apply it before deriving a context, including the unmapping shortcut. Keep normal frame initialization's validation and error code unchanged. Add a DRM-only FATE test for invalid hardware/software output formats and valid derivation/unmapping. No GPU or device node is required. Document explicit format selection with hwmap's derive_device option. Fixes #26020. Assisted-by: Fairy --- doc/filters.texi | 4 ++ libavutil/Makefile | 1 + libavutil/hwcontext.c | 40 +++++++++---- libavutil/tests/.gitignore | 1 + libavutil/tests/hwframe.c | 112 +++++++++++++++++++++++++++++++++++++ tests/fate/libavutil.mak | 4 ++ tests/ref/fate/hwframe | 7 +++ 7 files changed, 157 insertions(+), 12 deletions(-) create mode 100644 libavutil/tests/hwframe.c create mode 100644 tests/ref/fate/hwframe diff --git a/doc/filters.texi b/doc/filters.texi index fb866d24d16..4bde454523e 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -16128,6 +16128,10 @@ Defaults to @var{read+write} if not specified. Rather than using the device supplied at initialisation, instead derive a new device of type @var{type} from the device the input frames exist on. +For hardware frame output, the output pixel format must be supported by the +derived device. Use the @ref{format} filter to select it explicitly when +necessary, for example @code{hwmap=derive_device=vulkan,format=vulkan}. + @item reverse In a hardware to hardware mapping, map in reverse - create frames in the sink and map them back to the source. This may be necessary in some cases where diff --git a/libavutil/Makefile b/libavutil/Makefile index 1f91fd5d170..38a83dcdba1 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -334,6 +334,7 @@ TESTPROGS = adler32 \ tea \ TESTPROGS-$(CONFIG_CUDA) += hwcontext_cuda +TESTPROGS-$(CONFIG_LIBDRM) += hwframe TESTPROGS-$(HAVE_THREADS) += cpu_init TESTPROGS-$(HAVE_LZO1X_999_COMPRESS) += lzo diff --git a/libavutil/hwcontext.c b/libavutil/hwcontext.c index 297cc5dd85d..8b3db6e14d9 100644 --- a/libavutil/hwcontext.c +++ b/libavutil/hwcontext.c @@ -344,11 +344,30 @@ fail: return ret; } +static int hwframe_check_format(void *logctx, const HWContextType *hw_type, + enum AVPixelFormat format) +{ + const enum AVPixelFormat *pix_fmt; + + /* validate the pixel format */ + for (pix_fmt = hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) { + if (*pix_fmt == format) + break; + } + if (*pix_fmt == AV_PIX_FMT_NONE) { + av_log(logctx, AV_LOG_ERROR, + "The hardware pixel format '%s' is not supported by the device type '%s'\n", + av_get_pix_fmt_name(format), hw_type->name); + return AVERROR(ENOSYS); + } + + return 0; +} + int av_hwframe_ctx_init(AVBufferRef *ref) { FFHWFramesContext *ctxi = (FFHWFramesContext*)ref->data; AVHWFramesContext *ctx = &ctxi->p; - const enum AVPixelFormat *pix_fmt; int ret; if (ctxi->source_frames) { @@ -356,17 +375,9 @@ int av_hwframe_ctx_init(AVBufferRef *ref) return 0; } - /* validate the pixel format */ - for (pix_fmt = ctxi->hw_type->pix_fmts; *pix_fmt != AV_PIX_FMT_NONE; pix_fmt++) { - if (*pix_fmt == ctx->format) - break; - } - if (*pix_fmt == AV_PIX_FMT_NONE) { - av_log(ctx, AV_LOG_ERROR, - "The hardware pixel format '%s' is not supported by the device type '%s'\n", - av_get_pix_fmt_name(ctx->format), ctxi->hw_type->name); - return AVERROR(ENOSYS); - } + ret = hwframe_check_format(ctx, ctxi->hw_type, ctx->format); + if (ret < 0) + return ret; /* validate the dimensions */ ret = av_image_check_size(ctx->width, ctx->height, 0, ctx); @@ -932,12 +943,17 @@ int av_hwframe_ctx_create_derived(AVBufferRef **derived_frame_ctx, AVBufferRef *source_frame_ctx, int flags) { + FFHWDeviceContext *device_ctx = (FFHWDeviceContext*)derived_device_ctx->data; AVBufferRef *dst_ref = NULL; FFHWFramesContext *dsti = NULL; FFHWFramesContext *srci = (FFHWFramesContext*)source_frame_ctx->data; AVHWFramesContext *dst, *src = &srci->p; int ret; + ret = hwframe_check_format(&device_ctx->p, device_ctx->hw_type, format); + if (ret < 0) + return ret; + if (srci->source_frames) { AVHWFramesContext *src_src = (AVHWFramesContext*)srci->source_frames->data; diff --git a/libavutil/tests/.gitignore b/libavutil/tests/.gitignore index ac300592490..2e735e72a9f 100644 --- a/libavutil/tests/.gitignore +++ b/libavutil/tests/.gitignore @@ -35,6 +35,7 @@ /hmac /hwcontext_cuda /hwdevice +/hwframe /imgutils /integer /lfg diff --git a/libavutil/tests/hwframe.c b/libavutil/tests/hwframe.c new file mode 100644 index 00000000000..6394c4c2718 --- /dev/null +++ b/libavutil/tests/hwframe.c @@ -0,0 +1,112 @@ +/* + * 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/error.h" +#include "libavutil/hwcontext.h" +#include "libavutil/hwcontext_drm.h" +#include "libavutil/log.h" +#include "libavutil/pixdesc.h" + +static void print_result(const char *name, int ret, AVBufferRef *ref) +{ + printf("%s: ", name); + if (ret == AVERROR(ENOSYS)) { + printf("unsupported format\n"); + } else if (ret < 0) { + printf("error %d\n", ret); + } else { + const AVHWFramesContext *ctx = (const AVHWFramesContext *)ref->data; + printf("format=%s device=%s\n", av_get_pix_fmt_name(ctx->format), + av_hwdevice_get_type_name(ctx->device_ctx->type)); + } +} + +int main(void) +{ + AVBufferRef *devices[2] = { NULL, NULL }; + AVBufferRef *src = NULL, *derived = NULL, *dst = NULL; + AVHWFramesContext *ctx; + int ret = 0; + + av_log_set_level(AV_LOG_QUIET); + + /* DRM contexts need no device node until a frame is actually mapped. */ + for (int i = 0; i < 2; i++) { + AVHWDeviceContext *device; + devices[i] = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DRM); + if (!devices[i]) { + ret = AVERROR(ENOMEM); + goto fail; + } + device = (AVHWDeviceContext *)devices[i]->data; + ((AVDRMDeviceContext *)device->hwctx)->fd = -1; + ret = av_hwdevice_ctx_init(devices[i]); + if (ret < 0) + goto fail; + } + + src = av_hwframe_ctx_alloc(devices[0]); + if (!src) { + ret = AVERROR(ENOMEM); + goto fail; + } + ctx = (AVHWFramesContext *)src->data; + ctx->format = AV_PIX_FMT_DRM_PRIME; + ctx->sw_format = AV_PIX_FMT_NV12; + ctx->width = 64; + ctx->height = 64; + ret = av_hwframe_ctx_init(src); + print_result("source", ret, src); + if (ret < 0) + goto fail; + + ret = av_hwframe_ctx_create_derived(&dst, AV_PIX_FMT_VAAPI, + devices[1], src, 0); + print_result("derive hardware mismatch", ret, dst); + av_buffer_unref(&dst); + ret = av_hwframe_ctx_create_derived(&dst, AV_PIX_FMT_NV12, + devices[1], src, 0); + print_result("derive software format", ret, dst); + av_buffer_unref(&dst); + + ret = av_hwframe_ctx_create_derived(&derived, AV_PIX_FMT_DRM_PRIME, + devices[1], src, 0); + print_result("derive valid", ret, derived); + if (ret < 0) + goto fail; + + ret = av_hwframe_ctx_create_derived(&dst, AV_PIX_FMT_VAAPI, + devices[0], derived, 0); + print_result("unmap hardware mismatch", ret, dst); + av_buffer_unref(&dst); + ret = av_hwframe_ctx_create_derived(&dst, AV_PIX_FMT_DRM_PRIME, + devices[0], derived, 0); + print_result("unmap valid", ret, dst); + if (ret >= 0) + printf("unmap returned source: %d\n", dst->data == src->data); + +fail: + av_buffer_unref(&dst); + av_buffer_unref(&derived); + av_buffer_unref(&src); + av_buffer_unref(&devices[1]); + av_buffer_unref(&devices[0]); + return ret < 0; +} diff --git a/tests/fate/libavutil.mak b/tests/fate/libavutil.mak index fa55ebdbe36..5b64cd6ae85 100644 --- a/tests/fate/libavutil.mak +++ b/tests/fate/libavutil.mak @@ -139,6 +139,10 @@ FATE_LIBAVUTIL += fate-hmac fate-hmac: libavutil/tests/hmac$(EXESUF) fate-hmac: CMD = run libavutil/tests/hmac$(EXESUF) +FATE_LIBAVUTIL-$(CONFIG_LIBDRM) += fate-hwframe +fate-hwframe: libavutil/tests/hwframe$(EXESUF) +fate-hwframe: CMD = run libavutil/tests/hwframe$(EXESUF) + FATE_LIBAVUTIL += fate-iamf-class fate-iamf-class: libavutil/tests/iamf$(EXESUF) fate-iamf-class: CMD = run libavutil/tests/iamf$(EXESUF) diff --git a/tests/ref/fate/hwframe b/tests/ref/fate/hwframe new file mode 100644 index 00000000000..33a1544b5e1 --- /dev/null +++ b/tests/ref/fate/hwframe @@ -0,0 +1,7 @@ +source: format=drm_prime device=drm +derive hardware mismatch: unsupported format +derive software format: unsupported format +derive valid: format=drm_prime device=drm +unmap hardware mismatch: unsupported format +unmap valid: format=drm_prime device=drm +unmap returned source: 1 -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
