[vlc-devel] [PATCH 38/39] d3d11va: store the ID3D11DeviceContext in the video context

Steve Lhomme robux4 at ycbcr.xyz
Mon Oct 7 16:30:45 CEST 2019


We might store a reference to the VA module so the destructor of the module is
not called while the video context is used.
---
 include/vlc_picture.h            |  2 +-
 modules/codec/avcodec/d3d11va.c  |  5 ++++-
 modules/video_chroma/d3d11_fmt.c | 10 ++++++++++
 modules/video_chroma/d3d11_fmt.h | 12 ++++++++++++
 4 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/include/vlc_picture.h b/include/vlc_picture.h
index 92a8594725c..3298630e02a 100644
--- a/include/vlc_picture.h
+++ b/include/vlc_picture.h
@@ -94,7 +94,7 @@ enum vlc_video_context_type
     VLC_VIDEO_CONTEXT_VAAPI,
     VLC_VIDEO_CONTEXT_VDPAU,
     VLC_VIDEO_CONTEXT_DXVA2, /**< private: d3d9_video_context_t* */
-    VLC_VIDEO_CONTEXT_D3D11VA,
+    VLC_VIDEO_CONTEXT_D3D11VA,  /**< private: d3d11_video_context_t* */
     VLC_VIDEO_CONTEXT_AWINDOW,
     VLC_VIDEO_CONTEXT_NVDEC,
     VLC_VIDEO_CONTEXT_CVPX,
diff --git a/modules/codec/avcodec/d3d11va.c b/modules/codec/avcodec/d3d11va.c
index 4b079415e82..b660984b287 100644
--- a/modules/codec/avcodec/d3d11va.c
+++ b/modules/codec/avcodec/d3d11va.c
@@ -354,7 +354,7 @@ static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
             if (sys->d3d_dev.context_mutex == INVALID_HANDLE_VALUE)
                 msg_Warn(va, "No mutex found to lock the decoder");
 
-            sys->vctx = vlc_video_context_Create( dec_device, VLC_VIDEO_CONTEXT_D3D11VA, 0, NULL );
+            sys->vctx = vlc_video_context_Create( dec_device, VLC_VIDEO_CONTEXT_D3D11VA, sizeof(d3d11_video_context_t), &d3d11_vctx_ops );
             if (likely(sys->vctx != NULL))
             {
                 void *d3dvidctx = NULL;
@@ -366,6 +366,9 @@ static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
                     sys->vctx = NULL;
                 } else {
                     sys->hw.video_context = d3dvidctx;
+                    d3d11_video_context_t *priv = GetD3D11ContextPrivate(sys->vctx);
+                    priv->device = sys->d3d_dev.d3dcontext;
+                    ID3D11DeviceContext_Release(priv->device);
                 }
             }
         }
diff --git a/modules/video_chroma/d3d11_fmt.c b/modules/video_chroma/d3d11_fmt.c
index f2157e94d4e..1bbcb89e929 100644
--- a/modules/video_chroma/d3d11_fmt.c
+++ b/modules/video_chroma/d3d11_fmt.c
@@ -785,3 +785,13 @@ void D3D11_Destroy(d3d11_handle_t *hd3d)
 #endif
 #endif
 }
+
+static void ReleaseD3D11ContextPrivate(void *private)
+{
+    d3d11_video_context_t *octx = private;
+    ID3D11DeviceContext_Release(octx->device);
+}
+
+const struct vlc_video_context_operations d3d11_vctx_ops = {
+    ReleaseD3D11ContextPrivate,
+};
diff --git a/modules/video_chroma/d3d11_fmt.h b/modules/video_chroma/d3d11_fmt.h
index fea8064f690..a1ed7de8811 100644
--- a/modules/video_chroma/d3d11_fmt.h
+++ b/modules/video_chroma/d3d11_fmt.h
@@ -86,6 +86,11 @@ typedef struct
     ID3D11DeviceContext *device;
 } d3d11_decoder_device_t;
 
+typedef struct
+{
+    ID3D11DeviceContext *device;
+} d3d11_video_context_t;
+
 /* index to use for texture/resource that use a known DXGI format
  * (ie not DXGI_FORMAT_UNKNWON) */
 #define KNOWN_DXGI_INDEX   0
@@ -98,6 +103,8 @@ static inline bool is_d3d11_opaque(vlc_fourcc_t chroma)
            chroma == VLC_CODEC_D3D11_OPAQUE_BGRA;
 }
 
+const struct vlc_video_context_operations d3d11_vctx_ops;
+
 static inline d3d11_decoder_device_t *GetD3D11OpaqueDevice(vlc_decoder_device *device)
 {
     if (device == NULL || device->type != VLC_DECODER_DEVICE_D3D11VA)
@@ -120,6 +127,11 @@ static inline d3d11_decoder_device_t *GetD3D11OpaqueContext(vlc_video_context *v
     return res;
 }
 
+static inline d3d11_video_context_t *GetD3D11ContextPrivate(vlc_video_context *vctx)
+{
+    return (d3d11_video_context_t *) vlc_video_context_GetPrivate( vctx, VLC_VIDEO_CONTEXT_D3D11VA );
+}
+
 void AcquireD3D11PictureSys(picture_sys_d3d11_t *p_sys);
 
 void ReleaseD3D11PictureSys(picture_sys_d3d11_t *p_sys);
-- 
2.17.1



More information about the vlc-devel mailing list