Frames now cross between CUDA and D3D11 on the GPU instead of through
system memory. Each texture CUDA touches is imported once through the
external memory API, and a shared D3D11 fence, imported as a CUDA
external semaphore, chains the two queues on the GPU in both
directions, so no transfer waits on the CPU. CUDA cannot import the
KMT handles the bridge planes used to share, so they prefer NT handle
sharing now, with KMT kept for runtimes without it; Vulkan takes
either kind. No CUDA sharing API can address the planes of a two
plane texture, so those frames move through the plane bridge, and
single plane frames through an intermediate texture in their own
format, so the frame textures themselves, which may be recycled
decoder arrays, never have to be imported.
---
 libavutil/hwcontext_d3d11va.c          | 580 ++++++++++++++++++++++++-
 libavutil/hwcontext_d3d11va_internal.h |   2 +-
 2 files changed, 580 insertions(+), 2 deletions(-)

diff --git a/libavutil/hwcontext_d3d11va.c b/libavutil/hwcontext_d3d11va.c
index 023a8d7fe5..7b1bf6b130 100644
--- a/libavutil/hwcontext_d3d11va.c
+++ b/libavutil/hwcontext_d3d11va.c
@@ -38,6 +38,7 @@
 #include "hwcontext_d3d11va_internal.h"
 #include "hwcontext_internal.h"
 #if CONFIG_CUDA
+#include <d3d11_4.h>
 #include "cuda_check.h"
 #include "hwcontext_cuda_internal.h"
 #define CHECK_CU(x) FF_CUDA_CHECK_DL(cuda_cu, cu, x)
@@ -98,8 +99,20 @@ typedef struct D3D11VAFramesContext {
     DXGI_FORMAT format;
 
     ID3D11Texture2D *staging_texture;
+
+#if CONFIG_CUDA
+    pthread_mutex_t          cuda_lock;
+    int                      cuda_lock_init;
+    struct D3D11CudaInterop *cuda_interop;
+#endif
 } D3D11VAFramesContext;
 
+#if CONFIG_CUDA
+static void d3d11va_cuda_interops_free(AVHWFramesContext *ctx);
+static int d3d11va_cuda_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
+                                      const AVFrame *src);
+#endif
+
 static const struct {
     DXGI_FORMAT d3d_format;
     enum AVPixelFormat pix_fmt;
@@ -141,6 +154,14 @@ static void d3d11va_frames_uninit(AVHWFramesContext *ctx)
     D3D11VAFramesContext *s = ctx->hwctx;
     AVD3D11VAFramesContext *frames_hwctx = &s->p;
 
+#if CONFIG_CUDA
+    d3d11va_cuda_interops_free(ctx);
+    if (s->cuda_lock_init) {
+        pthread_mutex_destroy(&s->cuda_lock);
+        s->cuda_lock_init = 0;
+    }
+#endif
+
     if (frames_hwctx->texture)
         ID3D11Texture2D_Release(frames_hwctx->texture);
     frames_hwctx->texture = NULL;
@@ -289,6 +310,12 @@ static int d3d11va_frames_init(AVHWFramesContext *ctx)
     HRESULT hr;
     D3D11_TEXTURE2D_DESC texDesc;
 
+#if CONFIG_CUDA
+    if (pthread_mutex_init(&s->cuda_lock, NULL))
+        return AVERROR(ENOMEM);
+    s->cuda_lock_init = 1;
+#endif
+
     for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
         if (ctx->sw_format == supported_formats[i].pix_fmt) {
             s->format = supported_formats[i].d3d_format;
@@ -459,6 +486,11 @@ static int d3d11va_transfer_data(AVHWFramesContext *ctx, 
AVFrame *dst,
     if (frame->hw_frames_ctx->data != (uint8_t *)ctx)
         return AVERROR(EINVAL);
 
+#if CONFIG_CUDA
+    if (other->format == AV_PIX_FMT_CUDA)
+        return d3d11va_cuda_transfer_data(ctx, dst, src);
+#endif
+
     /* Not a transfer to or from a software frame we can handle. Report this as
      * unimplemented rather than invalid, so that a hardware to hardware
      * transfer can still be tried from the other side. */
@@ -707,9 +739,17 @@ int ff_d3d11va_bridge_create(FFD3D11PlaneBridge **bridge, 
ID3D11Device *dev,
             .SampleDesc = { .Count = 1 },
             .Usage      = D3D11_USAGE_DEFAULT,
             .BindFlags  = D3D11_BIND_SHADER_RESOURCE | 
D3D11_BIND_UNORDERED_ACCESS,
-            .MiscFlags  = D3D11_RESOURCE_MISC_SHARED,
+            /* NT handle sharing where the runtime has it: CUDA cannot
+             * import KMT handles, and Vulkan takes either kind. */
+            .MiscFlags  = D3D11_RESOURCE_MISC_SHARED |
+                          D3D11_RESOURCE_MISC_SHARED_NTHANDLE,
         };
         hr = ID3D11Device_CreateTexture2D(dev, &pd, NULL, &b->planes[i]);
+        if (FAILED(hr)) {
+            /* D3D11.0 runtimes reject NT handle sharing. */
+            pd.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
+            hr = ID3D11Device_CreateTexture2D(dev, &pd, NULL, &b->planes[i]);
+        }
         if (SUCCEEDED(hr))
             hr = ID3D11Device_CreateShaderResourceView(dev,
                                                        (ID3D11Resource 
*)b->planes[i],
@@ -797,6 +837,544 @@ void ff_d3d11va_bridge_run(FFD3D11PlaneBridge *b, 
ID3D11DeviceContext *ctx,
 
 #endif /* CONFIG_VULKAN || CONFIG_CUDA */
 
+#if CONFIG_CUDA
+
+/* ffnvcodec does not carry these yet; the values come from cuda.h, where
+ * they have been since CUDA 10.0. */
+#define CU_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_RESOURCE \
+    ((CUexternalMemoryHandleType)6)
+#define CUDA_EXTERNAL_MEMORY_DEDICATED 0x1
+
+/* CUDA shares a D3D11 texture through the external memory API: each of the
+ * private textures below is imported once as a CUDA mipmapped array, and a
+ * shared D3D11 fence, imported as a CUDA external semaphore, chains the two
+ * queues on the GPU in both directions, so no transfer ever waits on the
+ * CPU. What no CUDA sharing API can do is reach the second plane of a
+ * two-plane texture, so those frames move through the plane bridge, and
+ * single-plane frames through an intermediate texture in their own format,
+ * so the frame textures themselves, which may be recycled decoder arrays,
+ * never have to be imported. One state is kept for each CUDA context frames
+ * have been transferred to or from, created once and kept; cuda_lock
+ * serializes the transfers, which keeps the shared textures owned by one
+ * transfer at a time and the fence values increasing in submission order,
+ * which monitored fences require of their signals. The copies ride the
+ * CUDA device's stream, so the D3D11 waits they end in also depend on
+ * whatever else the application keeps on that stream. */
+typedef struct D3D11CudaInterop {
+    struct D3D11CudaInterop *next;
+    AVBufferRef        *device_ref; /* keeps the device context and loader 
alive */
+    CUcontext           cuda_ctx;   /* identity of the pairing */
+    int                 status;     /* 0 untried, 1 ready, else error */
+    FFD3D11PlaneBridge *bridge;     /* two-plane formats */
+    ID3D11Texture2D    *tex;        /* single-plane intermediate */
+    ID3D11DeviceContext4 *ctx4;
+    ID3D11Fence        *fence;
+    CUexternalSemaphore sem;
+    CUexternalMemory    mem[2];
+    CUmipmappedArray    mip[2];
+    CUarray             arr[2];     /* level 0 of each texture */
+    int                 nb_planes;
+    uint64_t            fence_val;  /* last fence value handed out */
+    uint64_t            cuda_done;  /* last value CUDA was told to signal */
+} D3D11CudaInterop;
+
+static void d3d11va_cuda_interops_free(AVHWFramesContext *ctx)
+{
+    D3D11VAFramesContext *s = ctx->hwctx;
+    D3D11CudaInterop *ci = s->cuda_interop;
+
+    while (ci) {
+        D3D11CudaInterop *next = ci->next;
+        if (ci->device_ref) {
+            AVHWDeviceContext *dev_ctx = (AVHWDeviceContext 
*)ci->device_ref->data;
+            AVCUDADeviceContext *cu_hw = dev_ctx->hwctx;
+            CudaFunctions *cu = cu_hw->internal->cuda_dl;
+            CUcontext dummy;
+
+            if (cu->cuCtxPushCurrent(ci->cuda_ctx) == CUDA_SUCCESS) {
+                /* Transfers do not wait for their copies, so make sure none
+                 * is still using the imports about to go away. */
+                cu->cuStreamSynchronize(cu_hw->stream);
+                if (ci->sem)
+                    cu->cuDestroyExternalSemaphore(ci->sem);
+                for (int i = 0; i < FF_ARRAY_ELEMS(ci->mem); i++) {
+                    if (ci->mip[i])
+                        cu->cuMipmappedArrayDestroy(ci->mip[i]);
+                    if (ci->mem[i])
+                        cu->cuDestroyExternalMemory(ci->mem[i]);
+                }
+                cu->cuCtxPopCurrent(&dummy);
+            } else {
+                av_log(ctx, AV_LOG_WARNING, "The CUDA context is gone; its "
+                       "imports leak, and in-flight copies cannot be waited "
+                       "out\n");
+            }
+            av_buffer_unref(&ci->device_ref);
+        }
+        ff_d3d11va_bridge_free(&ci->bridge);
+        if (ci->tex)
+            ID3D11Texture2D_Release(ci->tex);
+        if (ci->fence)
+            ID3D11Fence_Release(ci->fence);
+        if (ci->ctx4)
+            ID3D11DeviceContext4_Release(ci->ctx4);
+        av_free(ci);
+        ci = next;
+    }
+    s->cuda_interop = NULL;
+}
+
+/* Texture layouts CUDA arrays can express. Packed formats without a
+ * matching array format alias one with the same texel size: the copies
+ * only move bytes. */
+static const struct {
+    DXGI_FORMAT d3d_format;
+    CUarray_format format;
+    int channels;
+} cuda_array_formats[] = {
+    { DXGI_FORMAT_R8_UNORM,           CU_AD_FORMAT_UNSIGNED_INT8,  1 },
+    { DXGI_FORMAT_R8G8_UNORM,         CU_AD_FORMAT_UNSIGNED_INT8,  2 },
+    { DXGI_FORMAT_R16_UNORM,          CU_AD_FORMAT_UNSIGNED_INT16, 1 },
+    { DXGI_FORMAT_R16G16_UNORM,       CU_AD_FORMAT_UNSIGNED_INT16, 2 },
+    { DXGI_FORMAT_B8G8R8A8_UNORM,     CU_AD_FORMAT_UNSIGNED_INT8,  4 },
+    { DXGI_FORMAT_R8G8B8A8_UNORM,     CU_AD_FORMAT_UNSIGNED_INT8,  4 },
+    { DXGI_FORMAT_R10G10B10A2_UNORM,  CU_AD_FORMAT_UNSIGNED_INT8,  4 },
+    { DXGI_FORMAT_R16G16B16A16_FLOAT, CU_AD_FORMAT_HALF,           4 },
+};
+
+/* Import one of our own textures, which is NT handle shared, as a CUDA
+ * array. Partial state is left in place for a retry, and freed with the
+ * interop state. */
+static int d3d11va_cuda_import_texture(CudaFunctions *cu,
+                                       ID3D11Texture2D *tex,
+                                       CUexternalMemory *mem,
+                                       CUmipmappedArray *mip, CUarray *arr)
+{
+    D3D11_TEXTURE2D_DESC desc;
+    CUarray_format format = 0;
+    int channels = 0;
+    CUresult ret;
+
+    ID3D11Texture2D_GetDesc(tex, &desc);
+    for (int i = 0; i < FF_ARRAY_ELEMS(cuda_array_formats); i++) {
+        if (cuda_array_formats[i].d3d_format == desc.Format) {
+            format   = cuda_array_formats[i].format;
+            channels = cuda_array_formats[i].channels;
+            break;
+        }
+    }
+    /* KMT handles import into CUDA without an error, and then every access
+     * faults. */
+    if (!channels || !(desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_NTHANDLE))
+        return AVERROR(ENOSYS);
+
+    if (!*mem) {
+        int texel = channels * (format == CU_AD_FORMAT_UNSIGNED_INT8 ? 1 : 2);
+        IDXGIResource1 *res1;
+        HANDLE handle;
+        HRESULT hr;
+        /* A dedicated import is bound to the resource, whose allocation
+         * size the driver knows; the size field only has to be plausible,
+         * which is fortunate, because D3D11 never reports it. */
+        CUDA_EXTERNAL_MEMORY_HANDLE_DESC mdesc = {
+            .type  = CU_EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_RESOURCE,
+            .size  = (uint64_t)desc.Width * desc.Height * texel,
+            .flags = CUDA_EXTERNAL_MEMORY_DEDICATED,
+        };
+
+        hr = ID3D11Texture2D_QueryInterface(tex, &IID_IDXGIResource1,
+                                            (void **)&res1);
+        if (FAILED(hr))
+            return AVERROR(ENOSYS);
+        hr = IDXGIResource1_CreateSharedHandle(res1, NULL,
+                                               DXGI_SHARED_RESOURCE_READ |
+                                               DXGI_SHARED_RESOURCE_WRITE,
+                                               NULL, &handle);
+        IDXGIResource1_Release(res1);
+        if (FAILED(hr))
+            return AVERROR(ENOSYS);
+
+        mdesc.handle.win32.handle = handle;
+        ret = cu->cuImportExternalMemory(mem, &mdesc);
+        CloseHandle(handle);
+        if (ret != CUDA_SUCCESS)
+            return AVERROR(ENOSYS);
+    }
+
+    if (!*mip) {
+        CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC adesc = {
+            .arrayDesc = {
+                .Width       = desc.Width,
+                .Height      = desc.Height,
+                .Format      = format,
+                .NumChannels = channels,
+            },
+            .numLevels = 1,
+        };
+        ret = cu->cuExternalMemoryGetMappedMipmappedArray(mip, *mem, &adesc);
+        if (ret != CUDA_SUCCESS)
+            return AVERROR(ENOSYS);
+    }
+
+    ret = cu->cuMipmappedArrayGetLevel(arr, *mip, 0);
+    if (ret != CUDA_SUCCESS)
+        return AVERROR(ENOSYS);
+    return 0;
+}
+
+/* Find or create the state for this CUDA context, cuda_lock held. A failed
+ * creation attempt is kept, so it is not retried every frame, and whatever
+ * it did create is released with the frames context. */
+static int d3d11va_cuda_interop_get(AVHWFramesContext *ctx,
+                                    AVHWFramesContext *cuda_fc,
+                                    D3D11CudaInterop **out)
+{
+    D3D11VAFramesContext *s = ctx->hwctx;
+    AVD3D11VADeviceContext *hwctx = ctx->device_ctx->hwctx;
+    AVCUDADeviceContext *cu_hw = cuda_fc->device_ctx->hwctx;
+    CudaFunctions *cu = cu_hw->internal->cuda_dl;
+    const int planes = av_pix_fmt_count_planes(ctx->sw_format);
+    ID3D11Texture2D *plane_tex[2];
+    D3D11CudaInterop *ci;
+    CUcontext dummy;
+    HRESULT hr;
+    int err;
+
+    for (ci = s->cuda_interop; ci; ci = ci->next)
+        if (ci->cuda_ctx == cu_hw->cuda_ctx)
+            break;
+
+    if (!ci) {
+        ci = av_mallocz(sizeof(*ci));
+        if (!ci)
+            return AVERROR(ENOMEM);
+        ci->cuda_ctx   = cu_hw->cuda_ctx;
+        ci->device_ref = av_buffer_ref(cuda_fc->device_ref);
+        if (!ci->device_ref) {
+            av_free(ci);
+            return AVERROR(ENOMEM);
+        }
+        ci->next = s->cuda_interop;
+        s->cuda_interop = ci;
+    }
+    if (ci->status) {
+        *out = ci;
+        return ci->status > 0 ? 0 : ci->status;
+    }
+
+    if (planes == 2) {
+        if (!ci->bridge) {
+            err = ff_d3d11va_bridge_create(&ci->bridge, hwctx->device,
+                                           ctx->width, ctx->height,
+                                           ctx->sw_format, ctx);
+            if (err < 0)
+                goto fail;
+        }
+        plane_tex[0] = ci->bridge->planes[0];
+        plane_tex[1] = ci->bridge->planes[1];
+        ci->nb_planes = 2;
+    } else {
+        D3D11_TEXTURE2D_DESC desc = {
+            .Width      = ctx->width,
+            .Height     = ctx->height,
+            .MipLevels  = 1,
+            .ArraySize  = 1,
+            .Format     = s->format,
+            .SampleDesc = { .Count = 1 },
+            .Usage      = D3D11_USAGE_DEFAULT,
+            .MiscFlags  = D3D11_RESOURCE_MISC_SHARED |
+                          D3D11_RESOURCE_MISC_SHARED_NTHANDLE,
+        };
+        if (!ci->tex) {
+            hr = ID3D11Device_CreateTexture2D(hwctx->device, &desc, NULL,
+                                              &ci->tex);
+            if (FAILED(hr)) {
+                av_log(ctx, AV_LOG_DEBUG, "Cannot create the intermediate "
+                       "texture (%lx)\n", (long)hr);
+                err = AVERROR(ENOSYS);
+                goto fail;
+            }
+        }
+        plane_tex[0] = ci->tex;
+        ci->nb_planes = 1;
+    }
+
+    if (!ci->fence) {
+        ID3D11Device5 *dev5;
+        hr = ID3D11Device_QueryInterface(hwctx->device, &IID_ID3D11Device5,
+                                         (void **)&dev5);
+        if (SUCCEEDED(hr)) {
+            hr = ID3D11Device5_CreateFence(dev5, 0, D3D11_FENCE_FLAG_SHARED,
+                                           &IID_ID3D11Fence,
+                                           (void **)&ci->fence);
+            ID3D11Device5_Release(dev5);
+        }
+        if (FAILED(hr)) {
+            av_log(ctx, AV_LOG_DEBUG, "Cannot create a shared fence (%lx)\n",
+                   (long)hr);
+            err = AVERROR(ENOSYS);
+            goto fail;
+        }
+    }
+    if (!ci->ctx4) {
+        hr = ID3D11DeviceContext_QueryInterface(hwctx->device_context,
+                                                &IID_ID3D11DeviceContext4,
+                                                (void **)&ci->ctx4);
+        if (FAILED(hr)) {
+            err = AVERROR(ENOSYS);
+            goto fail;
+        }
+    }
+
+    /* The imports double as the capability probe: they fail when the CUDA
+     * context does not sit on the same device as the D3D11 one, and the
+     * caller falls back to system memory. */
+    if (cu->cuCtxPushCurrent(ci->cuda_ctx) != CUDA_SUCCESS) {
+        err = AVERROR_EXTERNAL;
+        goto fail;
+    }
+    err = 0;
+    if (!ci->sem) {
+        HANDLE handle;
+        hr = ID3D11Fence_CreateSharedHandle(ci->fence, NULL, GENERIC_ALL,
+                                            NULL, &handle);
+        if (SUCCEEDED(hr)) {
+            CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC sdesc = {
+                .type = CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE,
+                .handle.win32.handle = handle,
+            };
+            if (cu->cuImportExternalSemaphore(&ci->sem, &sdesc) != 
CUDA_SUCCESS)
+                err = AVERROR(ENOSYS);
+            CloseHandle(handle);
+        } else {
+            err = AVERROR(ENOSYS);
+        }
+    }
+    for (int i = 0; err >= 0 && i < ci->nb_planes; i++)
+        err = d3d11va_cuda_import_texture(cu, plane_tex[i], &ci->mem[i],
+                                          &ci->mip[i], &ci->arr[i]);
+    cu->cuCtxPopCurrent(&dummy);
+    if (err < 0) {
+        av_log(ctx, AV_LOG_DEBUG, "Cannot import a texture into CUDA\n");
+        goto fail;
+    }
+
+    ci->status = 1;
+    *out = ci;
+    return 0;
+
+fail:
+    /* Only the capability probe is worth remembering: a transient failure
+     * like ENOMEM must not condemn the pairing for good. */
+    if (err == AVERROR(ENOSYS))
+        ci->status = err;
+    *out = ci;
+    return err;
+}
+
+static int d3d11va_cuda_transfer_data(AVHWFramesContext *ctx, AVFrame *dst,
+                                      const AVFrame *src)
+{
+    D3D11VAFramesContext *s = ctx->hwctx;
+    AVD3D11VADeviceContext *hwctx = ctx->device_ctx->hwctx;
+    const int to_cuda = dst->format == AV_PIX_FMT_CUDA;
+    const AVFrame *cudaf = to_cuda ? dst : src;
+    const AVFrame *d3df  = to_cuda ? src : dst;
+    ID3D11Resource *tex = (ID3D11Resource *)d3df->data[0];
+    UINT index = (UINT)(intptr_t)d3df->data[1];
+    const int planes = av_pix_fmt_count_planes(ctx->sw_format);
+    const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(ctx->sw_format);
+    /* Like the system memory paths, only the common region is copied; with
+     * differing sizes the margin keeps whatever the long-lived intermediate
+     * textures held before, and those are sized by the frames context. */
+    int w = FFMIN3(dst->width,  src->width,  ctx->width);
+    int h = FFMIN3(dst->height, src->height, ctx->height);
+    AVHWFramesContext *cuda_fc;
+    AVHWDeviceContext *cuda_cu;
+    AVCUDADeviceContext *cu_hw;
+    CudaFunctions *cu;
+    D3D11CudaInterop *ci;
+    D3D11_TEXTURE2D_DESC desc;
+    D3D11_BOX box = { 0, 0, 0, ctx->width, ctx->height, 1 };
+    CUcontext dummy;
+    uint64_t v1, v2;
+    HRESULT hr;
+    int ret;
+
+    if (!cudaf->hw_frames_ctx)
+        return AVERROR(ENOSYS);
+    cuda_fc = (AVHWFramesContext *)cudaf->hw_frames_ctx->data;
+    if (cuda_fc->format != AV_PIX_FMT_CUDA)
+        return AVERROR(ENOSYS);
+    cuda_cu = cuda_fc->device_ctx;
+    cu_hw   = cuda_fc->device_ctx->hwctx;
+    cu      = cu_hw->internal->cuda_dl;
+
+    /* The copy moves bits, so the two sides have to agree on what the bits
+     * mean, and there is no way to address the planes of more of them.
+     * Packed subsampled formats are also out: their textures constrain the
+     * copy regions in ways this code does not track. */
+    if (cuda_fc->sw_format != ctx->sw_format || planes < 1 || planes > 2 ||
+        (planes == 1 && (pixdesc->log2_chroma_w || pixdesc->log2_chroma_h)))
+        return AVERROR(ENOSYS);
+
+    /* The staging copies address the frame-sized region of the texture,
+     * which decoders often pad, and copies of video formats only accept
+     * aligned regions. The texture must really be in the format the sw
+     * format implies, or the copies would silently move nothing. A
+     * keyed-mutex texture is only coherent for a user that acquires the
+     * mutex, which this code does not do. */
+    ID3D11Texture2D_GetDesc((ID3D11Texture2D *)tex, &desc);
+    if (desc.Width < ctx->width || desc.Height < ctx->height ||
+        desc.SampleDesc.Count != 1 ||
+        (desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX))
+        return AVERROR(ENOSYS);
+    if (planes == 2) {
+        DXGI_FORMAT tex_fmt, plane_fmts[2];
+        if (((ctx->width | ctx->height) & 1) ||
+            ff_d3d11va_bridge_formats(ctx->sw_format, &tex_fmt, plane_fmts) < 
0 ||
+            desc.Format != tex_fmt)
+            return AVERROR(ENOSYS);
+    } else {
+        /* Formats no CUDA array layout matches are rejected before the
+         * interop state allocates anything for them. */
+        int i;
+        for (i = 0; i < FF_ARRAY_ELEMS(cuda_array_formats); i++)
+            if (cuda_array_formats[i].d3d_format == s->format)
+                break;
+        if (desc.Format != s->format ||
+            i == FF_ARRAY_ELEMS(cuda_array_formats))
+            return AVERROR(ENOSYS);
+    }
+
+    pthread_mutex_lock(&s->cuda_lock);
+
+    ret = d3d11va_cuda_interop_get(ctx, cuda_fc, &ci);
+    if (ret < 0)
+        goto end;
+    /* Assembling a frame writes the staging texture through views its format
+     * does not support everywhere. */
+    if (planes == 2 && !to_cuda && !ci->bridge->staging_uav[0]) {
+        ret = AVERROR(ENOSYS);
+        goto end;
+    }
+
+    v1 = ++ci->fence_val;
+    v2 = ++ci->fence_val;
+
+    hwctx->lock(hwctx->lock_ctx);
+    /* The last transfer returned while its copies could still be touching
+     * these textures; the wait orders whatever comes next after them, on
+     * the GPU. Failures have to surface before anything crosses the APIs:
+     * CUDA waiting on a signal that never got submitted would block its
+     * stream for good. */
+    hr = ID3D11DeviceContext4_Wait(ci->ctx4, ci->fence, ci->cuda_done);
+    if (SUCCEEDED(hr) && to_cuda) {
+        if (planes == 2)
+            ff_d3d11va_bridge_run(ci->bridge, hwctx->device_context, tex,
+                                  index, 1);
+        else
+            ID3D11DeviceContext_CopySubresourceRegion(hwctx->device_context,
+                (ID3D11Resource *)ci->tex, 0, 0, 0, 0, tex, index, &box);
+    }
+    /* The signal orders all D3D11 work issued so far ahead of the CUDA
+     * copies, whether it staged the frame or still reads the textures from
+     * an earlier download; the flush submits it, or CUDA would wait on
+     * work still sitting in the command buffer. */
+    if (SUCCEEDED(hr))
+        hr = ID3D11DeviceContext4_Signal(ci->ctx4, ci->fence, v1);
+    ID3D11DeviceContext_Flush(hwctx->device_context);
+    hwctx->unlock(hwctx->lock_ctx);
+    if (FAILED(hr)) {
+        ret = AVERROR_EXTERNAL;
+        goto end;
+    }
+
+    ret = CHECK_CU(cu->cuCtxPushCurrent(ci->cuda_ctx));
+    if (ret < 0)
+        goto end;
+
+    {
+        CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS wait = {
+            .params.fence.value = v1,
+        };
+        ret = CHECK_CU(cu->cuWaitExternalSemaphoresAsync(&ci->sem, &wait, 1,
+                                                         cu_hw->stream));
+    }
+
+    for (int i = 0; ret >= 0 && i < planes; i++) {
+        CUDA_MEMCPY2D cpy = {
+            .WidthInBytes = av_image_get_linesize(ctx->sw_format, w, i),
+            .Height       = i ? AV_CEIL_RSHIFT(h, pixdesc->log2_chroma_h) : h,
+        };
+
+        if (to_cuda) {
+            cpy.srcMemoryType = CU_MEMORYTYPE_ARRAY;
+            cpy.srcArray      = ci->arr[i];
+            cpy.dstMemoryType = CU_MEMORYTYPE_DEVICE;
+            cpy.dstDevice     = (CUdeviceptr)(uintptr_t)cudaf->data[i];
+            cpy.dstPitch      = cudaf->linesize[i];
+        } else {
+            cpy.srcMemoryType = CU_MEMORYTYPE_DEVICE;
+            cpy.srcDevice     = (CUdeviceptr)(uintptr_t)cudaf->data[i];
+            cpy.srcPitch      = cudaf->linesize[i];
+            cpy.dstMemoryType = CU_MEMORYTYPE_ARRAY;
+            cpy.dstArray      = ci->arr[i];
+        }
+        ret = CHECK_CU(cu->cuMemcpy2DAsync(&cpy, cu_hw->stream));
+    }
+
+    if (ret >= 0) {
+        CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS signal = {
+            .params.fence.value = v2,
+        };
+        ret = CHECK_CU(cu->cuSignalExternalSemaphoresAsync(&ci->sem, &signal,
+                                                           1, cu_hw->stream));
+    }
+    if (ret >= 0) {
+        /* No synchronize: the copies stay ordered on the stream for CUDA
+         * consumers, and behind the fence value for D3D11 ones. */
+        ci->cuda_done = v2;
+    } else {
+        /* v2 never signals now, and since cuda_done was not advanced,
+         * nothing will ever wait on it; the next transfer signals a higher
+         * value, which is all a monitored fence asks. */
+        CHECK_CU(cu->cuStreamSynchronize(cu_hw->stream));
+    }
+    CHECK_CU(cu->cuCtxPopCurrent(&dummy));
+    if (ret < 0)
+        goto end;
+
+    if (!to_cuda) {
+        /* Only past the fence value do the textures hold the frame; the
+         * wait keeps the assembly behind them, on the GPU. If it cannot be
+         * enqueued, the destination must not be assembled from stale
+         * planes. */
+        hwctx->lock(hwctx->lock_ctx);
+        hr = ID3D11DeviceContext4_Wait(ci->ctx4, ci->fence, v2);
+        if (SUCCEEDED(hr)) {
+            if (planes == 2)
+                ff_d3d11va_bridge_run(ci->bridge, hwctx->device_context, tex,
+                                      index, 0);
+            else
+                
ID3D11DeviceContext_CopySubresourceRegion(hwctx->device_context,
+                    tex, index, 0, 0, 0, (ID3D11Resource *)ci->tex, 0, NULL);
+        }
+        hwctx->unlock(hwctx->lock_ctx);
+        if (FAILED(hr)) {
+            ret = AVERROR_EXTERNAL;
+            goto end;
+        }
+    }
+    ret = 0;
+
+end:
+    pthread_mutex_unlock(&s->cuda_lock);
+    return ret;
+}
+
+#endif /* CONFIG_CUDA */
+
 static int d3d11va_device_init(AVHWDeviceContext *hwdev)
 {
     AVD3D11VADeviceContext *device_hwctx = hwdev->hwctx;
diff --git a/libavutil/hwcontext_d3d11va_internal.h 
b/libavutil/hwcontext_d3d11va_internal.h
index 0b78aeac5a..3ceaa3d3ef 100644
--- a/libavutil/hwcontext_d3d11va_internal.h
+++ b/libavutil/hwcontext_d3d11va_internal.h
@@ -40,7 +40,7 @@ typedef struct FFD3D11PlaneBridge {
     ID3D11Texture2D           *staging;    /* in the two-plane format */
     ID3D11ShaderResourceView  *staging_srv[2];
     ID3D11UnorderedAccessView *staging_uav[2]; /* missing without UAV support 
*/
-    ID3D11Texture2D           *planes[2];  /* created with MISC_SHARED */
+    ID3D11Texture2D           *planes[2];  /* shared, NT handle if possible */
     ID3D11ShaderResourceView  *plane_srv[2];
     ID3D11UnorderedAccessView *plane_uav[2];
     int                        width;
-- 
2.45.1.windows.1

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

Reply via email to