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

Depends on #25149, whose three commits this is stacked on; only the last commit 
belongs to this pull request.

`amf_map_frame()` sets `dst->format` to the surface's own format before 
mapping, whatever the caller requested. `hwmap` passes the format negotiated on 
its output link, so `hwmap=mode=read,format=yuv420p` on NV12 surfaces output 
NV12 data labelled yuv420p. A destination with a hardware frames context but no 
format took the CPU path, and its frames context was dropped. The Forgejo_Fairy 
review of #25458 flagged this as pre-existing.

This takes the requested format from `dst->format`, or from the destination's 
frames context when it is unset, dispatches the D3D11 mapping on it, and 
refuses a CPU mapping to another format with `AVERROR(ENOSYS)` before `Map()`. 
`dst->format` is set only when the mapping succeeded.

Tested on Windows 11, RX 9070 XT, driver 32.0.32029.2010: 
`vpp_amf,hwmap=mode=read,format=yuv420p` on D3D11VA input gave NV12 frames as 
yuv420p (the same framemd5 as `format=nv12`), and now fails with "Function not 
implemented". `hwmap=mode=read,format=nv12`, the mapping to D3D11 frames and 
`hwdownload` give the same output as before.


>From b7583c92c5791d85ef1cf511aa5f8cfa73bb97e9 Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Mon, 28 Sep 2026 11:22:38 +0200
Subject: [PATCH 1/4] avutil/hwcontext_amf: do not map to system memory for a
 hardware target

amf_map_frame() ignores dst->format and always maps to system memory.
hwmap to a hardware format therefore passes a system memory frame down a
hardware link, and the next filter fails. Return AVERROR(ENOSYS) for a
hardware destination format. Mapping to system memory is unchanged.

Signed-off-by: Julius Bairaktaris <[email protected]>
---
 libavutil/hwcontext_amf.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c
index 7709614ebde..994411383b8 100644
--- a/libavutil/hwcontext_amf.c
+++ b/libavutil/hwcontext_amf.c
@@ -1097,6 +1097,10 @@ static int amf_map_frame(AVHWFramesContext *device_ctx, 
AVFrame *dst, const AVFr
     size_t plane_count = 0;
     int ret = -1;
 
+    if (dst->format != AV_PIX_FMT_NONE &&
+        av_pix_fmt_desc_get(dst->format)->flags & AV_PIX_FMT_FLAG_HWACCEL)
+        return AVERROR(ENOSYS);
+
     AMF_RETURN_IF_FALSE(device_ctx, surface != NULL, AVERROR(EINVAL) , "Source 
surface is null");
 
     res = AMF_IFACE_CALL(surface, QueryInterface, &guid, (void**)&surface1);
-- 
2.54.1


>From 6b5e6b5264b04cb10662f313671325b3f1f61133 Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Sat, 24 Oct 2026 12:14:12 +0200
Subject: [PATCH 2/4] avutil/hwcontext_amf: download without converting the
 source surface

amf_transfer_data_from() converted the source surface to host memory in
place. The surface is shared by every reference to the frame, so a
download through one reference moved the surface of all others to host
memory, and the runtime handed its released texture to the next
allocation. Copy the surface into a new host surface with Duplicate()
instead, under the device lock that the Convert() call held, and read
host surfaces directly.

Tested on Windows 11, RX 9070 XT, driver 32.0.32015.2008: after a
download through a second reference the surface stays AMF_MEMORY_DX11,
and its texture is no longer handed to new surfaces. 1200 frames of
vqe_amf,hwdownload take 1.47 s against 1.49 s before (median of 3).

Signed-off-by: Julius Bairaktaris <[email protected]>
---
 libavutil/hwcontext_amf.c | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c
index 994411383b8..25758077b62 100644
--- a/libavutil/hwcontext_amf.c
+++ b/libavutil/hwcontext_amf.c
@@ -504,6 +504,8 @@ static int amf_transfer_data_from(AVHWFramesContext *ctx, 
AVFrame *dst,
 {
     AMFSurface* surface = (AMFSurface*)src->data[0];
     AVAMFDeviceContext *amf_ctx = ctx->device_ctx->hwctx;
+    const AMFGuid guid = IID_AMFSurface();
+    AMFData  *data;
     AMFPlane *plane;
     uint8_t  *src_data[4];
     int       src_linesize[4];
@@ -516,10 +518,17 @@ static int amf_transfer_data_from(AVHWFramesContext *ctx, 
AVFrame *dst,
     if (src->hw_frames_ctx->data != (uint8_t *)ctx || dst->format != 
ctx->sw_format)
         return AVERROR(EINVAL);
 
-    amf_ctx->lock(amf_ctx->lock_ctx);
-    ret = surface->pVtbl->Convert(surface, AMF_MEMORY_HOST);
-    amf_ctx->unlock(amf_ctx->lock_ctx);
-    AMF_RETURN_IF_FALSE(ctx, ret == AMF_OK, AVERROR_UNKNOWN, 
"Convert(amf::AMF_MEMORY_HOST) failed with error %d\n", ret);
+    if (surface->pVtbl->GetMemoryType(surface) == AMF_MEMORY_HOST) {
+        surface->pVtbl->Acquire(surface);
+    } else {
+        amf_ctx->lock(amf_ctx->lock_ctx);
+        ret = surface->pVtbl->Duplicate(surface, AMF_MEMORY_HOST, &data);
+        amf_ctx->unlock(amf_ctx->lock_ctx);
+        AMF_RETURN_IF_FALSE(ctx, ret == AMF_OK, AVERROR_UNKNOWN, 
"Duplicate(AMF_MEMORY_HOST) failed with error %d\n", ret);
+        ret = data->pVtbl->QueryInterface(data, &guid, (void**)&surface);
+        data->pVtbl->Release(data);
+        AMF_RETURN_IF_FALSE(ctx, ret == AMF_OK, AVERROR_UNKNOWN, 
"QueryInterface(AMFSurface) failed with error %d\n", ret);
+    }
 
     planes = (int)surface->pVtbl->GetPlanesCount(surface);
     av_assert0(planes < FF_ARRAY_ELEMS(src_data));
@@ -532,6 +541,7 @@ static int amf_transfer_data_from(AVHWFramesContext *ctx, 
AVFrame *dst,
     av_image_copy2(dst->data, dst->linesize,
                    src_data, src_linesize, dst->format,
                    w, h);
+    surface->pVtbl->Release(surface);
     return 0;
 }
 
-- 
2.54.1


>From 3bbc9d295546e430b6820fd1a78d4e6d48df274f Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Mon, 28 Sep 2026 11:23:11 +0200
Subject: [PATCH 3/4] 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 <[email protected]>
---
 doc/APIchanges            |  3 ++
 libavutil/hwcontext_amf.c | 82 +++++++++++++++++++++++++++++++++++++++
 libavutil/version.h       |  2 +-
 3 files changed, 86 insertions(+), 1 deletion(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 33d895237c9..d25504d346b 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 25758077b62..bc9dd07dbe7 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 1e8c7802357..f2c0f7b2a55 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, \
-- 
2.54.1


>From 0b1b8cda5994d88e2e6f217a6aff8e2fbe72def7 Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Fri, 27 Nov 2026 12:34:15 +0100
Subject: [PATCH 4/4] avutil/hwcontext_amf: map to the requested format only

amf_map_frame() sets dst->format to the format of the surface before it
maps it, whatever format the caller asked for. hwmap passes the format
negotiated on its output link, so hwmap=mode=read,format=yuv420p on NV12
surfaces output NV12 data labelled yuv420p. A destination with a
hardware frames context but no format took the CPU path, and the
hardware frames context was dropped.

Take the requested format from dst->format, or from the frames context
of the destination when it is unset, dispatch the D3D11 mapping on it,
and refuse a CPU mapping to a format other than the surface's own with
AVERROR(ENOSYS) before mapping. dst->format is set when the mapping
succeeded.

Tested on Windows 11, RX 9070 XT, driver 32.0.32029.2010:
vpp_amf,hwmap=mode=read,format=yuv420p on D3D11VA input output NV12
frames as yuv420p before and fails with "Function not implemented" now.
hwmap=mode=read,format=nv12, the mapping to D3D11 frames and hwdownload
give the same output as before.

Reported by the Forgejo_Fairy review of #25458.

Signed-off-by: Julius Bairaktaris <[email protected]>
---
 libavutil/hwcontext_amf.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c
index bc9dd07dbe7..94736570e1f 100644
--- a/libavutil/hwcontext_amf.c
+++ b/libavutil/hwcontext_amf.c
@@ -1183,18 +1183,30 @@ static int amf_map_frame(AVHWFramesContext *device_ctx, 
AVFrame *dst, const AVFr
     const AMFGuid guid = IID_AMFSurface1();
     AMF_RESULT res = AMF_FAIL;
     size_t plane_count = 0;
+    enum AVPixelFormat format = dst->format;
+    enum AVPixelFormat sw_format;
     int ret = -1;
 
+    if (format == AV_PIX_FMT_NONE && dst->hw_frames_ctx)
+        format = ((AVHWFramesContext *)dst->hw_frames_ctx->data)->format;
 #if CONFIG_D3D11VA
-    if (dst->format == AV_PIX_FMT_D3D11)
-        return amf_map_to_d3d11(device_ctx, dst, src);
+    if (format == AV_PIX_FMT_D3D11) {
+        ret = amf_map_to_d3d11(device_ctx, dst, src);
+        if (ret >= 0)
+            dst->format = AV_PIX_FMT_D3D11;
+        return ret;
+    }
 #endif
-    if (dst->format != AV_PIX_FMT_NONE &&
-        av_pix_fmt_desc_get(dst->format)->flags & AV_PIX_FMT_FLAG_HWACCEL)
+    if (format != AV_PIX_FMT_NONE &&
+        av_pix_fmt_desc_get(format)->flags & AV_PIX_FMT_FLAG_HWACCEL)
         return AVERROR(ENOSYS);
 
     AMF_RETURN_IF_FALSE(device_ctx, surface != NULL, AVERROR(EINVAL) , "Source 
surface is null");
 
+    sw_format = av_amf_to_av_format(AMF_IFACE_CALL(surface, GetFormat));
+    if (format != AV_PIX_FMT_NONE && format != sw_format)
+        return AVERROR(ENOSYS);
+
     res = AMF_IFACE_CALL(surface, QueryInterface, &guid, (void**)&surface1);
     AMF_RETURN_IF_FALSE(device_ctx, res == AMF_OK, AVERROR_UNKNOWN, 
"QueryInterface(AMFSurface1) failed with error %d\n", res);
 
@@ -1211,8 +1223,6 @@ static int amf_map_frame(AVHWFramesContext *device_ctx, 
AVFrame *dst, const AVFr
     map = av_malloc_array(sizeof(uint8_t *), plane_count);
     AMF_GOTO_FAIL_IF_FALSE(device_ctx, map, AVERROR(ENOMEM), "Unable to 
allocate mapping buffer\n");
 
-    dst->format = av_amf_to_av_format(AMF_IFACE_CALL(surface, GetFormat));
-
     amf_ctx->lock(amf_ctx->lock_ctx);
     res = AMF_IFACE_CALL(surface1, Map, amf_mem_flags, plane_count, linesizes, 
(void**)map);
     amf_ctx->unlock(amf_ctx->lock_ctx);
@@ -1221,6 +1231,7 @@ static int amf_map_frame(AVHWFramesContext *device_ctx, 
AVFrame *dst, const AVFr
     ret = ff_hwframe_map_create(src->hw_frames_ctx, dst, src, 
&amf_unmap_frame, surface1);
     AMF_GOTO_FAIL_IF_FALSE(device_ctx, ret >= 0, ret, "ff_hwframe_map_create 
failed with error %d\n", ret);
 
+    dst->format = sw_format;
     dst->width  = src->width;
     dst->height = src->height;
     av_frame_copy_props(dst, src);
-- 
2.54.1

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

Reply via email to