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

Depends on #23019 and #25458 (which is stacked on #25149), whose 10 commits 
this is stacked on; only the last 2 commits belong to this pull request.

#23019 lets an AMF device be derived from a Vulkan device, and #25458 makes the 
AMF filters take the device lock around their AMF calls, which on a 
Vulkan-derived device is the queue lock from #23019. With both in place, this 
lets the AMF filters take Vulkan frames directly, so a Vulkan decode chain 
stays on the GPU through `vqe_amf`, `sr_amf`, `vpp_amf` and `frc_amf`. 
@DmitriiGershenkop, these are the two filter commits I mentioned in #23019, 
rebased onto your final revision.

- `avfilter/vf_amf_common: support Vulkan frames`: the filters accept 
`AV_PIX_FMT_VULKAN` and output Vulkan frames when the filter device is a Vulkan 
device. AMF waits on the frame's timeline semaphore value and signals the next 
one, which is claimed in the `AVVkFrame` under the frame lock. Input is copied 
on AMF's queue into an AMF surface, because the components keep input surfaces 
across calls and do not reliably report the layout they leave a wrapped image 
in.
- `avfilter/vf_frc_amf: run on Vulkan frames`: `frc_amf` accepts Vulkan frames 
and selects its Vulkan engine on a Vulkan context when no engine is given.

```
ffmpeg -init_hw_device 
vulkan=vk:0,instance_extensions=VK_KHR_get_physical_device_properties2+VK_KHR_surface,device_extensions=VK_KHR_swapchain
 \
       -filter_hw_device vk -hwaccel vulkan -hwaccel_device vk 
-hwaccel_output_format vulkan -i input.mp4 \
       -vf vqe_amf,frc_amf,hwdownload,format=nv12 output.y4m
```

Tested on Windows 11, RX 9070 XT, driver 32.0.32029.2010, AMF runtime 1.5.3.0, 
with H.264 1080p decoded by the Vulkan hwaccel to Vulkan frames, against the 
same filter on D3D11VA input:

| filter | result |
|---|---|
| `vpp_amf=w=1280:h=720` | identical frames |
| `frc_amf` | identical frames (119 from 60) |
| `vqe_amf` | 90 dB luma PSNR against the D3D11 output, chroma identical |
| `sr_amf=w=2560:h=1440` | 62 dB luma PSNR against the D3D11 output, chroma 
identical |

`vqe_amf,frc_amf` runs as a chain, and `vqe_amf,scale_vulkan` runs on the 
filter's Vulkan output. Both commits build on their own.

Not covered: Linux, and a run under the Vulkan validation layer with other 
Vulkan filters submitting concurrently (the scenario from #23019); I have only 
run the pipelines above.

Notes on the stack: where #23019 and #25458 overlap in `hwcontext_amf.c`, the 
download path keeps #25458's `Duplicate()` with a single lock/unlock pair, and 
#25458's commit that frees the default lock is dropped because #23019 already 
contains that fix.



>From b3e50a3e22ad143e3004e79a9f5816a861331d6b Mon Sep 17 00:00:00 2001
From: Dmitrii Gershenkop <[email protected]>
Date: Thu, 19 Nov 2026 13:30:58 +0100
Subject: [PATCH 01/12] avutil/hwcontext_amf: add derivation from a Vulkan
 device

Lets AMF share the caller's Vulkan device. The Vulkan extensions AMF
needs are documented and listed when initialization fails. The derived
device's lock takes all queue locks of the source device and is held
around the AMF calls in hwcontext_amf that can submit to them (init,
download, mapping and termination), so these are serialized with
FFmpeg's own submissions. Also fixes the default lock never being freed.

Assisted-by: Claude Opus 5.5
Co-authored-by: Julius Bairaktaris <[email protected]>
---
 Changelog                 |   1 +
 doc/general_contents.texi |  16 ++++
 libavutil/hwcontext_amf.c | 176 +++++++++++++++++++++++++++++++++++++-
 libavutil/version.h       |   2 +-
 4 files changed, 190 insertions(+), 5 deletions(-)

diff --git a/Changelog b/Changelog
index 0e5578ac28c..856aa095908 100644
--- a/Changelog
+++ b/Changelog
@@ -7,6 +7,7 @@ version <next>:
 - AVS+ (AVS1-P16, Broadcasting profile) support in the Chinese AVS decoder
 - PCM PAMF decoder
 - Super Audio CD (SACD) demuxer
+- Added the ability to derive an AMF hwdevice from a Vulkan hwdevice
 
 version 9.1:
 - ASTC image encoding and decoding via libastcenc, with .astc and KTX 1.0 
muxers and demuxers
diff --git a/doc/general_contents.texi b/doc/general_contents.texi
index ffbe33be0b0..8f7e07becb3 100644
--- a/doc/general_contents.texi
+++ b/doc/general_contents.texi
@@ -73,6 +73,22 @@ Initialization of amf encoder occurs in this order:
 2) trying to initialize through dx9(only windows)
 3) trying to initialize through vulkan
 
+An AMF device can also be derived from an existing Vulkan device. AMF requires 
the
+@code{VK_KHR_get_physical_device_properties2} and @code{VK_KHR_surface} 
instance
+extensions and the @code{VK_KHR_swapchain} device extension, which are not 
enabled
+by default; enable them through the Vulkan device options:
+@example
+ffmpeg -init_hw_device 
vulkan=vk,instance_extensions=VK_KHR_get_physical_device_properties2+VK_KHR_surface,device_extensions=VK_KHR_swapchain
 \
+       -init_hw_device amf=amf@@vk ...
+@end example
+If the Vulkan device uses custom queue lock callbacks, they must allow
+holding the locks of several queues at the same time; a single
+non-recursive mutex shared between queues cannot be used.
+Some AMF runtimes cannot use Vulkan devices whose queues were created with
+nonzero creation flags, such as internally synchronized queues; Vulkan devices
+with reduced queue counts (e.g. created with @code{limit_queues}) should not
+be used for derivation.
+
 To use h.264(AMD VCE) encoder on linux amdgru-pro version 19.20+ and 
amf-amdgpu-pro
 package(amdgru-pro contains, but does not install automatically) are required.
 
diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c
index 7f47df5b59d..7709614ebde 100644
--- a/libavutil/hwcontext_amf.c
+++ b/libavutil/hwcontext_amf.c
@@ -25,6 +25,7 @@
 #include "hwcontext_amf.h"
 #include "hwcontext_internal.h"
 #include "hwcontext_amf_internal.h"
+#include "internal.h"
 
 #include "libavutil/thread.h"
 #include "libavutil/avassert.h"
@@ -33,6 +34,7 @@
 #include <AMF/core/Trace.h>
 
 #if CONFIG_VULKAN
+#include <AMF/core/VulkanAMF.h>
 #include "hwcontext_vulkan.h"
 #endif
 #if CONFIG_D3D11VA
@@ -52,6 +54,13 @@
 #endif
 #define FFMPEG_AMF_WRITER_ID L"ffmpeg_amf"
 
+typedef struct AMFDevicePriv {
+    AVAMFDeviceContext p;
+#if CONFIG_VULKAN
+    AMFVulkanDevice    vk_dev;
+#endif
+} AMFDevicePriv;
+
 static void amf_lock_default(void *opaque)
 {
     ff_mutex_lock((AVMutex*)opaque);
@@ -494,6 +503,7 @@ static int amf_transfer_data_from(AVHWFramesContext *ctx, 
AVFrame *dst,
                                     const AVFrame *src)
 {
     AMFSurface* surface = (AMFSurface*)src->data[0];
+    AVAMFDeviceContext *amf_ctx = ctx->device_ctx->hwctx;
     AMFPlane *plane;
     uint8_t  *src_data[4];
     int       src_linesize[4];
@@ -506,8 +516,10 @@ 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_RETURN_IF_FALSE(ctx, ret == AMF_OK, AVERROR_UNKNOWN, 
"Convert(amf::AMF_MEMORY_HOST) failed with error %d\n", AVERROR_UNKNOWN);
+    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);
 
     planes = (int)surface->pVtbl->GetPlanesCount(surface);
     av_assert0(planes < FF_ARRAY_ELEMS(src_data));
@@ -532,8 +544,12 @@ static void amf_device_uninit(AVHWDeviceContext 
*device_ctx)
     AMFTrace           *trace;
 
     if (amf_ctx->context) {
+        if (amf_ctx->lock)
+            amf_ctx->lock(amf_ctx->lock_ctx);
         amf_ctx->context->pVtbl->Terminate(amf_ctx->context);
         amf_ctx->context->pVtbl->Release(amf_ctx->context);
+        if (amf_ctx->unlock)
+            amf_ctx->unlock(amf_ctx->lock_ctx);
         amf_ctx->context = NULL;
     }
 
@@ -552,7 +568,7 @@ static void amf_device_uninit(AVHWDeviceContext *device_ctx)
         amf_writer_free(amf_ctx->trace_writer);
     }
 
-    if (amf_ctx->lock_ctx == amf_lock_default) {
+    if (amf_ctx->lock == amf_lock_default) {
         ff_mutex_destroy((AVMutex*)amf_ctx->lock_ctx);
         av_freep(&amf_ctx->lock_ctx);
         amf_ctx->lock = NULL;
@@ -878,6 +894,148 @@ static int amf_init_from_d3d12_device(AVAMFDeviceContext* 
amf_ctx, AVHWDeviceCon
 }
 #endif
 
+#if CONFIG_VULKAN
+static int amf_vk_ext_enabled(const char * const *exts, int nb_exts, const 
char *name)
+{
+    for (int i = 0; i < nb_exts; i++)
+        if (!strcmp(exts[i], name))
+            return 1;
+    return 0;
+}
+
+// AMF loads vkGetPhysicalDeviceProperties2KHR and the VK_KHR_swapchain entry 
points (swapchain depends on VK_KHR_surface).
+static void amf_vulkan_log_missing_exts(AVHWDeviceContext *child_device_ctx, 
const AVVulkanDeviceContext *hwctx)
+{
+    static const struct {
+        const char *name;
+        int dev;
+    } required_exts[] = {
+        { VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME, 0 },
+        { VK_KHR_SURFACE_EXTENSION_NAME,                          0 },
+        { VK_KHR_SWAPCHAIN_EXTENSION_NAME,                        1 },
+    };
+    int missing = 0;
+
+    for (int i = 0; i < FF_ARRAY_ELEMS(required_exts); i++) {
+        int enabled = required_exts[i].dev ?
+            amf_vk_ext_enabled(hwctx->enabled_dev_extensions,  
hwctx->nb_enabled_dev_extensions,  required_exts[i].name) :
+            amf_vk_ext_enabled(hwctx->enabled_inst_extensions, 
hwctx->nb_enabled_inst_extensions, required_exts[i].name);
+        if (!enabled) {
+            av_log(child_device_ctx, AV_LOG_ERROR, "Vulkan %s extension %s is 
not enabled.\n",
+                   required_exts[i].dev ? "device" : "instance", 
required_exts[i].name);
+            missing = 1;
+        }
+    }
+
+    if (missing)
+        av_log(child_device_ctx, AV_LOG_ERROR, "AMF requires them, create the 
Vulkan device with instance_extensions="
+               VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME "+" 
VK_KHR_SURFACE_EXTENSION_NAME
+               ",device_extensions=" VK_KHR_SWAPCHAIN_EXTENSION_NAME ".\n");
+}
+
+#if FF_API_VULKAN_SYNC_QUEUES
+// qf may list a family more than once and in any order. Queues are walked 
once each, in ascending family and
+// queue index order, so that queue locks shared with other users are always 
taken in the same order.
+static int amf_vk_nb_families(const AVVulkanDeviceContext *hwctx)
+{
+    int nb = 0;
+    for (int i = 0; i < hwctx->nb_qf; i++)
+        nb = FFMAX(nb, hwctx->qf[i].idx + 1);
+    return nb;
+}
+
+static int amf_vk_family_queues(const AVVulkanDeviceContext *hwctx, int family)
+{
+    int num = 0;
+    for (int i = 0; i < hwctx->nb_qf; i++)
+        if (hwctx->qf[i].idx == family)
+            num = FFMAX(num, hwctx->qf[i].num);
+    return num;
+}
+
+// AMF submits to the queues of the Vulkan device it was derived from, so its 
lock takes all of them.
+// The queue callbacks must allow holding several queue locks at once and need 
not be recursive, so no FFmpeg
+// Vulkan work may be submitted on this thread while they are held.
+static void amf_lock_vulkan(void *opaque)
+{
+    AVHWDeviceContext *vk_ctx = opaque;
+    AVVulkanDeviceContext *hwctx = vk_ctx->hwctx;
+    int nb_families = amf_vk_nb_families(hwctx);
+
+FF_DISABLE_DEPRECATION_WARNINGS
+    for (int i = 0; i < nb_families; i++) {
+        int nb_queues = amf_vk_family_queues(hwctx, i);
+        for (int j = 0; j < nb_queues; j++)
+            hwctx->lock_queue(vk_ctx, i, j);
+    }
+FF_ENABLE_DEPRECATION_WARNINGS
+}
+
+static void amf_unlock_vulkan(void *opaque)
+{
+    AVHWDeviceContext *vk_ctx = opaque;
+    AVVulkanDeviceContext *hwctx = vk_ctx->hwctx;
+    int nb_families = amf_vk_nb_families(hwctx);
+
+FF_DISABLE_DEPRECATION_WARNINGS
+    for (int i = 0; i < nb_families; i++) {
+        int nb_queues = amf_vk_family_queues(hwctx, i);
+        for (int j = 0; j < nb_queues; j++)
+            hwctx->unlock_queue(vk_ctx, i, j);
+    }
+FF_ENABLE_DEPRECATION_WARNINGS
+}
+#endif
+
+static int amf_init_from_vulkan_device(AMFDevicePriv *priv, AVHWDeviceContext 
*child_device_ctx)
+{
+    AVVulkanDeviceContext *hwctx = child_device_ctx->hwctx;
+    AMFVulkanDevice *vk_dev = &priv->vk_dev;
+    AMFContext1 *context1 = NULL;
+    AMFGuid guid = IID_AMFContext1();
+    AMF_RESULT res;
+
+    res = AMF_IFACE_CALL(priv->p.context, QueryInterface, &guid, 
(void**)&context1);
+    AMF_RETURN_IF_FALSE(child_device_ctx, res == AMF_OK, AVERROR_UNKNOWN, 
"QueryInterface(AMFContext1) failed with error %d\n", res);
+
+    vk_dev->cbSizeof        = sizeof(*vk_dev);
+    vk_dev->pNext           = NULL;
+    vk_dev->hInstance       = hwctx->inst;
+    vk_dev->hPhysicalDevice = hwctx->phys_dev;
+    vk_dev->hDevice         = hwctx->act_dev;
+
+    AMF_ASSIGN_PROPERTY_BOOL(res, context1, 
AMF_CONTEXT_VULKAN_USE_TIMELINE_SEMAPHORES, 1);
+    if (res != AMF_OK)
+        av_log(child_device_ctx, AV_LOG_WARNING, "Failed to enable Vulkan 
timeline semaphores on the AMF context: %d.\n", res);
+
+#if FF_API_VULKAN_SYNC_QUEUES
+    priv->p.lock     = amf_lock_vulkan;
+    priv->p.unlock   = amf_unlock_vulkan;
+    priv->p.lock_ctx = child_device_ctx;
+#endif
+    if (priv->p.lock)
+        priv->p.lock(priv->p.lock_ctx);
+    res = AMF_IFACE_CALL(context1, InitVulkan, vk_dev);
+    if (priv->p.unlock)
+        priv->p.unlock(priv->p.lock_ctx);
+    AMF_IFACE_CALL(context1, Release);
+    if (res != AMF_OK && res != AMF_ALREADY_INITIALIZED) {
+        if (res == AMF_NOT_SUPPORTED)
+            av_log(child_device_ctx, AV_LOG_ERROR, "AMF via Vulkan is not 
supported on the given device.\n");
+        else
+            av_log(child_device_ctx, AV_LOG_ERROR, "AMF failed to initialise 
on the given Vulkan device: %d.\n", res);
+
+        amf_vulkan_log_missing_exts(child_device_ctx, hwctx);
+        if (hwctx->queue_flags)
+            av_log(child_device_ctx, AV_LOG_ERROR, "The Vulkan queues were 
created with flags 0x%x (e.g. internally "
+                   "synchronized queues), which some AMF runtimes cannot 
use.\n", hwctx->queue_flags);
+
+        return AVERROR(ENODEV);
+    }
+    av_log(child_device_ctx, AV_LOG_INFO, "AMF via Vulkan.\n");
+    return 0;
+}
+#endif
 
 static int amf_device_derive(AVHWDeviceContext *device_ctx,
                               AVHWDeviceContext *child_device_ctx, 
AVDictionary *opts,
@@ -904,6 +1062,10 @@ static int amf_device_derive(AVHWDeviceContext 
*device_ctx,
 #if CONFIG_D3D12VA
     case AV_HWDEVICE_TYPE_D3D12VA:
         return amf_init_from_d3d12_device(amf_ctx, child_device_ctx);
+#endif
+#if CONFIG_VULKAN
+    case AV_HWDEVICE_TYPE_VULKAN:
+        return amf_init_from_vulkan_device(device_ctx->hwctx, 
child_device_ctx);
 #endif
     default:
         av_log(child_device_ctx, AV_LOG_ERROR, "AMF initialisation from a %s 
device is not supported.\n",
@@ -912,15 +1074,19 @@ static int amf_device_derive(AVHWDeviceContext 
*device_ctx,
     }
 }
 
-static void amf_unmap_frame(av_unused AVHWFramesContext *unused, 
HWMapDescriptor *hwmap)
+static void amf_unmap_frame(AVHWFramesContext *ctx, HWMapDescriptor *hwmap)
 {
+    AVAMFDeviceContext *amf_ctx = ctx->device_ctx->hwctx;
     AMFSurface1 *surface1 = hwmap->priv;
+    amf_ctx->lock(amf_ctx->lock_ctx);
     AMF_IFACE_CALL(surface1, Unmap);
+    amf_ctx->unlock(amf_ctx->lock_ctx);
     AMF_IFACE_CALL(surface1, Release);
 }
 
 static int amf_map_frame(AVHWFramesContext *device_ctx, AVFrame *dst, const 
AVFrame *src, int flags)
 {
+    AVAMFDeviceContext *amf_ctx = device_ctx->device_ctx->hwctx;
     AMFSurface *surface = (AMFSurface *)src->data[0];
     AMFSurface1 *surface1 = NULL;
     size_t *linesizes = NULL;
@@ -951,7 +1117,9 @@ static int amf_map_frame(AVHWFramesContext *device_ctx, 
AVFrame *dst, const AVFr
 
     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);
     AMF_GOTO_FAIL_IF_FALSE(device_ctx, res == AMF_OK, AVERROR_UNKNOWN, 
"AMFSurface1->Map() failed with error %d\n", res);
 
     ret = ff_hwframe_map_create(src->hw_frames_ctx, dst, src, 
&amf_unmap_frame, surface1);
@@ -987,7 +1155,7 @@ const HWContextType ff_hwcontext_type_amf = {
     .type                 = AV_HWDEVICE_TYPE_AMF,
     .name                 = "AMF",
 
-    .device_hwctx_size    = sizeof(AVAMFDeviceContext),
+    .device_hwctx_size    = sizeof(AMFDevicePriv),
     .frames_hwctx_size    = sizeof(AMFFramesContext),
 
     .device_create        = amf_device_create,
diff --git a/libavutil/version.h b/libavutil/version.h
index 3a39a0b47cb..1e8c7802357 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 100
+#define LIBAVUTIL_VERSION_MICRO 101
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
                                                LIBAVUTIL_VERSION_MINOR, \
-- 
2.54.1


>From 960a6bb390b7b10375e40d8d997adaa5b1e606e7 Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Mon, 28 Sep 2026 11:22:38 +0200
Subject: [PATCH 02/12] 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 914d058f953a9491446561363582fafb5cac319d Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Sat, 24 Oct 2026 12:14:12 +0200
Subject: [PATCH 03/12] 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, 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 4b170f83f9993afcdbc16ab48e6720e5bceb5f62 Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Mon, 28 Sep 2026 11:23:11 +0200
Subject: [PATCH 04/12] 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 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)

diff --git a/doc/APIchanges b/doc/APIchanges
index 3eac08a0542..44ed4d48e05 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.101 - 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);
-- 
2.54.1


>From 4dc0c70937d2f4796f9f1fd8bae55f718d300269 Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Sun, 1 Nov 2026 12:55:00 +0100
Subject: [PATCH 05/12] avcodec/amfenc: lock input resubmission

ff_amf_receive_packet() resubmits a surface after AMF_INPUT_FULL with
SubmitInput() outside the device lock, while every other SubmitInput()
of the encoders takes it. On a device that other code also uses, that
call can interleave with the other user's work.

Take the device lock around the resubmission, as the regular submission
does, and release it before waiting or querying output.

Reported by the Forgejo_Fairy review of #25458.

Tested on Windows 11, RX 9070 XT, driver 32.0.32015.7029: h264_amf,
hevc_amf and av1_amf on d3d11va input give unchanged output.

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

diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
index 99b48a279b7..27b32a90813 100644
--- a/libavcodec/amfenc.c
+++ b/libavcodec/amfenc.c
@@ -744,8 +744,14 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket 
*avpkt)
     } else {
         if(surface) {
             // resubmit surface
+            AVHWDeviceContext  *hw_device_ctx  = 
(AVHWDeviceContext*)ctx->device_ctx_ref->data;
+            AVAMFDeviceContext *amf_device_ctx = hw_device_ctx->hwctx;
             do {
+                if (amf_device_ctx->lock)
+                    amf_device_ctx->lock(amf_device_ctx->lock_ctx);
                 res = ctx->encoder->pVtbl->SubmitInput(ctx->encoder, 
(AMFData*)surface);
+                if (amf_device_ctx->unlock)
+                    amf_device_ctx->unlock(amf_device_ctx->lock_ctx);
                 if (res != AMF_INPUT_FULL)
                     break;
 
-- 
2.54.1


>From fe0b91115eba5b0ff0d1eac86fa2f82f0808f3ac Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Tue, 27 Oct 2026 16:02:31 +0100
Subject: [PATCH 06/12] avutil/hwcontext_amf: take the source device's lock on
 a derived device

An AMF device derived from another hardware device runs AMF on that
device, but its lock defaults to a private mutex that only the AMF
filters and encoders take. AMF's calls are therefore not serialized with
FFmpeg's own users of the source device. For D3D11VA, those users hold
the AVD3D11VADeviceContext lock around their device_context and
video_context calls, as that header requires. For D3D12VA, the callbacks
serialize staging transfers and other users of the callbacks, but do
not generally serialize decoder command submissions.

Make the AMF lock of a device derived from a D3D11VA or D3D12VA device
take the lock of the device context it was derived from. The source
device outlives the derived one, so the lock stays valid until
amf_device_uninit(). The lock is an AMFSourceLock that wraps the source
device's callbacks, so that other device types and per-device
synchronization can be added to it. Hold the lock around InitDX11(),
InitDX12(), the context's Terminate(), and in the download and CPU map
paths, which run AMF on the device. Document the lock fields, which were 
undocumented.

The lock of the source device is required to be recursive, so code that
takes it again while holding the AMF lock does not deadlock.

Tested on Windows 11, RX 9070 XT, driver 32.0.32015.7029: framemd5 of
vqe_amf, sr_amf, vpp_amf and frc_amf on d3d11va NV12 and P010 input and
of h264_amf, hevc_amf and av1_amf on d3d11va input is unchanged, as is
the output of h264_amf, hevc_amf and vpp_amf on frames uploaded to an
AMF device derived from a D3D12VA device. hwcontext_amf.c also builds
without warnings for Linux, without D3D11VA and D3D12VA.

Signed-off-by: Julius Bairaktaris <[email protected]>
---
 doc/APIchanges            |  5 ++++
 libavutil/hwcontext_amf.c | 57 +++++++++++++++++++++++++++++++++++++++
 libavutil/hwcontext_amf.h | 10 +++++++
 libavutil/version.h       |  2 +-
 4 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 44ed4d48e05..dcc99982455 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,11 @@ 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
+  Document AVAMFDeviceContext.lock, .unlock and .lock_ctx. An AMF device
+  derived from a D3D11VA or D3D12VA device now takes the lock of that
+  device context.
+
 2026-11-xx - xxxxxxxxxx - lavu 61.21.101 - hwcontext_amf.h
   AMF frames can be mapped to D3D11 frames with av_hwframe_map().
 
diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c
index bc9dd07dbe7..bbf5a35b9bd 100644
--- a/libavutil/hwcontext_amf.c
+++ b/libavutil/hwcontext_amf.c
@@ -71,6 +71,48 @@ static void amf_unlock_default(void *opaque)
     ff_mutex_unlock((AVMutex*)opaque);
 }
 
+#if CONFIG_D3D11VA || CONFIG_D3D12VA
+typedef struct AMFSourceLock {
+    void (*lock)(void *lock_ctx);
+    void (*unlock)(void *lock_ctx);
+    void *lock_ctx;
+} AMFSourceLock;
+
+static void amf_lock_source(void *opaque)
+{
+    AMFSourceLock *lock = opaque;
+    lock->lock(lock->lock_ctx);
+}
+
+static void amf_unlock_source(void *opaque)
+{
+    AMFSourceLock *lock = opaque;
+    lock->unlock(lock->lock_ctx);
+}
+
+static void amf_free_source_lock(AMFSourceLock **plock)
+{
+    av_freep(plock);
+}
+
+static AMFSourceLock *amf_create_source_lock(AVAMFDeviceContext *amf_ctx, void 
(*lock_cb)(void *),
+                                             void (*unlock_cb)(void *), void 
*lock_ctx)
+{
+    AMFSourceLock *lock = av_mallocz(sizeof(*lock));
+    if (!lock)
+        return NULL;
+
+    lock->lock        = lock_cb;
+    lock->unlock      = unlock_cb;
+    lock->lock_ctx    = lock_ctx;
+    amf_ctx->lock     = amf_lock_source;
+    amf_ctx->unlock   = amf_unlock_source;
+    amf_ctx->lock_ctx = lock;
+    return lock;
+}
+#endif
+
+
 typedef struct AmfTraceWriter {
     AMFTraceWriterVtbl *vtblp;
     void               *avctx;
@@ -584,6 +626,13 @@ static void amf_device_uninit(AVHWDeviceContext 
*device_ctx)
         amf_ctx->lock = NULL;
         amf_ctx->unlock = NULL;
     }
+#if CONFIG_D3D11VA || CONFIG_D3D12VA
+    if (amf_ctx->lock == amf_lock_source) {
+        amf_free_source_lock((AMFSourceLock **)&amf_ctx->lock_ctx);
+        amf_ctx->lock = NULL;
+        amf_ctx->unlock = NULL;
+    }
+#endif
 
     amf_ctx->version = 0;
 }
@@ -867,7 +916,11 @@ static int amf_init_from_d3d11_device(AVAMFDeviceContext* 
amf_ctx, AVHWDeviceCon
 {
     AMF_RESULT res;
     AVD3D11VADeviceContext *hwctx = child_device_ctx->hwctx;
+    if (!amf_create_source_lock(amf_ctx, hwctx->lock, hwctx->unlock, 
hwctx->lock_ctx))
+        return AVERROR(ENOMEM);
+    amf_ctx->lock(amf_ctx->lock_ctx);
     res = amf_ctx->context->pVtbl->InitDX11(amf_ctx->context, hwctx->device, 
AMF_DX11_1);
+    amf_ctx->unlock(amf_ctx->lock_ctx);
     if (res != AMF_OK && res != AMF_ALREADY_INITIALIZED) {
         if (res == AMF_NOT_SUPPORTED)
             av_log(child_device_ctx, AV_LOG_ERROR, "AMF via D3D11 is not 
supported on the given device.\n");
@@ -888,9 +941,13 @@ static int amf_init_from_d3d12_device(AVAMFDeviceContext* 
amf_ctx, AVHWDeviceCon
     AMF_RESULT res;
     AMFContext2 *context2 = NULL;
     AMFGuid guid = IID_AMFContext2();
+    if (!amf_create_source_lock(amf_ctx, hwctx->lock, hwctx->unlock, 
hwctx->lock_ctx))
+        return AVERROR(ENOMEM);
     res = amf_ctx->context->pVtbl->QueryInterface(amf_ctx->context, &guid, 
(void**)&context2);
     AMF_RETURN_IF_FALSE(child_device_ctx, res == AMF_OK, AVERROR_UNKNOWN, 
"CreateContext2() failed with error %d\n", res);
+    amf_ctx->lock(amf_ctx->lock_ctx);
     res = context2->pVtbl->InitDX12(context2, hwctx->device, AMF_DX12);
+    amf_ctx->unlock(amf_ctx->lock_ctx);
     context2->pVtbl->Release(context2);
     if (res != AMF_OK && res != AMF_ALREADY_INITIALIZED) {
         if (res == AMF_NOT_SUPPORTED)
diff --git a/libavutil/hwcontext_amf.h b/libavutil/hwcontext_amf.h
index d2bbb418911..5fe240a9e11 100644
--- a/libavutil/hwcontext_amf.h
+++ b/libavutil/hwcontext_amf.h
@@ -41,6 +41,16 @@ typedef struct AVAMFDeviceContext {
     AMFContext         *context;
     AMF_MEMORY_TYPE     memory_type;
 
+    /**
+     * Callbacks for locking. They are held around AMF calls that use the
+     * device, so that other code sharing the device can exclude them.
+     * Callers must not assume these callbacks are recursive.
+     *
+     * If unset on init, an internal mutex is used, which only serializes
+     * the callers of these callbacks. A device derived from a D3D11VA or
+     * D3D12VA device instead takes the lock of that device context, so that
+     * AMF's calls are serialized with the other users of that lock.
+     */
     void (*lock)(void *lock_ctx);
     void (*unlock)(void *lock_ctx);
     void *lock_ctx;
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 d6aa7307042998583c611932e41245377b05a8d7 Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Tue, 27 Oct 2026 16:02:54 +0100
Subject: [PATCH 07/12] avutil/hwcontext_amf: give AMF its own D3D11 context
 state

An AMF device derived from a D3D11VA device runs AMF on that device's
immediate context, which the other users of the device share. The
AVD3D11VADeviceContext lock only excludes code that takes it; an
application that renders on the device without that lock can change the
pipeline state AMF relies on between AMF's calls. An application that
renders on the device it gave to the AMF filters, such as mpv with HDR
content and frc_amf, sees AMF_DIRECTX_FAILED followed by a GPU engine
timeout.

AMF does all its work on the device synchronously inside its API calls,
which I checked by hooking the D3D11 immediate context. So, if the
device supports D3D11.1 and is not single-threaded, enable the device's
multithread protection, as hwcontext_d3d11va does for the devices it
creates, so that every call on the device takes its multithread lock.
Make the source lock of a D3D11 device also enter that lock and switch
the immediate context to a device context state of AMF's own, and switch
back on unlock. AMF then keeps its state between calls and neither sees
nor changes the application's graphics pipeline state, and the
application does not need to take a lock itself. The D3D11VA lock is
taken first, in the order in which FFmpeg's D3D11VA code already
acquires the two. Without D3D11.1, on a single-threaded device, or
without ID3D10Multithread, the D3D11VA lock alone is used.

Video context state and asynchronous queries are not part of a device
context state, as the documentation of the lock fields says.

Tested on Windows 11, RX 9070 XT, driver 32.0.32015.7029: framemd5 of
vqe_amf, sr_amf, vpp_amf and frc_amf on d3d11va NV12 and P010 input,
and of vqe_amf on software input, is unchanged, as is the output of
h264_amf, hevc_amf and av1_amf on d3d11va input and of hevc_amf on
uploaded BGRA. The encoders make no immediate context calls in Init(),
QueryOutput(), Drain() or Terminate(), which the hook also showed. mpv,
without a change of its own, plays HDR10 1080p60 through frc_amf at 120
fps without dropped frames in 4 runs, where it failed within a second
before.

Signed-off-by: Julius Bairaktaris <[email protected]>
---
 doc/APIchanges            |  5 ++-
 libavutil/hwcontext_amf.c | 78 ++++++++++++++++++++++++++++++++++++++-
 libavutil/hwcontext_amf.h |  9 ++++-
 3 files changed, 89 insertions(+), 3 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index dcc99982455..11a252db11d 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -5,7 +5,10 @@ API changes, most recent first:
 2026-11-xx - xxxxxxxxxx - lavu 61.21.102 - hwcontext_amf.h
   Document AVAMFDeviceContext.lock, .unlock and .lock_ctx. An AMF device
   derived from a D3D11VA or D3D12VA device now takes the lock of that
-  device context.
+  device context. If a source D3D11 device supports D3D11.1 and is not
+  single-threaded, the lock also enables the device's multithread
+  protection and runs AMF in a device context state of its own, under the
+  D3D11 device's multithread lock.
 
 2026-11-xx - xxxxxxxxxx - lavu 61.21.101 - hwcontext_amf.h
   AMF frames can be mapped to D3D11 frames with av_hwframe_map().
diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c
index bbf5a35b9bd..85a4571ccc0 100644
--- a/libavutil/hwcontext_amf.c
+++ b/libavutil/hwcontext_amf.c
@@ -39,6 +39,7 @@
 #endif
 #if CONFIG_D3D11VA
 #include "libavutil/hwcontext_d3d11va.h"
+#include <d3d11_1.h>
 #endif
 #if CONFIG_D3D12VA
 #include "libavutil/hwcontext_d3d12va.h"
@@ -76,22 +77,57 @@ typedef struct AMFSourceLock {
     void (*lock)(void *lock_ctx);
     void (*unlock)(void *lock_ctx);
     void *lock_ctx;
+#if CONFIG_D3D11VA
+    ID3D10Multithread      *multithread;
+    ID3D11DeviceContext1   *context;
+    ID3DDeviceContextState *state;
+    ID3DDeviceContextState *prev_state;
+    int                     depth;
+#endif
 } AMFSourceLock;
 
 static void amf_lock_source(void *opaque)
 {
     AMFSourceLock *lock = opaque;
     lock->lock(lock->lock_ctx);
+#if CONFIG_D3D11VA
+    if (lock->state) {
+        lock->multithread->lpVtbl->Enter(lock->multithread);
+        if (!lock->depth++)
+            lock->context->lpVtbl->SwapDeviceContextState(lock->context, 
lock->state, &lock->prev_state);
+    }
+#endif
 }
 
 static void amf_unlock_source(void *opaque)
 {
     AMFSourceLock *lock = opaque;
+#if CONFIG_D3D11VA
+    if (lock->state) {
+        if (!--lock->depth) {
+            lock->context->lpVtbl->SwapDeviceContextState(lock->context, 
lock->prev_state, NULL);
+            lock->prev_state->lpVtbl->Release(lock->prev_state);
+            lock->prev_state = NULL;
+        }
+        lock->multithread->lpVtbl->Leave(lock->multithread);
+    }
+#endif
     lock->unlock(lock->lock_ctx);
 }
 
 static void amf_free_source_lock(AMFSourceLock **plock)
 {
+    AMFSourceLock *lock = *plock;
+    if (!lock)
+        return;
+#if CONFIG_D3D11VA
+    if (lock->context)
+        lock->context->lpVtbl->Release(lock->context);
+    if (lock->state)
+        lock->state->lpVtbl->Release(lock->state);
+    if (lock->multithread)
+        lock->multithread->lpVtbl->Release(lock->multithread);
+#endif
     av_freep(plock);
 }
 
@@ -112,6 +148,43 @@ static AMFSourceLock 
*amf_create_source_lock(AVAMFDeviceContext *amf_ctx, void (
 }
 #endif
 
+#if CONFIG_D3D11VA
+static int amf_create_d3d11_state(AMFSourceLock *lock, ID3D11Device *device)
+{
+    static const GUID iid_device      = { 0xdb6f6ddb, 0xac77, 0x4e88, { 0x82, 
0x53, 0x81, 0x9d, 0xf9, 0xbb, 0xf1, 0x40 } };
+    static const GUID iid_device1     = { 0xa04bfb29, 0x08ef, 0x43d6, { 0xa4, 
0x9c, 0xa9, 0xbd, 0xbd, 0xcb, 0xe6, 0x86 } };
+    static const GUID iid_multithread = { 0x9b7e4e00, 0x342c, 0x4106, { 0xa1, 
0x9f, 0x4f, 0x27, 0x04, 0xf6, 0x89, 0xf0 } };
+    ID3D11Device1 *device1;
+    D3D_FEATURE_LEVEL level;
+    HRESULT hr;
+
+    if (device->lpVtbl->GetCreationFlags(device) & 
D3D11_CREATE_DEVICE_SINGLETHREADED)
+        return AVERROR(ENOSYS);
+    if (FAILED(device->lpVtbl->QueryInterface(device, &iid_device1, (void 
**)&device1)))
+        return AVERROR(ENOSYS);
+
+    level = device1->lpVtbl->GetFeatureLevel(device1);
+    hr = device1->lpVtbl->CreateDeviceContextState(device1, 0, &level, 1, 
D3D11_SDK_VERSION,
+                                                   &iid_device, NULL, 
&lock->state);
+    if (SUCCEEDED(hr))
+        hr = device->lpVtbl->QueryInterface(device, &iid_multithread, (void 
**)&lock->multithread);
+    if (SUCCEEDED(hr))
+        device1->lpVtbl->GetImmediateContext1(device1, &lock->context);
+    device1->lpVtbl->Release(device1);
+    if (FAILED(hr)) {
+        if (lock->state)
+            lock->state->lpVtbl->Release(lock->state);
+        if (lock->multithread)
+            lock->multithread->lpVtbl->Release(lock->multithread);
+        lock->state       = NULL;
+        lock->multithread = NULL;
+        return AVERROR(ENOSYS);
+    }
+
+    lock->multithread->lpVtbl->SetMultithreadProtected(lock->multithread, 
TRUE);
+    return 0;
+}
+#endif
 
 typedef struct AmfTraceWriter {
     AMFTraceWriterVtbl *vtblp;
@@ -916,8 +989,11 @@ static int amf_init_from_d3d11_device(AVAMFDeviceContext* 
amf_ctx, AVHWDeviceCon
 {
     AMF_RESULT res;
     AVD3D11VADeviceContext *hwctx = child_device_ctx->hwctx;
-    if (!amf_create_source_lock(amf_ctx, hwctx->lock, hwctx->unlock, 
hwctx->lock_ctx))
+    AMFSourceLock *lock = amf_create_source_lock(amf_ctx, hwctx->lock, 
hwctx->unlock, hwctx->lock_ctx);
+    if (!lock)
         return AVERROR(ENOMEM);
+    if (amf_create_d3d11_state(lock, hwctx->device) < 0)
+        av_log(child_device_ctx, AV_LOG_VERBOSE, "The D3D11 device does not 
support a separate context state for AMF.\n");
     amf_ctx->lock(amf_ctx->lock_ctx);
     res = amf_ctx->context->pVtbl->InitDX11(amf_ctx->context, hwctx->device, 
AMF_DX11_1);
     amf_ctx->unlock(amf_ctx->lock_ctx);
diff --git a/libavutil/hwcontext_amf.h b/libavutil/hwcontext_amf.h
index 5fe240a9e11..ee77b346c37 100644
--- a/libavutil/hwcontext_amf.h
+++ b/libavutil/hwcontext_amf.h
@@ -49,7 +49,14 @@ typedef struct AVAMFDeviceContext {
      * If unset on init, an internal mutex is used, which only serializes
      * the callers of these callbacks. A device derived from a D3D11VA or
      * D3D12VA device instead takes the lock of that device context, so that
-     * AMF's calls are serialized with the other users of that lock.
+     * AMF's calls are serialized with the other users of that lock. If a
+     * source D3D11 device supports D3D11.1 and is not single-threaded, the
+     * lock also enables the device's multithread protection, enters its
+     * multithread lock and switches the immediate context to a device
+     * context state of its own. AMF's calls are then serialized with all
+     * other calls on the device, and AMF neither sees nor changes the
+     * graphics pipeline state of other users of the device. Video context
+     * state and asynchronous queries are not part of a device context state.
      */
     void (*lock)(void *lock_ctx);
     void (*unlock)(void *lock_ctx);
-- 
2.54.1


>From 7aba8f57f266a04fcd9ed35d9e98d5169bb8c63c Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Sun, 25 Oct 2026 17:14:54 +0100
Subject: [PATCH 08/12] avfilter/amf: hold the device lock around AMF calls

The AMF filters call Init(), SubmitInput(), QueryOutput(), Drain() and
Terminate(), wrap D3D11 textures and duplicate surfaces without the
device lock, so on a device that other code also uses, those calls can
interleave with the other user's work on the same D3D11 context. Hold
the AMF device lock around each of them. Wrapping DXVA2 surfaces is left
as is: a DXVA2 device does not install a source lock. The lock is not
held between calls or across ff_filter_frame(), so a downstream filter
on the same device can take it, and the D3D11 copy of decoder slices
still takes the D3D11VA lock on its own. The texture-array slice index,
which AMF reads from the texture's private data when wrapping it, is set
under the same lock as the wrapping, so two filters wrapping slices of
one decoder texture cannot swap their indices.

Signed-off-by: Julius Bairaktaris <[email protected]>
---
 libavfilter/vf_amf_common.c | 37 +++++++++++++++++++++++++++++++++----
 libavfilter/vf_amf_common.h |  2 ++
 libavfilter/vf_frc_amf.c    |  2 ++
 libavfilter/vf_sr_amf.c     |  4 ++++
 libavfilter/vf_vpp_amf.c    |  2 ++
 libavfilter/vf_vqe_amf.c    |  2 ++
 6 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/libavfilter/vf_amf_common.c b/libavfilter/vf_amf_common.c
index e1d0b42ee37..7294cf099bb 100644
--- a/libavfilter/vf_amf_common.c
+++ b/libavfilter/vf_amf_common.c
@@ -61,10 +61,23 @@ int amf_filter_init(AVFilterContext *avctx)
     return 0;
 }
 
+void amf_lock(AMFFilterContext *ctx)
+{
+    if (ctx->amf_device_ctx && ctx->amf_device_ctx->lock)
+        ctx->amf_device_ctx->lock(ctx->amf_device_ctx->lock_ctx);
+}
+
+void amf_unlock(AMFFilterContext *ctx)
+{
+    if (ctx->amf_device_ctx && ctx->amf_device_ctx->unlock)
+        ctx->amf_device_ctx->unlock(ctx->amf_device_ctx->lock_ctx);
+}
+
 void amf_filter_uninit(AVFilterContext *avctx)
 {
     AMFFilterContext *ctx = avctx->priv;
 
+    amf_lock(ctx);
     if (ctx->component) {
         ctx->component->pVtbl->Terminate(ctx->component);
         ctx->component->pVtbl->Release(ctx->component);
@@ -76,6 +89,7 @@ void amf_filter_uninit(AVFilterContext *avctx)
         ctx->pre_converter->pVtbl->Release(ctx->pre_converter);
         ctx->pre_converter = NULL;
     }
+    amf_unlock(ctx);
 
     if (ctx->pending) {
         AVFrame *props;
@@ -121,7 +135,9 @@ static int amf_receive_surface(AVFilterContext *avctx, 
AMFComponent *component,
     AMF_RESULT res;
 
     *surface = NULL;
+    amf_lock(avctx->priv);
     res = component->pVtbl->QueryOutput(component, &data);
+    amf_unlock(avctx->priv);
     AMF_RETURN_IF_FALSE(avctx, res == AMF_OK || res == AMF_REPEAT || res == 
AMF_EOF,
                         AVERROR_UNKNOWN, "QueryOutput() failed with error 
%d\n", res);
     if (!data)
@@ -232,7 +248,12 @@ static int amf_submit_surface(AVFilterContext *avctx, 
AMFComponent *component, A
     AMF_RESULT res;
     int ret = 0;
 
-    while ((res = component->pVtbl->SubmitInput(component, (AMFData*)surface)) 
== AMF_INPUT_FULL) {
+    for (;;) {
+        amf_lock(avctx->priv);
+        res = component->pVtbl->SubmitInput(component, (AMFData*)surface);
+        amf_unlock(avctx->priv);
+        if (res != AMF_INPUT_FULL)
+            break;
         ret = deliver(avctx);
         if (ret < 0)
             break;
@@ -268,7 +289,12 @@ static int amf_drain_component(AVFilterContext *avctx, 
AMFComponent *component,
     AMF_RESULT res;
     int ret;
 
-    while ((res = component->pVtbl->Drain(component)) == AMF_INPUT_FULL) {
+    for (;;) {
+        amf_lock(avctx->priv);
+        res = component->pVtbl->Drain(component);
+        amf_unlock(avctx->priv);
+        if (res != AMF_INPUT_FULL)
+            break;
         ret = deliver(avctx);
         if (ret < 0)
             return ret;
@@ -803,12 +829,13 @@ int amf_avframe_to_amfsurface(AVFilterContext *avctx, 
const AVFrame *frame, AMFS
                 break;
             }
 
-            texture->lpVtbl->SetPrivateData(texture, 
&AMFTextureArrayIndexGUID, sizeof(index), &index);
-
             ret = amf_hold_frame(frame, &observer);
             if (ret < 0)
                 return ret;
+            amf_lock(ctx);
+            texture->lpVtbl->SetPrivateData(texture, 
&AMFTextureArrayIndexGUID, sizeof(index), &index);
             res = 
ctx->amf_device_ctx->context->pVtbl->CreateSurfaceFromDX11Native(ctx->amf_device_ctx->context,
 texture, &surface, observer); // wrap to AMF surface
+            amf_unlock(ctx);
             if (res != AMF_OK)
                 amf_release_held_frame(observer, NULL);
             AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR(ENOMEM), 
"CreateSurfaceFromDX11Native() failed  with error %d\n", res);
@@ -927,7 +954,9 @@ int amf_avframe_to_amfsurface(AVFilterContext *avctx, const 
AVFrame *frame, AMFS
         if (crop_x || crop_y) {
             if (crop_w == outlink->w && crop_h == outlink->h) {
                 AMFData *cropped_buffer = NULL;
+                amf_lock(ctx);
                 res = surface->pVtbl->Duplicate(surface, 
surface->pVtbl->GetMemoryType(surface), &cropped_buffer);
+                amf_unlock(ctx);
                 AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR(ENOMEM), 
"Duplicate() failed  with error %d\n", res);
                 surface->pVtbl->Release(surface);
                 surface = (AMFSurface*)cropped_buffer;
diff --git a/libavfilter/vf_amf_common.h b/libavfilter/vf_amf_common.h
index 5b44555f619..7d387873f6a 100644
--- a/libavfilter/vf_amf_common.h
+++ b/libavfilter/vf_amf_common.h
@@ -80,6 +80,8 @@ typedef struct AMFFilterContext {
 } AMFFilterContext;
 
 int amf_filter_init(AVFilterContext *avctx);
+void amf_lock(AMFFilterContext *ctx);
+void amf_unlock(AMFFilterContext *ctx);
 void amf_filter_uninit(AVFilterContext *avctx);
 enum AVPixelFormat amf_inlink_sw_format(AVFilterLink *inlink);
 int amf_init_filter_config(AVFilterLink *outlink, enum AVPixelFormat 
*in_format);
diff --git a/libavfilter/vf_frc_amf.c b/libavfilter/vf_frc_amf.c
index 0cd90e42c70..743ac5dd0ba 100644
--- a/libavfilter/vf_frc_amf.c
+++ b/libavfilter/vf_frc_amf.c
@@ -143,7 +143,9 @@ static int amf_frc_filter_config_output(AVFilterLink 
*outlink)
 
     AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, amf_filter, 
AMF_FRC_USE_FUTURE_FRAME, frc_ctx->use_future_frame);
 
+    amf_lock(amf_ctx);
     res = AMF_IFACE_CALL(amf_filter, Init, av_av_to_amf_format(in_format), 
inlink->w, inlink->h);
+    amf_unlock(amf_ctx);
     AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "AMFFRC->Init() 
failed with error %d\n", res);
 
     AMF_ASSIGN_PROPERTY_INT64(res, amf_filter, AMF_FRC_MODE, frc_ctx->enable ? 
FRC_x2_PRESENT : FRC_OFF);
diff --git a/libavfilter/vf_sr_amf.c b/libavfilter/vf_sr_amf.c
index d589fa99703..6c009c04158 100644
--- a/libavfilter/vf_sr_amf.c
+++ b/libavfilter/vf_sr_amf.c
@@ -172,7 +172,9 @@ static int amf_filter_config_output(AVFilterLink *outlink)
         AMF_ASSIGN_PROPERTY_INT64(res, ctx->pre_converter, 
AMF_VIDEO_CONVERTER_OUTPUT_COLOR_RANGE, AMF_COLOR_RANGE_FULL);
         AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, 
"AMFConverter-SetProperty() failed with error %d\n", res);
 
+        amf_lock(ctx);
         res = ctx->pre_converter->pVtbl->Init(ctx->pre_converter, 
av_av_to_amf_format(in_format), inlink->w, inlink->h);
+        amf_unlock(ctx);
         AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN,
                             "AMFConverter-Init() failed with error %d, %s to 
%s is not supported by this device%s\n",
                             res, av_get_pix_fmt_name(in_format), 
av_get_pix_fmt_name(ctx->format),
@@ -216,7 +218,9 @@ static int amf_filter_config_output(AVFilterLink *outlink)
     ctx->out_primaries = AMF_COLOR_PRIMARIES_UNDEFINED;
     ctx->out_trc = AMF_COLOR_TRANSFER_CHARACTERISTIC_UNDEFINED;
 
+    amf_lock(ctx);
     res = ctx->component->pVtbl->Init(ctx->component, 
av_av_to_amf_format(in_format), inlink->w, inlink->h);
+    amf_unlock(ctx);
     AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, 
"AMFHQScaler-Init() failed with error %d\n", res);
 
     return 0;
diff --git a/libavfilter/vf_vpp_amf.c b/libavfilter/vf_vpp_amf.c
index 5bde07dd468..5ec5366848a 100644
--- a/libavfilter/vf_vpp_amf.c
+++ b/libavfilter/vf_vpp_amf.c
@@ -218,7 +218,9 @@ static int amf_filter_config_output(AVFilterLink *outlink)
         }
     }
 
+    amf_lock(ctx);
     res = ctx->component->pVtbl->Init(ctx->component, 
av_av_to_amf_format(in_format), inlink->w, inlink->h);
+    amf_unlock(ctx);
     AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, 
"AMFConverter-Init() failed with error %d\n", res);
 
     return 0;
diff --git a/libavfilter/vf_vqe_amf.c b/libavfilter/vf_vqe_amf.c
index 0e5a55ada51..84d5bfed1c6 100644
--- a/libavfilter/vf_vqe_amf.c
+++ b/libavfilter/vf_vqe_amf.c
@@ -117,7 +117,9 @@ static int amf_vqe_filter_config_output(AVFilterLink 
*outlink)
     AMF_ASSIGN_PROPERTY_DOUBLE(res, amf_filter, AMF_VE_FCR_ATTENUATION, 
vqe_ctx->attenuation);
     AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, "Failed to set 
VQ enhancer attenuation: %d\n", res);
 
+    amf_lock(amf_ctx);
     res = AMF_IFACE_CALL(amf_filter, Init, av_av_to_amf_format(in_format), 
inlink->w, inlink->h);
+    amf_unlock(amf_ctx);
     AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, 
"AMFVQEnhancer-Init() failed with error %d\n", res);
 
     return 0;
-- 
2.54.1


>From 58d83d9799819c905d9340097524f16e5049698c Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Sun, 1 Nov 2026 12:59:10 +0100
Subject: [PATCH 09/12] avcodec/amfenc: copy software frames outside the device
 lock

amf_submit_frame_locked() holds the AMF device lock while it allocates a
host surface for a software frame and copies the frame into it. On an
AMF device derived from a D3D11VA or D3D12VA device, that lock is the
source device's, so every other user of that device waits for the CPU
copy, which does not touch the device.

Allocate the host surface and copy the frame before taking the lock.

Suggested by the Forgejo_Fairy review of #25458.

Tested on Windows 11, RX 9070 XT, driver 32.0.32015.7029: h264_amf,
hevc_amf and av1_amf give bit-identical output for 8-bit and 10-bit
software input and for d3d11va and AMF surface input. With 1080p NV12
software input to hevc_amf, the lock is held 748 us per frame on
average instead of 1071 us, and 577 us instead of 741 us for P010.

Signed-off-by: Julius Bairaktaris <[email protected]>
---
 libavcodec/amfenc.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
index 27b32a90813..e0c3e09648c 100644
--- a/libavcodec/amfenc.c
+++ b/libavcodec/amfenc.c
@@ -453,7 +453,8 @@ static AMF_RESULT 
amf_release_attached_frame_ref(AMFEncoderContext *ctx, AMFBuff
     return res;
 }
 
-static int amf_submit_frame(AVCodecContext *avctx, AVFrame    *frame, 
AMFSurface **surface_resubmit)
+static int amf_submit_frame(AVCodecContext *avctx, AVFrame    *frame, 
AMFSurface *host_surface,
+                            AMFSurface **surface_resubmit)
 {
     AMFEncoderContext      *ctx = avctx->priv_data;
     AVHWDeviceContext      *hw_device_ctx = 
(AVHWDeviceContext*)ctx->device_ctx_ref->data;
@@ -499,11 +500,9 @@ static int amf_submit_frame(AVCodecContext *avctx, AVFrame 
   *frame, AMFSurface
         }
         break;
     default:
-        {
-            res = 
amf_device_ctx->context->pVtbl->AllocSurface(amf_device_ctx->context, 
AMF_MEMORY_HOST, ctx->format, avctx->width, avctx->height, &surface);
-            AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), 
"AllocSurface() failed  with error %d\n", res);
-            amf_copy_surface(avctx, frame, surface);
-        }
+        AMF_RETURN_IF_FALSE(ctx, host_surface, AVERROR(EINVAL), "Unsupported 
input format %s\n",
+                            av_get_pix_fmt_name(frame->format));
+        surface = host_surface;
         break;
     }
     if (hw_surface) {
@@ -621,9 +620,18 @@ static int amf_submit_frame_locked(AVCodecContext *avctx, 
AVFrame *frame, AMFSur
     AVHWDeviceContext     *hw_device_ctx = 
(AVHWDeviceContext*)ctx->device_ctx_ref->data;
     AVAMFDeviceContext    *amf_device_ctx = (AVAMFDeviceContext 
*)hw_device_ctx->hwctx;
 
+    AMFSurface            *host_surface = NULL;
+    AMF_RESULT             res;
+
+    if (!(av_pix_fmt_desc_get(frame->format)->flags & 
AV_PIX_FMT_FLAG_HWACCEL)) {
+        res = 
amf_device_ctx->context->pVtbl->AllocSurface(amf_device_ctx->context, 
AMF_MEMORY_HOST, ctx->format, avctx->width, avctx->height, &host_surface);
+        AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR(ENOMEM), 
"AllocSurface() failed  with error %d\n", res);
+        amf_copy_surface(avctx, frame, host_surface);
+    }
+
     if (amf_device_ctx->lock)
         amf_device_ctx->lock(amf_device_ctx->lock_ctx);
-    ret = amf_submit_frame(avctx, frame, surface_resubmit);
+    ret = amf_submit_frame(avctx, frame, host_surface, surface_resubmit);
     if (amf_device_ctx->unlock)
         amf_device_ctx->unlock(amf_device_ctx->lock_ctx);
 
-- 
2.54.1


>From 6281bd4991dc70f71947bc6632c673d6b363e31c Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Sat, 14 Nov 2026 18:43:28 +0100
Subject: [PATCH 10/12] avutil/hwcontext_amf: unmap the surface when the
 mapping descriptor fails

amf_map_frame() maps the surface with Map() and then creates the
mapping descriptor, which owns the Unmap() call. When the descriptor
cannot be allocated, ff_hwframe_map_create() returns an error without
running the unmap callback, and the failure path only released the
AMFSurface1 interface, so the surface stayed mapped. Unmap it, under
the device lock like the Map() call, before returning the error.

Found by Forgejo_Fairy in the review of pull request 25458.

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

diff --git a/libavutil/hwcontext_amf.c b/libavutil/hwcontext_amf.c
index 85a4571ccc0..fa10012b9a8 100644
--- a/libavutil/hwcontext_amf.c
+++ b/libavutil/hwcontext_amf.c
@@ -1352,6 +1352,11 @@ static int amf_map_frame(AVHWFramesContext *device_ctx, 
AVFrame *dst, const AVFr
     AMF_GOTO_FAIL_IF_FALSE(device_ctx, res == AMF_OK, AVERROR_UNKNOWN, 
"AMFSurface1->Map() failed with error %d\n", res);
 
     ret = ff_hwframe_map_create(src->hw_frames_ctx, dst, src, 
&amf_unmap_frame, surface1);
+    if (ret < 0) {
+        amf_ctx->lock(amf_ctx->lock_ctx);
+        AMF_IFACE_CALL(surface1, Unmap);
+        amf_ctx->unlock(amf_ctx->lock_ctx);
+    }
     AMF_GOTO_FAIL_IF_FALSE(device_ctx, ret >= 0, ret, "ff_hwframe_map_create 
failed with error %d\n", ret);
 
     dst->width  = src->width;
-- 
2.54.1


>From 413a4a9c7314c8fec30d1fed3a2c33befb202491 Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Mon, 28 Sep 2026 06:36:28 +0200
Subject: [PATCH 11/12] avfilter/vf_amf_common: support Vulkan frames

vqe_amf, sr_amf and vpp_amf do not accept Vulkan frames. Accept
AV_PIX_FMT_VULKAN, and output Vulkan frames when the filter device is a
Vulkan device; the AMF device is derived from it. AMF waits on the
frame's timeline value and signals the next one, which is claimed in
the AVVkFrame under the frame lock, so AMF's own semaphores are never
exposed. Input is copied on AMF's queue into an AMF surface, because
the components keep input surfaces across calls and do not reliably
report the layout they leave a wrapped image in.

Tested on Windows 11, RX 9070 XT, driver 32.0.32029.2010, with H.264
1080p decoded by the Vulkan hwaccel to Vulkan frames: vpp_amf scaling
to 1280x720 gives the same frames as on D3D11VA input, vqe_amf differs
from its D3D11 output by 90 dB PSNR in luma and sr_amf to 2560x1440 by
62 dB, with identical chroma. vqe_amf followed by scale_vulkan runs on
the filter's Vulkan output.

Signed-off-by: Julius Bairaktaris <[email protected]>
---
 Changelog                   |   1 +
 doc/filters.texi            |  10 ++
 libavfilter/vf_amf_common.c | 209 +++++++++++++++++++++++++++++++++++-
 libavfilter/vf_sr_amf.c     |   3 +
 libavfilter/vf_vpp_amf.c    |   1 +
 libavfilter/vf_vqe_amf.c    |   1 +
 6 files changed, 221 insertions(+), 4 deletions(-)

diff --git a/Changelog b/Changelog
index 856aa095908..df145475f57 100644
--- a/Changelog
+++ b/Changelog
@@ -8,6 +8,7 @@ version <next>:
 - PCM PAMF decoder
 - Super Audio CD (SACD) demuxer
 - Added the ability to derive an AMF hwdevice from a Vulkan hwdevice
+- AMF filters on Vulkan frames
 
 version 9.1:
 - ASTC image encoding and decoding via libastcenc, with .astc and KTX 1.0 
muxers and demuxers
diff --git a/doc/filters.texi b/doc/filters.texi
index 98d3e9f6406..3a19877d79e 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -15110,6 +15110,16 @@ Example:
 ffmpeg -i input.mp4 -vf vqe_amf=attenuation=0.4 -c:v hevc_amf output.mp4
 @end example
 
+The AMF filters also run on Vulkan frames; the AMF device is then derived from
+the Vulkan device, which needs the extensions listed for that derivation in the
+AMF section of the general documentation. With a Vulkan filter device the
+output frames are Vulkan frames:
+@example
+ffmpeg -init_hw_device 
vulkan=vk:0,instance_extensions=VK_KHR_get_physical_device_properties2+VK_KHR_surface,device_extensions=VK_KHR_swapchain
 \
+       -filter_hw_device vk -hwaccel vulkan -hwaccel_output_format vulkan -i 
input.mp4 \
+       -vf vqe_amf,hwdownload,format=nv12 output.y4m
+@end example
+
 @section framestep
 
 Select one frame every N-th frame.
diff --git a/libavfilter/vf_amf_common.c b/libavfilter/vf_amf_common.c
index 7294cf099bb..f7e9612cc1b 100644
--- a/libavfilter/vf_amf_common.c
+++ b/libavfilter/vf_amf_common.c
@@ -42,6 +42,11 @@
 #include "libavutil/hwcontext_d3d11va.h"
 #endif
 
+#if CONFIG_VULKAN
+#include "libavutil/hwcontext_vulkan.h"
+#include <AMF/core/VulkanAMF.h>
+#endif
+
 int amf_filter_init(AVFilterContext *avctx)
 {
     AMFFilterContext     *ctx = avctx->priv;
@@ -413,11 +418,14 @@ int amf_setup_input_output_formats(AVFilterContext *avctx,
     int err;
     AVFilterFormats *input_formats;
     AVFilterFormats *output_formats;
-    static const enum AVPixelFormat output_pix_fmts[] = {
+    const enum AVPixelFormat *output_pix_fmts;
+    static const enum AVPixelFormat output_pix_fmts_amf[] = {
         AV_PIX_FMT_AMF_SURFACE,
         AV_PIX_FMT_NONE,
     };
 
+    output_pix_fmts = output_pix_fmts_amf;
+
     //in case if hw_device_ctx is set to DXVA2 we change order of pixel 
formats to set DXVA2 be chosen by default
     //The order is ignored if hw_frames_ctx is not NULL on the config_output 
stage
     if (avctx->hw_device_ctx) {
@@ -445,6 +453,18 @@ int amf_setup_input_output_formats(AVFilterContext *avctx,
                 input_pix_fmts  = pix_fmts_dxva2;
             }
             break;
+    #endif
+    #if CONFIG_VULKAN
+        case AV_HWDEVICE_TYPE_VULKAN:
+            {
+                static const enum AVPixelFormat pix_fmts_vulkan[] = {
+                    AV_PIX_FMT_VULKAN,
+                    AV_PIX_FMT_NONE,
+                };
+                input_pix_fmts  = pix_fmts_vulkan;
+                output_pix_fmts = pix_fmts_vulkan;
+            }
+            break;
     #endif
         case AV_HWDEVICE_TYPE_AMF:
             break;
@@ -589,7 +609,12 @@ int amf_init_filter_config(AVFilterLink *outlink, enum 
AVPixelFormat *in_format)
         else
             out_sw_format = outlink->format;
     }
-    ctx->hwframes_out_ref = av_hwframe_ctx_alloc(ctx->amf_device_ref);
+    if (outlink->format == AV_PIX_FMT_VULKAN)
+        ctx->hwframes_out_ref = av_hwframe_ctx_alloc(inl->hw_frames_ctx ?
+                                    
((AVHWFramesContext*)inl->hw_frames_ctx->data)->device_ref :
+                                    avctx->hw_device_ctx);
+    else
+        ctx->hwframes_out_ref = av_hwframe_ctx_alloc(ctx->amf_device_ref);
     if (!ctx->hwframes_out_ref)
         return AVERROR(ENOMEM);
     hwframes_out = (AVHWFramesContext*)ctx->hwframes_out_ref->data;
@@ -598,7 +623,7 @@ int amf_init_filter_config(AVFilterLink *outlink, enum 
AVPixelFormat *in_format)
     {
         ctx->amf_device_ctx =  hwdev_ctx->hwctx;
     }
-    hwframes_out->format    = AV_PIX_FMT_AMF_SURFACE;
+    hwframes_out->format    = outlink->format == AV_PIX_FMT_VULKAN ? 
AV_PIX_FMT_VULKAN : AV_PIX_FMT_AMF_SURFACE;
     hwframes_out->sw_format = out_sw_format;
 
     // in_sw_format is inlink->format for software input and the underlying
@@ -635,6 +660,39 @@ void amf_free_amfsurface(void *opaque, uint8_t *data)
     surface->pVtbl->Release(surface);
 }
 
+#if CONFIG_VULKAN
+static int amf_wrap_vulkan_frame(AVFilterContext *avctx, const AVFrame *frame,
+                                 VkImageLayout amf_layout, AMFSurface 
**ppSurface);
+
+static int amf_copy_to_vulkan_frame(AVFilterContext *avctx, AMFSurface *src, 
AVFrame *frame)
+{
+    AMFFilterContext *ctx = avctx->priv;
+    AMFSurface *dst;
+    AMF_RESULT res;
+    int ret;
+
+    ret = av_hwframe_get_buffer(ctx->hwframes_out_ref, frame, 0);
+    if (ret < 0)
+        return ret;
+
+    ret = amf_wrap_vulkan_frame(avctx, frame, 
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &dst);
+    if (ret < 0)
+        return ret;
+
+    amf_lock(ctx);
+    res = AMF_IFACE_CALL(src, Convert, AMF_MEMORY_VULKAN);
+    if (res == AMF_OK && AMF_IFACE_CALL(src, GetMemoryType) != 
AMF_MEMORY_VULKAN)
+        res = AMF_NOT_SUPPORTED;
+    if (res == AMF_OK)
+        res = AMF_IFACE_CALL(src, CopySurfaceRegion, dst, 0, 0, 0, 0,
+                             frame->width, frame->height);
+    amf_unlock(ctx);
+    AMF_IFACE_CALL(dst, Release);
+    AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_EXTERNAL, 
"CopySurfaceRegion() failed with error %d\n", res);
+    return 0;
+}
+#endif
+
 AVFrame *amf_amfsurface_to_avframe(AVFilterContext *avctx, AMFSurface* 
pSurface)
 {
     AVFrame *frame = av_frame_alloc();
@@ -660,6 +718,12 @@ AVFrame *amf_amfsurface_to_avframe(AVFilterContext *avctx, 
AMFSurface* pSurface)
                                             AV_BUFFER_FLAG_READONLY);
             if (!frame->buf[1])
                 goto fail;
+#if CONFIG_VULKAN
+        } else if (hwframes_out->format == AV_PIX_FMT_VULKAN) {
+            if (amf_copy_to_vulkan_frame(avctx, pSurface, frame) < 0)
+                goto fail;
+            pSurface->pVtbl->Release(pSurface);
+#endif
         } else { // FIXME: add processing of other hw formats
             av_log(ctx, AV_LOG_ERROR, "Unknown pixel format\n");
             goto fail;
@@ -765,16 +829,41 @@ static int amf_copy_d3d11_texture(AVFilterContext *avctx, 
const AVFrame *frame,
 }
 #endif
 
-#if CONFIG_D3D11VA || CONFIG_DXVA2
+#if CONFIG_D3D11VA || CONFIG_DXVA2 || CONFIG_VULKAN
 typedef struct AMFFrameHolder {
     AMFSurfaceObserver observer;
     AVFrame *frame;
+#if CONFIG_VULKAN
+    AMFVulkanSurface  vk_surface;
+    AMFVulkanTimeline vk_timeline;
+    uint64_t          vk_claimed;
+#endif
 } AMFFrameHolder;
 
 static void AMF_STD_CALL amf_release_held_frame(AMFSurfaceObserver *observer, 
AMFSurface *surface)
 {
     AMFFrameHolder *holder = (AMFFrameHolder*)observer;
 
+#if CONFIG_VULKAN
+    if (holder->frame && holder->frame->format == AV_PIX_FMT_VULKAN) {
+        AVHWFramesContext    *frames = 
(AVHWFramesContext*)holder->frame->hw_frames_ctx->data;
+        AVVulkanDeviceContext *hwctx = frames->device_ctx->hwctx;
+        AVVkFrame                 *f = (AVVkFrame*)holder->frame->data[0];
+
+        if (holder->vk_timeline.uiCount < holder->vk_claimed) {
+            PFN_vkGetDeviceProcAddr get_dev_proc_addr =
+                (PFN_vkGetDeviceProcAddr)hwctx->get_proc_addr(hwctx->inst, 
"vkGetDeviceProcAddr");
+            PFN_vkSignalSemaphore signal_semaphore =
+                (PFN_vkSignalSemaphore)get_dev_proc_addr(hwctx->act_dev, 
"vkSignalSemaphore");
+            VkSemaphoreSignalInfo info = {
+                .sType     = VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO,
+                .semaphore = f->sem[0],
+                .value     = holder->vk_claimed,
+            };
+            signal_semaphore(hwctx->act_dev, &info);
+        }
+    }
+#endif
     av_frame_free(&holder->frame);
     av_free(holder);
 }
@@ -798,6 +887,107 @@ static int amf_hold_frame(const AVFrame *frame, 
AMFSurfaceObserver **observer)
 }
 #endif
 
+#if CONFIG_VULKAN
+static int amf_wrap_vulkan_frame(AVFilterContext *avctx, const AVFrame *frame,
+                                 VkImageLayout amf_layout, AMFSurface 
**ppSurface)
+{
+    AMFFilterContext        *ctx = avctx->priv;
+    AVHWFramesContext    *frames = 
(AVHWFramesContext*)frame->hw_frames_ctx->data;
+    AVVulkanFramesContext *vkfc  = frames->hwctx;
+    AVVkFrame                 *f = (AVVkFrame*)frame->data[0];
+    AMFGuid                 guid = IID_AMFContext1();
+    AMFContext1        *context1 = NULL;
+    AMFSurfaceObserver *observer;
+    AMFFrameHolder       *holder;
+    AMF_RESULT               res;
+    int                      ret;
+
+    if (f->img[1] != VK_NULL_HANDLE) {
+        av_log(avctx, AV_LOG_ERROR, "AMF needs single-image multiplane Vulkan 
frames.\n");
+        return AVERROR(EINVAL);
+    }
+
+    res = AMF_IFACE_CALL(ctx->amf_device_ctx->context, QueryInterface, &guid, 
(void**)&context1);
+    AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR_UNKNOWN, 
"QueryInterface(AMFContext1) failed with error %d\n", res);
+
+    ret = amf_hold_frame(frame, &observer);
+    if (ret < 0) {
+        AMF_IFACE_CALL(context1, Release);
+        return ret;
+    }
+    holder = (AMFFrameHolder*)observer;
+
+    vkfc->lock_frame(frames, f);
+    holder->vk_timeline = (AMFVulkanTimeline) {
+        .cbSizeof       = sizeof(holder->vk_timeline),
+        .eExtensionType = AMF_VARIANT_TIMELINE_SEMAPHORE,
+        .uiCount        = f->sem_value[0],
+    };
+    holder->vk_surface = (AMFVulkanSurface) {
+        .cbSizeof       = sizeof(holder->vk_surface),
+        .hImage         = f->img[0],
+        .hMemory        = f->mem[0],
+        .iSize          = f->size[0],
+        .eFormat        = vkfc->format[0],
+        .iWidth         = frames->width,
+        .iHeight        = frames->height,
+        .eCurrentLayout = f->layout[0],
+        .Sync           = {
+            .cbSizeof   = sizeof(holder->vk_surface.Sync),
+            .pNext      = &holder->vk_timeline,
+            .hSemaphore = f->sem[0],
+            .bSubmitted = 1,
+        },
+    };
+    res = AMF_IFACE_CALL(context1, CreateSurfaceFromVulkanNative, 
&holder->vk_surface, ppSurface, observer);
+    if (res == AMF_OK) {
+        holder->vk_claimed = ++f->sem_value[0];
+        f->layout[0]       = amf_layout;
+        f->access[0]       = VK_ACCESS_2_NONE;
+    }
+    vkfc->unlock_frame(frames, f);
+    AMF_IFACE_CALL(context1, Release);
+    if (res != AMF_OK) {
+        av_frame_free(&holder->frame);
+        av_free(holder);
+    }
+    AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR(ENOMEM), 
"CreateSurfaceFromVulkanNative() failed with error %d\n", res);
+    return 0;
+}
+
+static int amf_copy_from_vulkan_frame(AVFilterContext *avctx, const AVFrame 
*frame, AMFSurface **ppSurface)
+{
+    AMFFilterContext     *ctx = avctx->priv;
+    AVHWFramesContext *frames = (AVHWFramesContext*)frame->hw_frames_ctx->data;
+    AMFSurface *src, *dst;
+    AMF_RESULT res;
+    int ret;
+
+    res = AMF_IFACE_CALL(ctx->amf_device_ctx->context, AllocSurface, 
AMF_MEMORY_VULKAN,
+                         av_av_to_amf_format(frames->sw_format), 
frames->width, frames->height, &dst);
+    AMF_RETURN_IF_FALSE(avctx, res == AMF_OK, AVERROR(ENOMEM), "AllocSurface() 
failed with error %d\n", res);
+
+    ret = amf_wrap_vulkan_frame(avctx, frame, 
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, &src);
+    if (ret < 0) {
+        AMF_IFACE_CALL(dst, Release);
+        return ret;
+    }
+
+    amf_lock(ctx);
+    res = AMF_IFACE_CALL(src, CopySurfaceRegion, dst, 0, 0, 0, 0, 
frames->width, frames->height);
+    amf_unlock(ctx);
+    AMF_IFACE_CALL(src, Release);
+    if (res != AMF_OK) {
+        AMF_IFACE_CALL(dst, Release);
+        av_log(avctx, AV_LOG_ERROR, "CopySurfaceRegion() failed with error 
%d\n", res);
+        return AVERROR_EXTERNAL;
+    }
+
+    *ppSurface = dst;
+    return 0;
+}
+#endif
+
 int amf_avframe_to_amfsurface(AVFilterContext *avctx, const AVFrame *frame, 
AMFSurface** ppSurface)
 {
     AMFVariantStruct var = { 0 };
@@ -809,6 +999,9 @@ int amf_avframe_to_amfsurface(AVFilterContext *avctx, const 
AVFrame *frame, AMFS
 #if CONFIG_D3D11VA || CONFIG_DXVA2
     AMFSurfaceObserver *observer;
 #endif
+#if CONFIG_VULKAN
+    int ret;
+#endif
 
     switch (frame->format) {
 #if CONFIG_D3D11VA
@@ -842,6 +1035,14 @@ int amf_avframe_to_amfsurface(AVFilterContext *avctx, 
const AVFrame *frame, AMFS
             hw_surface = 1;
         }
         break;
+#endif
+#if CONFIG_VULKAN
+    case AV_PIX_FMT_VULKAN:
+        ret = amf_copy_from_vulkan_frame(avctx, frame, &surface);
+        if (ret < 0)
+            return ret;
+        hw_surface = 1;
+        break;
 #endif
     case AV_PIX_FMT_AMF_SURFACE:
         {
diff --git a/libavfilter/vf_sr_amf.c b/libavfilter/vf_sr_amf.c
index 6c009c04158..00f4149091a 100644
--- a/libavfilter/vf_sr_amf.c
+++ b/libavfilter/vf_sr_amf.c
@@ -77,6 +77,7 @@ static int amf_filter_query_formats(AVFilterContext *avctx)
         AV_PIX_FMT_AMF_SURFACE,
         AV_PIX_FMT_D3D11,
         AV_PIX_FMT_DXVA2_VLD,
+        AV_PIX_FMT_VULKAN,
         AV_PIX_FMT_RGBAF16,
         AV_PIX_FMT_NONE,
     };
@@ -89,6 +90,7 @@ static int amf_filter_query_formats(AVFilterContext *avctx)
         AV_PIX_FMT_AMF_SURFACE,
         AV_PIX_FMT_D3D11,
         AV_PIX_FMT_DXVA2_VLD,
+        AV_PIX_FMT_VULKAN,
         AV_PIX_FMT_NONE,
     };
     enum AVPixelFormat pix_fmts_requested[] = {
@@ -96,6 +98,7 @@ static int amf_filter_query_formats(AVFilterContext *avctx)
         AV_PIX_FMT_AMF_SURFACE,
         AV_PIX_FMT_D3D11,
         AV_PIX_FMT_DXVA2_VLD,
+        AV_PIX_FMT_VULKAN,
         AV_PIX_FMT_NONE,
     };
     int i;
diff --git a/libavfilter/vf_vpp_amf.c b/libavfilter/vf_vpp_amf.c
index 5ec5366848a..9b0e472f47a 100644
--- a/libavfilter/vf_vpp_amf.c
+++ b/libavfilter/vf_vpp_amf.c
@@ -50,6 +50,7 @@ static int amf_filter_query_formats(AVFilterContext *avctx)
         AV_PIX_FMT_AMF_SURFACE,
         AV_PIX_FMT_D3D11,
         AV_PIX_FMT_DXVA2_VLD,
+        AV_PIX_FMT_VULKAN,
         AV_PIX_FMT_NV12,
         AV_PIX_FMT_P010,
         AV_PIX_FMT_BGRA,
diff --git a/libavfilter/vf_vqe_amf.c b/libavfilter/vf_vqe_amf.c
index 84d5bfed1c6..6d4f8725a13 100644
--- a/libavfilter/vf_vqe_amf.c
+++ b/libavfilter/vf_vqe_amf.c
@@ -67,6 +67,7 @@ static int amf_filter_query_formats(AVFilterContext *avctx)
         AV_PIX_FMT_AMF_SURFACE,
         AV_PIX_FMT_D3D11,
         AV_PIX_FMT_DXVA2_VLD,
+        AV_PIX_FMT_VULKAN,
         AV_PIX_FMT_NV12,
         AV_PIX_FMT_P010,
         AV_PIX_FMT_NONE,
-- 
2.54.1


>From 43e4d2c848c0c809a1ec1abf0a3365da34b38703 Mon Sep 17 00:00:00 2001
From: Julius Bairaktaris <[email protected]>
Date: Mon, 28 Sep 2026 06:36:41 +0200
Subject: [PATCH 12/12] avfilter/vf_frc_amf: run on Vulkan frames

frc_amf does not accept Vulkan frames. Accept AV_PIX_FMT_VULKAN and
select FRC_ENGINE_VULKAN on a Vulkan context when no engine is given.
FRC has its own engine enum, so the AMF_MEMORY_VULKAN value the other
components take does not apply. engine_type is unchanged, so the Vulkan
engine is never chosen on a D3D11 context.

Tested on Windows 11, RX 9070 XT, driver 32.0.32029.2010: 60 frames of
H.264 1080p decoded by the Vulkan hwaccel give the same 119 frames as
on D3D11VA input, and vqe_amf,frc_amf runs as a chain on Vulkan frames.

Signed-off-by: Julius Bairaktaris <[email protected]>
---
 doc/filters.texi         | 4 ++--
 libavfilter/vf_frc_amf.c | 3 +++
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/doc/filters.texi b/doc/filters.texi
index 3a19877d79e..67f4f18e5a8 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -15021,8 +15021,8 @@ DirectX 12.
 
 By default DirectX 11 is used when FFmpeg is built with D3D11VA support and
 the AMF context uses a DirectX 11 device with feature level 11_1 or higher,
-which the DirectX 11 engine requires. Otherwise, the AMF runtime default
-(DirectX 12) is used.
+which the DirectX 11 engine requires. On a Vulkan device the Vulkan engine is
+used. Otherwise, the AMF runtime default (DirectX 12) is used.
 
 @item enable
 Boolean value: enable/disable FRC. Dynamic value, can be altered at a runtime
diff --git a/libavfilter/vf_frc_amf.c b/libavfilter/vf_frc_amf.c
index 743ac5dd0ba..e956c692a2c 100644
--- a/libavfilter/vf_frc_amf.c
+++ b/libavfilter/vf_frc_amf.c
@@ -72,6 +72,7 @@ static int amf_filter_query_formats(AVFilterContext *avctx)
         AV_PIX_FMT_AMF_SURFACE,
         AV_PIX_FMT_D3D11,
         AV_PIX_FMT_DXVA2_VLD,
+        AV_PIX_FMT_VULKAN,
         AV_PIX_FMT_NV12,
         AV_PIX_FMT_P010,
         AV_PIX_FMT_BGRA,
@@ -130,6 +131,8 @@ static int amf_frc_filter_config_output(AVFilterLink 
*outlink)
             engine_type = FRC_ENGINE_DX11;
     }
 #endif
+    if (engine_type == -1 && av_amf_get_memory_type(device_ctx) == 
AMF_MEMORY_VULKAN)
+        engine_type = FRC_ENGINE_VULKAN;
     if (engine_type != -1)
         AMF_FRC_ASSIGN_PROPERTY_INT64_CHECK(avctx, amf_filter, 
AMF_FRC_ENGINE_TYPE, engine_type);
 
-- 
2.54.1

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

Reply via email to