D3D11VA implemented no device_derive, so av_hwdevice_ctx_create_derived()
targeting AV_HWDEVICE_TYPE_D3D11VA always failed with ENOSYS and callers
had to fall back to creating a standalone device.
A standalone device is created with a NULL DXGI adapter, which selects
whichever adapter enumerates first rather than the one the source device
runs on. On a multi-adapter system that can be a different GPU entirely,
so a caller ends up using a device on another GPU than the component that
provided the source device.
Derive by matching the source device's LUID against the AdapterLuid of
each DXGI adapter and creating the device on the one that matches. The
LUID comes from VkPhysicalDeviceIDProperties for a Vulkan source and from
cuDeviceGetLuid() for a CUDA source. This reuses d3d11va_device_create()
with the resulting adapter index, so the option, debug and multithread
handling is not duplicated.
cuDeviceGetLuid() is loaded optionally by ffnvcodec, so its absence on an
older driver is reported as ENOSYS rather than treated as an error.
---
libavutil/hwcontext_d3d11va.c | 129 ++++++++++++++++++++++++++++++++++
1 file changed, 129 insertions(+)
diff --git a/libavutil/hwcontext_d3d11va.c b/libavutil/hwcontext_d3d11va.c
index 834c2ce3dd..904ccd55d5 100644
--- a/libavutil/hwcontext_d3d11va.c
+++ b/libavutil/hwcontext_d3d11va.c
@@ -35,6 +35,14 @@
#include "hwcontext.h"
#include "hwcontext_d3d11va.h"
#include "hwcontext_internal.h"
+#if CONFIG_CUDA
+#include "cuda_check.h"
+#include "hwcontext_cuda_internal.h"
+#define CHECK_CU(x) FF_CUDA_CHECK_DL(cuda_cu, cu, x)
+#endif
+#if CONFIG_VULKAN
+#include "hwcontext_vulkan.h"
+#endif
#include "imgutils.h"
#include "mem.h"
#include "pixdesc.h"
@@ -729,6 +737,126 @@ static int d3d11va_device_create(AVHWDeviceContext *ctx,
const char *device,
return 0;
}
+static int d3d11va_device_find_adapter_by_luid(AVHWDeviceContext *ctx,
+ const LUID *luid)
+{
+ HRESULT hr;
+ IDXGIAdapter *adapter = NULL;
+ IDXGIFactory2 *factory;
+ int adapter_id = 0;
+ int ret = -1;
+
+ hr = mCreateDXGIFactory(&IID_IDXGIFactory2, (void **)&factory);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_ERROR, "CreateDXGIFactory returned error\n");
+ return -1;
+ }
+
+ while (IDXGIFactory2_EnumAdapters(factory, adapter_id++, &adapter) !=
DXGI_ERROR_NOT_FOUND) {
+ DXGI_ADAPTER_DESC adapter_desc;
+
+ hr = IDXGIAdapter2_GetDesc(adapter, &adapter_desc);
+ IDXGIAdapter_Release(adapter);
+ if (FAILED(hr)) {
+ av_log(ctx, AV_LOG_DEBUG, "IDXGIAdapter2_GetDesc returned error,
try next adapter\n");
+ continue;
+ }
+
+ if (adapter_desc.AdapterLuid.LowPart == luid->LowPart &&
+ adapter_desc.AdapterLuid.HighPart == luid->HighPart) {
+ ret = adapter_id - 1;
+ break;
+ }
+ }
+
+ IDXGIFactory2_Release(factory);
+ return ret;
+}
+
+static int d3d11va_device_derive(AVHWDeviceContext *ctx,
+ AVHWDeviceContext *src_ctx,
+ AVDictionary *opts, int flags)
+{
+ LUID luid;
+ int adapter, ret;
+ char adapter_str[16];
+
+ if ((ret = ff_thread_once(&functions_loaded, load_functions)) != 0)
+ return AVERROR_UNKNOWN;
+ if (!mD3D11CreateDevice || !mCreateDXGIFactory) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to load D3D11 library or its
functions\n");
+ return AVERROR_UNKNOWN;
+ }
+
+ switch (src_ctx->type) {
+#if CONFIG_VULKAN
+ case AV_HWDEVICE_TYPE_VULKAN: {
+ AVVulkanDeviceContext *src_hwctx = src_ctx->hwctx;
+ PFN_vkGetPhysicalDeviceProperties2 prop_fn;
+ VkPhysicalDeviceIDProperties vk_idp = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES,
+ };
+ VkPhysicalDeviceProperties2 vk_dev_props = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
+ .pNext = &vk_idp,
+ };
+
+ prop_fn = (PFN_vkGetPhysicalDeviceProperties2)
+ src_hwctx->get_proc_addr(src_hwctx->inst,
+ "vkGetPhysicalDeviceProperties2");
+ if (!prop_fn)
+ return AVERROR(ENOSYS);
+
+ prop_fn(src_hwctx->phys_dev, &vk_dev_props);
+ if (!vk_idp.deviceLUIDValid) {
+ av_log(ctx, AV_LOG_VERBOSE, "Source device does not expose a
LUID\n");
+ return AVERROR(ENOSYS);
+ }
+
+ // VK_LUID_SIZE is defined as 8, which is also the size of a LUID.
+ memcpy(&luid, vk_idp.deviceLUID, sizeof(luid));
+ break;
+ }
+#endif
+#if CONFIG_CUDA
+ case AV_HWDEVICE_TYPE_CUDA: {
+ AVHWDeviceContext *cuda_cu = src_ctx;
+ AVCUDADeviceContext *src_hwctx = src_ctx->hwctx;
+ AVCUDADeviceContextInternal *cu_internal = src_hwctx->internal;
+ CudaFunctions *cu = cu_internal->cuda_dl;
+ unsigned int node_mask;
+
+ // Optionally loaded, so it can be absent on an older driver.
+ if (!cu->cuDeviceGetLuid) {
+ av_log(ctx, AV_LOG_VERBOSE, "cuDeviceGetLuid is unavailable\n");
+ return AVERROR(ENOSYS);
+ }
+
+ // The LUID is documented as 8 bytes, matching a Windows LUID.
+ ret = CHECK_CU(cu->cuDeviceGetLuid((char *)&luid, &node_mask,
+ cu_internal->cuda_device));
+ if (ret < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Unable to get LUID from CUDA\n");
+ return AVERROR_EXTERNAL;
+ }
+ break;
+ }
+#endif
+ default:
+ return AVERROR(ENOSYS);
+ }
+
+ adapter = d3d11va_device_find_adapter_by_luid(ctx, &luid);
+ if (adapter < 0) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to find a d3d11va adapter matching "
+ "the source device\n");
+ return AVERROR(ENODEV);
+ }
+
+ snprintf(adapter_str, sizeof(adapter_str), "%d", adapter);
+ return d3d11va_device_create(ctx, adapter_str, opts, flags);
+}
+
const HWContextType ff_hwcontext_type_d3d11va = {
.type = AV_HWDEVICE_TYPE_D3D11VA,
.name = "D3D11VA",
@@ -737,6 +865,7 @@ const HWContextType ff_hwcontext_type_d3d11va = {
.frames_hwctx_size = sizeof(D3D11VAFramesContext),
.device_create = d3d11va_device_create,
+ .device_derive = d3d11va_device_derive,
.device_init = d3d11va_device_init,
.device_uninit = d3d11va_device_uninit,
.frames_get_constraints = d3d11va_frames_get_constraints,
--
2.45.1.windows.1
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]