PR #26251 opened by JuliusBairaktaris URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26251 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26251.patch
Depends on #23019, whose commit this is stacked on; only the last commit belongs to this pull request. This is the application-owned AMF device I asked about in #23019: an `AMFContext` that the application creates, keeps using for its own components, and hands to FFmpeg through `av_hwdevice_ctx_alloc()` + `AVAMFDeviceContext.context`. @DmitriiGershenkop listed what stands in the way today, and this implements that list. @MikhailAMD, I'd like your opinion on the semantics before this goes further. - `avutil/hwcontext_amf: support an AMF context owned by the caller` - `amf_device_uninit()` terminates only a context that hwcontext_amf created itself (`av_hwdevice_ctx_create()` or derivation). For a context set by the caller it releases the reference and nothing else. The flag lives in the private device struct that #23019 adds, so the public struct is unchanged. - `av_hwdevice_ctx_init()` loads the runtime and sets `library`, `factory` and `version` when the caller left `factory` unset. A caller that sets `factory` has to set `version` too, otherwise init fails with EINVAL. - `av_hwdevice_ctx_init()` fails with EINVAL instead of dereferencing a null pointer when no context was set. - The caller's context may already be initialized for a device; if it is not, init initializes it as before. `lock`, `unlock` and `lock_ctx` can be set by the caller as before. - Documented in `hwcontext_amf.h`, with an APIchanges entry (lavu 61.21.102). Tested on Windows 11, RX 9070 XT, driver 32.0.32029.2010, AMF runtime 1.5.3.0, with a program that creates an `AMFContext`, calls `InitDX11()`, hands it to an allocated AMF device and decodes H.264 with h264_amf to AMF surfaces: | caller sets | before | now | |---|---|---| | context | crash when the decoder is opened (factory is null) | decodes; version 1.5.3.0; factory equals the one from the caller's `AMFInit()` | | context, factory, version | decodes, but after the device is freed the caller's context has no D3D11 device and `AllocSurface()` fails | decodes; after the device is freed the context still returns its D3D11 device and allocates a surface | | context, not initialized | crash as in the first row | initialized by `av_hwdevice_ctx_init()`, decodes | | context, factory without version | accepted with version 0 | EINVAL | | nothing | null dereference | EINVAL | The decoded picture is the same as with a device from `av_hwdevice_ctx_create()`, which behaves as before. Open point for AMD: the factory that FFmpeg gets from its own `AMFInit()` was the same object as the caller's in my runs. If that is not guaranteed by the runtime, the caller should always pass its factory, and I would make that a requirement instead. >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 1/2] 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 1040d50cbf90be52e30d67100e33be1f2b6de314 Mon Sep 17 00:00:00 2001 From: Julius Bairaktaris <[email protected]> Date: Sat, 21 Nov 2026 18:30:09 +0100 Subject: [PATCH 2/2] avutil/hwcontext_amf: support an AMF context owned by the caller A caller can allocate an AMF device with av_hwdevice_ctx_alloc() and set AVAMFDeviceContext.context to a context of its own, but the device then behaves as if FFmpeg had created the context: - amf_device_uninit() calls Terminate() on it, so the caller's context loses its device as soon as FFmpeg frees the hardware device, although the caller may still run its own components on it. - factory and version are only set by av_hwdevice_ctx_create(). With the factory unset the decoders, encoders and filters dereference a null pointer when they create their component, and with the version unset the runtime version checks in amfdec and amfenc see version 0. - av_hwdevice_ctx_init() dereferences the context without checking that one was set. Terminate only a context that hwcontext_amf created itself and release just the reference otherwise. In av_hwdevice_ctx_init(), load the runtime and set library, factory and version when the caller left the factory unset, require the version when the caller did set the factory, and fail with EINVAL when no context was set. amf_load_library() moves above its new caller unchanged. Document this in the header. Tested on Windows 11, RX 9070 XT, driver 32.0.32029.2010, AMF runtime 1.5.3.0, with a program that creates an AMFContext, initializes it with InitDX11(), hands it to an allocated AMF device and decodes H.264 with h264_amf to AMF surfaces on it. Before, the process crashed when the decoder was opened if the factory was unset; with factory and version set, decoding worked, but after the device was freed the context returned no D3D11 device and AllocSurface() failed. Now both variants decode the same picture as a device from av_hwdevice_ctx_create(), the version is 1.5.3.0, the factory is the one the caller got from AMFInit(), and after the device is freed the context still returns its D3D11 device and allocates a surface. A context that the caller did not initialize is initialized by av_hwdevice_ctx_init() and decodes too; a factory without version and a missing context fail with EINVAL. Signed-off-by: Julius Bairaktaris <[email protected]> --- doc/APIchanges | 6 ++++ libavutil/hwcontext_amf.c | 72 ++++++++++++++++++++++++--------------- libavutil/hwcontext_amf.h | 19 +++++++++++ libavutil/version.h | 2 +- 4 files changed, 71 insertions(+), 28 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index 3eac08a0542..65c3cf863f4 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -2,6 +2,12 @@ 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 + An AMFContext set by the caller in AVAMFDeviceContext.context is no longer + terminated when the device is freed. av_hwdevice_ctx_init() loads the AMF + runtime and sets AVAMFDeviceContext.factory and .version if the caller + left the factory unset. + 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 7709614ebde..6d0139f72b4 100644 --- a/libavutil/hwcontext_amf.c +++ b/libavutil/hwcontext_amf.c @@ -56,6 +56,7 @@ typedef struct AMFDevicePriv { AVAMFDeviceContext p; + int own_context; #if CONFIG_VULKAN AMFVulkanDevice vk_dev; #endif @@ -539,14 +540,16 @@ static int amf_transfer_data_from(AVHWFramesContext *ctx, AVFrame *dst, static void amf_device_uninit(AVHWDeviceContext *device_ctx) { - AVAMFDeviceContext *amf_ctx = device_ctx->hwctx; + AMFDevicePriv *priv = device_ctx->hwctx; + AVAMFDeviceContext *amf_ctx = &priv->p; AMF_RESULT res = AMF_NOT_INITIALIZED; 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); + if (priv->own_context) + 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); @@ -632,6 +635,31 @@ enum AMF_MEMORY_TYPE av_amf_get_memory_type(AVAMFDeviceContext *amf_ctx) return AMF_MEMORY_UNKNOWN; } +static int amf_load_library(AVAMFDeviceContext* amf_ctx, void* avcl) +{ + AMFInit_Fn init_fun; + AMFQueryVersion_Fn version_fun; + AMF_RESULT res; + + amf_ctx->library = dlopen(AMF_DLL_NAMEA, RTLD_NOW | RTLD_LOCAL); + AMF_RETURN_IF_FALSE(avcl, amf_ctx->library != NULL, + AVERROR_UNKNOWN, "DLL %s failed to open\n", AMF_DLL_NAMEA); + + init_fun = (AMFInit_Fn)dlsym(amf_ctx->library, AMF_INIT_FUNCTION_NAME); + AMF_RETURN_IF_FALSE(avcl, init_fun != NULL, AVERROR_UNKNOWN, "DLL %s failed to find function %s\n", AMF_DLL_NAMEA, AMF_INIT_FUNCTION_NAME); + + version_fun = (AMFQueryVersion_Fn)dlsym(amf_ctx->library, AMF_QUERY_VERSION_FUNCTION_NAME); + AMF_RETURN_IF_FALSE(avcl, version_fun != NULL, AVERROR_UNKNOWN, "DLL %s failed to find function %s\n", AMF_DLL_NAMEA, AMF_QUERY_VERSION_FUNCTION_NAME); + + amf_uint64 version; + res = version_fun(&version); + AMF_RETURN_IF_FALSE(avcl, res == AMF_OK, AVERROR_UNKNOWN, "%s failed with error %d\n", AMF_QUERY_VERSION_FUNCTION_NAME, res); + amf_ctx->version = version; + res = init_fun(AMF_FULL_VERSION, &amf_ctx->factory); + AMF_RETURN_IF_FALSE(avcl, res == AMF_OK, AVERROR_UNKNOWN, "%s failed with error %d\n", AMF_INIT_FUNCTION_NAME, res); + return 0; +} + static int amf_device_init(AVHWDeviceContext *ctx) { AVAMFDeviceContext *amf_ctx = ctx->hwctx; @@ -648,6 +676,20 @@ static int amf_device_init(AVHWDeviceContext *ctx) }; #endif + if (!context) { + av_log(ctx, AV_LOG_ERROR, "No AMF context was set.\n"); + return AVERROR(EINVAL); + } + + if (!amf_ctx->factory) { + int ret = amf_load_library(amf_ctx, ctx); + if (ret < 0) + return ret; + } else if (!amf_ctx->version) { + av_log(ctx, AV_LOG_ERROR, "The AMF runtime version must be set together with the factory.\n"); + return AVERROR(EINVAL); + } + if (!amf_ctx->lock) { amf_ctx->lock_ctx = av_mallocz(sizeof(AVMutex)); if (!amf_ctx->lock_ctx) { @@ -714,31 +756,6 @@ static int amf_device_init(AVHWDeviceContext *ctx) return 0; } -static int amf_load_library(AVAMFDeviceContext* amf_ctx, void* avcl) -{ - AMFInit_Fn init_fun; - AMFQueryVersion_Fn version_fun; - AMF_RESULT res; - - amf_ctx->library = dlopen(AMF_DLL_NAMEA, RTLD_NOW | RTLD_LOCAL); - AMF_RETURN_IF_FALSE(avcl, amf_ctx->library != NULL, - AVERROR_UNKNOWN, "DLL %s failed to open\n", AMF_DLL_NAMEA); - - init_fun = (AMFInit_Fn)dlsym(amf_ctx->library, AMF_INIT_FUNCTION_NAME); - AMF_RETURN_IF_FALSE(avcl, init_fun != NULL, AVERROR_UNKNOWN, "DLL %s failed to find function %s\n", AMF_DLL_NAMEA, AMF_INIT_FUNCTION_NAME); - - version_fun = (AMFQueryVersion_Fn)dlsym(amf_ctx->library, AMF_QUERY_VERSION_FUNCTION_NAME); - AMF_RETURN_IF_FALSE(avcl, version_fun != NULL, AVERROR_UNKNOWN, "DLL %s failed to find function %s\n", AMF_DLL_NAMEA, AMF_QUERY_VERSION_FUNCTION_NAME); - - amf_uint64 version; - res = version_fun(&version); - AMF_RETURN_IF_FALSE(avcl, res == AMF_OK, AVERROR_UNKNOWN, "%s failed with error %d\n", AMF_QUERY_VERSION_FUNCTION_NAME, res); - amf_ctx->version = version; - res = init_fun(AMF_FULL_VERSION, &amf_ctx->factory); - AMF_RETURN_IF_FALSE(avcl, res == AMF_OK, AVERROR_UNKNOWN, "%s failed with error %d\n", AMF_INIT_FUNCTION_NAME, res); - return 0; -} - static int amf_device_create(AVHWDeviceContext *device_ctx, const char *device, AVDictionary *opts, int flags) @@ -796,6 +813,7 @@ static int amf_device_create(AVHWDeviceContext *device_ctx, ret = ctx->factory->pVtbl->CreateContext(ctx->factory, &ctx->context); if (ret == AMF_OK) { + ((AMFDevicePriv *)device_ctx->hwctx)->own_context = 1; AMF_ASSIGN_PROPERTY_INT64(ret, ctx->context, L"DeviceSurfaceCacheSize", 50 ); return 0; } diff --git a/libavutil/hwcontext_amf.h b/libavutil/hwcontext_amf.h index d2bbb418911..ab9e610a9a4 100644 --- a/libavutil/hwcontext_amf.h +++ b/libavutil/hwcontext_amf.h @@ -31,13 +31,32 @@ /** * This struct is allocated as AVHWDeviceContext.hwctx + * + * A caller that allocates the device with av_hwdevice_ctx_alloc() must set + * context before calling av_hwdevice_ctx_init(). Such a context is not + * terminated when the device is freed, only the reference is released, so + * the caller can keep using it through a reference of its own. */ typedef struct AVAMFDeviceContext { void * library; + /** + * The factory of the AMF runtime. + * + * A caller that sets context may leave this unset; av_hwdevice_ctx_init() + * then loads the runtime and sets library, factory and version. A caller + * that sets it must set version as well. + */ AMFFactory *factory; void *trace_writer; int64_t version; ///< version of AMF runtime + /** + * The AMF context. + * + * A context set by the caller may already be initialized for a device; + * otherwise av_hwdevice_ctx_init() initializes it. The reference is + * released when the device is freed. + */ AMFContext *context; AMF_MEMORY_TYPE memory_type; 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 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
