Commit 25bf077615 for ffmpeg
commit 25bf0776155da64accda16cf3e1834cdaf42f3a4
Author: Julius Bairaktaris <julius@bairaktaris.de>
Date: Mon Sep 28 11:23:11 2026 +0200
avutil/hwcontext_amf: map AMF surfaces to D3D11 frames
AMF surfaces reach a D3D11 consumer only through hwdownload and a new
upload, although on an AMF device derived from D3D11VA they already are
textures on that device. Export the surface's texture as a D3D11 frame.
The mapping keeps a reference to the source frame, so it needs no unmap
callback.
Only surfaces in AMF_MEMORY_DX11 are mapped, since anything else would
need a copy; other surfaces, such as the DX12 frames that frc_amf's
DX12 engine returns, get AVERROR(ENOSYS), and hwdownload remains
available for them. The mapping also requires the AMF context to run on
the target's D3D11 device, compared by IUnknown identity, and a target
frames context with the source's sw_format and size, and a texture of
exactly that size, because a D3D11 download copies the whole texture
into staging of the frames context's size; otherwise it also returns
AVERROR(ENOSYS), as an impossible mapping. A target frames context
on a device of another type is refused first, without reading its
hardware context: hwmap creates one when the selected device is an AMF
device that is not derived from D3D11VA. The mapping is also refused for surfaces
whose planes are separate textures (yuv420p) and for surfaces on a
texture array: a wrapped array slice carries no reliable slice
index, because the index is private data on the shared texture and the
last wrapped slice overwrites it. Surfaces allocated by the AMF runtime
are single textures of exactly the frame size, including odd sizes.
hwmap does not restrict its output to the derived device, so
format=d3d11 must follow it.
Tested on Windows 11, RX 9070 XT, driver 32.0.32015.7029: framemd5 over
120 frames of d3d11va input matches the hwdownload path for vqe_amf,
sr_amf, vpp_amf (nv12 and p010, also scaled to 1366x768, 854x480 and
1278x718) and frc_amf with the DX11 engine; frc_amf with the DX12
engine is refused, and its hwdownload output is unchanged. An API test maps nv12, p010,
bgra, rgba, bgr0, x2bgr10le and rgbaf16le surfaces at 1920x1080 and
1918x1078 and reads them back through D3D11 unchanged, also after
another reference was downloaded, and checks that host surfaces,
mismatched sizes and frames on another D3D11 device are refused with
AVERROR(ENOSYS).
Signed-off-by: Julius Bairaktaris <julius@bairaktaris.de>
diff --git a/doc/APIchanges b/doc/APIchanges
index 33d895237c..d25504d346 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2026-06-23.
API changes, most recent first:
+2026-11-xx - xxxxxxxxxx - lavu 61.21.102 - hwcontext_amf.h
+ AMF frames can be mapped to D3D11 frames with av_hwframe_map().
+
2026-11-xx - xxxxxxxxxx - lavu 61.21.100 - cpu.h
Add AV_CPU_FLAG_AVXVNNI.
diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c
index 25758077b6..bc9dd07dbe 100644
--- a/libavutil/hwcontext_amf.c
+++ b/libavutil/hwcontext_amf.c
@@ -1094,6 +1094,84 @@ static void amf_unmap_frame(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
AMF_IFACE_CALL(surface1, Release);
}
+#if CONFIG_D3D11VA
+static int amf_same_d3d11_device(ID3D11Device *a, ID3D11Device *b)
+{
+ static const GUID iid_unknown = { 0x00000000, 0x0000, 0x0000, { 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 } };
+ IUnknown *unk_a, *unk_b;
+ int same;
+
+ if (a == b)
+ return 1;
+ if (!a || !b)
+ return 0;
+ if (FAILED(a->lpVtbl->QueryInterface(a, &iid_unknown, (void **)&unk_a)))
+ return 0;
+ if (FAILED(b->lpVtbl->QueryInterface(b, &iid_unknown, (void **)&unk_b))) {
+ unk_a->lpVtbl->Release(unk_a);
+ return 0;
+ }
+ same = unk_a == unk_b;
+ unk_a->lpVtbl->Release(unk_a);
+ unk_b->lpVtbl->Release(unk_b);
+ return same;
+}
+
+static int amf_map_to_d3d11(AVHWFramesContext *ctx, AVFrame *dst, const AVFrame *src)
+{
+ AVAMFDeviceContext *amf_ctx = ctx->device_ctx->hwctx;
+ AVHWFramesContext *dst_fc;
+ AVD3D11VADeviceContext *d3d11_ctx;
+ AMFSurface *surface = (AMFSurface *)src->data[0];
+ D3D11_TEXTURE2D_DESC desc;
+ ID3D11Texture2D *texture;
+ AMFPlane *plane;
+ int ret;
+
+ AMF_RETURN_IF_FALSE(ctx, surface, AVERROR(EINVAL), "Source surface is null\n");
+ if (!dst->hw_frames_ctx)
+ return AVERROR(EINVAL);
+ dst_fc = (AVHWFramesContext *)dst->hw_frames_ctx->data;
+ if (dst_fc->device_ctx->type != AV_HWDEVICE_TYPE_D3D11VA)
+ return AVERROR(ENOSYS);
+ d3d11_ctx = dst_fc->device_ctx->hwctx;
+ if (dst_fc->sw_format != ctx->sw_format ||
+ dst_fc->width != ctx->width || dst_fc->height != ctx->height)
+ return AVERROR(ENOSYS);
+ if (!amf_same_d3d11_device(AMF_IFACE_CALL(amf_ctx->context, GetDX11Device, AMF_DX11_0),
+ d3d11_ctx->device)) {
+ av_log(ctx, AV_LOG_DEBUG, "The AMF context does not use the D3D11 device of the target frames.\n");
+ return AVERROR(ENOSYS);
+ }
+ if (AMF_IFACE_CALL(surface, GetMemoryType) != AMF_MEMORY_DX11)
+ return AVERROR(ENOSYS);
+
+ plane = AMF_IFACE_CALL(surface, GetPlaneAt, 0);
+ AMF_RETURN_IF_FALSE(ctx, plane, AVERROR(EINVAL), "Surface has no plane\n");
+ texture = plane->pVtbl->GetNative(plane);
+ AMF_RETURN_IF_FALSE(ctx, texture, AVERROR(EINVAL), "Surface has no D3D11 texture\n");
+ for (int i = 1; i < AMF_IFACE_CALL(surface, GetPlanesCount); i++) {
+ plane = AMF_IFACE_CALL(surface, GetPlaneAt, i);
+ if (!plane || plane->pVtbl->GetNative(plane) != texture)
+ return AVERROR(ENOSYS);
+ }
+ texture->lpVtbl->GetDesc(texture, &desc);
+ if (desc.ArraySize > 1 ||
+ desc.Width != dst_fc->width || desc.Height != dst_fc->height)
+ return AVERROR(ENOSYS);
+
+ ret = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src, NULL, NULL);
+ if (ret < 0)
+ return ret;
+
+ dst->data[0] = (uint8_t *)texture;
+ dst->data[1] = 0;
+ dst->width = src->width;
+ dst->height = src->height;
+ return 0;
+}
+#endif
+
static int amf_map_frame(AVHWFramesContext *device_ctx, AVFrame *dst, const AVFrame *src, int flags)
{
AVAMFDeviceContext *amf_ctx = device_ctx->device_ctx->hwctx;
@@ -1107,6 +1185,10 @@ static int amf_map_frame(AVHWFramesContext *device_ctx, AVFrame *dst, const AVFr
size_t plane_count = 0;
int ret = -1;
+#if CONFIG_D3D11VA
+ if (dst->format == AV_PIX_FMT_D3D11)
+ return amf_map_to_d3d11(device_ctx, dst, src);
+#endif
if (dst->format != AV_PIX_FMT_NONE &&
av_pix_fmt_desc_get(dst->format)->flags & AV_PIX_FMT_FLAG_HWACCEL)
return AVERROR(ENOSYS);
diff --git a/libavutil/version.h b/libavutil/version.h
index 1e8c780235..f2c0f7b2a5 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -80,7 +80,7 @@
#define LIBAVUTIL_VERSION_MAJOR 61
#define LIBAVUTIL_VERSION_MINOR 21
-#define LIBAVUTIL_VERSION_MICRO 101
+#define LIBAVUTIL_VERSION_MICRO 102
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
LIBAVUTIL_VERSION_MINOR, \