[vlc-devel] [PATCH] directx_va: clean the dx_sys calls with macros

Steve Lhomme robux4 at videolabs.io
Fri May 5 10:17:06 CEST 2017


---
 modules/codec/avcodec/d3d11va.c | 51 ++++++++++++++++++++++-------------------
 modules/codec/avcodec/dxva2.c   | 23 ++++++++++---------
 2 files changed, 39 insertions(+), 35 deletions(-)

diff --git a/modules/codec/avcodec/d3d11va.c b/modules/codec/avcodec/d3d11va.c
index ad7cbcb495..13af5286d3 100644
--- a/modules/codec/avcodec/d3d11va.c
+++ b/modules/codec/avcodec/d3d11va.c
@@ -139,6 +139,9 @@ struct vlc_va_sys_t
     DXGI_FORMAT                    processorFormat;
 };
 
+#define VIDEO_DEVICE(x)  ((ID3D11VideoDevice*) (x)->d3ddec)
+#define D3D_DEVICE(x)    ((ID3D11Device*)      (x)->d3ddev)
+
 /* */
 static int D3dCreateDevice(vlc_va_t *);
 static void D3dDestroyDevice(vlc_va_t *);
@@ -270,7 +273,7 @@ static int Extract(vlc_va_t *va, picture_t *output, uint8_t *data)
                     .ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D,
                 };
 
-                HRESULT hr = ID3D11VideoDevice_CreateVideoProcessorOutputView((ID3D11VideoDevice*) sys->dx_sys.d3ddec,
+                HRESULT hr = ID3D11VideoDevice_CreateVideoProcessorOutputView(VIDEO_DEVICE(&sys->dx_sys),
                                                                  p_sys_out->resource[KNOWN_DXGI_INDEX],
                                                                  sys->procEnumerator,
                                                                  &outDesc,
@@ -384,7 +387,7 @@ static int Get(vlc_va_t *va, picture_t *pic, uint8_t **data)
         viewDesc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
         viewDesc.Texture2D.ArraySlice = p_sys->slice_index;
 
-        hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( (ID3D11VideoDevice*) dx_sys->d3ddec,
+        hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( VIDEO_DEVICE(dx_sys),
                                                              p_sys->resource[KNOWN_DXGI_INDEX],
                                                              &viewDesc,
                                                              &p_sys->decoder );
@@ -484,10 +487,10 @@ static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
         if (FAILED(hr)) {
            msg_Err(va, "Could not Query ID3D11VideoDevice Interface from the picture. (hr=0x%lX)", hr);
         } else {
-            ID3D11DeviceContext_GetDevice( p_sys->context, (ID3D11Device**) &dx_sys->d3ddev );
+            ID3D11DeviceContext_GetDevice( p_sys->context, (ID3D11Device**) &dx_sys->d3ddev);
             HANDLE context_lock = INVALID_HANDLE_VALUE;
             UINT dataSize = sizeof(context_lock);
-            hr = ID3D11Device_GetPrivateData((ID3D11Device*)dx_sys->d3ddev, &GUID_CONTEXT_MUTEX, &dataSize, &context_lock);
+            hr = ID3D11Device_GetPrivateData(D3D_DEVICE(dx_sys), &GUID_CONTEXT_MUTEX, &dataSize, &context_lock);
             if (FAILED(hr))
                 msg_Warn(va, "No mutex found to lock the decoder");
             sys->context_mutex = context_lock;
@@ -653,7 +656,7 @@ static char *DxDescribe(directx_sys_t *dx_sys)
         { 0, "" }
     };
 
-    IDXGIAdapter *p_adapter = D3D11DeviceAdapter((ID3D11Device*) dx_sys->d3ddev);
+    IDXGIAdapter *p_adapter = D3D11DeviceAdapter(D3D_DEVICE(dx_sys));
     if (!p_adapter) {
        return NULL;
     }
@@ -715,7 +718,7 @@ static int D3dCreateDeviceManager(vlc_va_t *va)
     sys->devmng = devmng;
     msg_Info(va, "obtained IDirect3DDeviceManager9");
 
-    HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, (ID3D11Device*) dx_sys->d3ddev, token);
+    HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, D3D_DEVICE(dx_sys), token);
     if (FAILED(hr)) {
         msg_Err(va, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
         return VLC_EGENERIC;
@@ -743,7 +746,7 @@ static int DxCreateVideoService(vlc_va_t *va)
     directx_sys_t *dx_sys = &va->sys->dx_sys;
 
     ID3D11VideoDevice *d3dviddev = NULL;
-    HRESULT hr = ID3D11Device_QueryInterface( (ID3D11Device*) dx_sys->d3ddev, &IID_ID3D11VideoDevice, (void **)&d3dviddev);
+    HRESULT hr = ID3D11Device_QueryInterface( D3D_DEVICE(dx_sys), &IID_ID3D11VideoDevice, (void **)&d3dviddev);
     if (FAILED(hr)) {
        msg_Err(va, "Could not Query ID3D11VideoDevice Interface. (hr=0x%lX)", hr);
        return VLC_EGENERIC;
@@ -771,7 +774,7 @@ static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
     directx_sys_t *dx_sys = &va->sys->dx_sys;
     HRESULT hr;
 
-    UINT input_count = ID3D11VideoDevice_GetVideoDecoderProfileCount((ID3D11VideoDevice*) dx_sys->d3ddec);
+    UINT input_count = ID3D11VideoDevice_GetVideoDecoderProfileCount(VIDEO_DEVICE(dx_sys));
 
     p_list->count = input_count;
     p_list->list = calloc(input_count, sizeof(*p_list->list));
@@ -781,7 +784,7 @@ static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
     p_list->pf_release = ReleaseInputList;
 
     for (unsigned i = 0; i < input_count; i++) {
-        hr = ID3D11VideoDevice_GetVideoDecoderProfile((ID3D11VideoDevice*) dx_sys->d3ddec, i, &p_list->list[i]);
+        hr = ID3D11VideoDevice_GetVideoDecoderProfile(VIDEO_DEVICE(dx_sys), i, &p_list->list[i]);
         if (FAILED(hr))
         {
             msg_Err(va, "GetVideoDecoderProfile %d failed. (hr=0x%lX)", i, hr);
@@ -813,7 +816,7 @@ static bool SetupProcessor(vlc_va_t *va, const video_format_t *fmt)
         .OutputHeight = fmt->i_height,
         .Usage = D3D11_VIDEO_USAGE_PLAYBACK_NORMAL,
     };
-    hr = ID3D11VideoDevice_CreateVideoProcessorEnumerator((ID3D11VideoDevice*) dx_sys->d3ddec, &processorDesc, &processorEnumerator);
+    hr = ID3D11VideoDevice_CreateVideoProcessorEnumerator(VIDEO_DEVICE(dx_sys), &processorDesc, &processorEnumerator);
     if ( processorEnumerator == NULL )
     {
         msg_Dbg(va, "Can't get a video processor for the video.");
@@ -846,7 +849,7 @@ static bool SetupProcessor(vlc_va_t *va, const video_format_t *fmt)
         // check if we can create render texture of that format
         // check the decoder can output to that format
         const d3d_format_t *found;
-        found = FindD3D11Format( (ID3D11Device*) dx_sys->d3ddev, 0, 0, true,
+        found = FindD3D11Format( D3D_DEVICE(dx_sys), 0, 0, true,
                                  D3D11_FORMAT_SUPPORT_SHADER_LOAD | D3D11_FORMAT_SUPPORT_VIDEO_PROCESSOR_OUTPUT);
         if (found)
             processorOutput = found->formatTexture;
@@ -860,7 +863,7 @@ static bool SetupProcessor(vlc_va_t *va, const video_format_t *fmt)
 
         for (UINT type = 0; type < processorCaps.RateConversionCapsCount; ++type)
         {
-            hr = ID3D11VideoDevice_CreateVideoProcessor((ID3D11VideoDevice*) dx_sys->d3ddec,
+            hr = ID3D11VideoDevice_CreateVideoProcessor(VIDEO_DEVICE(dx_sys),
                                                         processorEnumerator, type, &sys->videoProcessor);
             if (SUCCEEDED(hr))
                 break;
@@ -887,7 +890,7 @@ static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *
 #ifndef NDEBUG
     BOOL bSupported = false;
     for (int format = 0; format < 188; format++) {
-        hr = ID3D11VideoDevice_CheckVideoDecoderFormat((ID3D11VideoDevice*) dx_sys->d3ddec, input, format, &bSupported);
+        hr = ID3D11VideoDevice_CheckVideoDecoderFormat(VIDEO_DEVICE(dx_sys), input, format, &bSupported);
         if (SUCCEEDED(hr) && bSupported)
             msg_Dbg(va, "format %s is supported for output", DxgiFormatToStr(format));
     }
@@ -907,7 +910,7 @@ static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *
     for (idx = 0; processorInput[idx] != DXGI_FORMAT_UNKNOWN; ++idx)
     {
         BOOL is_supported = false;
-        hr = ID3D11VideoDevice_CheckVideoDecoderFormat((ID3D11VideoDevice*) dx_sys->d3ddec, input, processorInput[idx], &is_supported);
+        hr = ID3D11VideoDevice_CheckVideoDecoderFormat(VIDEO_DEVICE(dx_sys), input, processorInput[idx], &is_supported);
         if (SUCCEEDED(hr) && is_supported)
             msg_Dbg(va, "%s output is supported for decoder %s.", DxgiFormatToStr(processorInput[idx]), psz_decoder_name);
         else
@@ -918,7 +921,7 @@ static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *
 
        // check if we can create render texture of that format
        // check the decoder can output to that format
-       if ( !DeviceSupportsFormat((ID3D11Device*) dx_sys->d3ddev, processorInput[idx],
+       if ( !DeviceSupportsFormat(D3D_DEVICE(dx_sys), processorInput[idx],
                                   D3D11_FORMAT_SUPPORT_SHADER_LOAD) )
        {
            msg_Dbg(va, "Format %s needs a processor", DxgiFormatToStr(processorInput[idx]));
@@ -939,7 +942,7 @@ static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *
         decoderDesc.OutputFormat = processorInput[idx];
 
         UINT cfg_count = 0;
-        hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( (ID3D11VideoDevice*) dx_sys->d3ddec, &decoderDesc, &cfg_count );
+        hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( VIDEO_DEVICE(dx_sys), &decoderDesc, &cfg_count );
         if (FAILED(hr))
         {
             msg_Err( va, "Failed to get configuration for decoder %s. (hr=0x%lX)", psz_decoder_name, hr );
@@ -977,7 +980,7 @@ static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id, const video_forma
      * crashes totally the device */
     if (codec_id == AV_CODEC_ID_H264 &&
         (dx_sys->surface_width > 2304 || dx_sys->surface_height > 2304) &&
-        isXboxHardware((ID3D11Device*) dx_sys->d3ddev))
+        isXboxHardware(D3D_DEVICE(dx_sys))
     {
         msg_Warn(va, "%dx%d resolution not supported by your hardware", dx_sys->surface_width, dx_sys->surface_height);
         dx_sys->surface_count = 0;
@@ -1030,7 +1033,7 @@ static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id, const video_forma
 #endif
 
             viewDesc.Texture2D.ArraySlice = pic->p_sys->slice_index;
-            hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( (ID3D11VideoDevice*) dx_sys->d3ddec,
+            hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( VIDEO_DEVICE(dx_sys),
                                                                  pic->p_sys->resource[KNOWN_DXGI_INDEX],
                                                                  &viewDesc,
                                                                  &pic->p_sys->decoder );
@@ -1080,7 +1083,7 @@ static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id, const video_forma
         texDesc.CPUAccessFlags = 0;
 
         ID3D11Texture2D *p_texture;
-        hr = ID3D11Device_CreateTexture2D( (ID3D11Device*) dx_sys->d3ddev, &texDesc, NULL, &p_texture );
+        hr = ID3D11Device_CreateTexture2D( D3D_DEVICE(dx_sys), &texDesc, NULL, &p_texture );
         if (FAILED(hr)) {
             msg_Err(va, "CreateTexture2D %d failed. (hr=0x%0lx)", dx_sys->surface_count, hr);
             dx_sys->surface_count = 0;
@@ -1092,7 +1095,7 @@ static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id, const video_forma
             sys->extern_pics[dx_sys->surface_count] = NULL;
             viewDesc.Texture2D.ArraySlice = dx_sys->surface_count;
 
-            hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( (ID3D11VideoDevice*) dx_sys->d3ddec,
+            hr = ID3D11VideoDevice_CreateVideoDecoderOutputView( VIDEO_DEVICE(dx_sys),
                                                                  (ID3D11Resource*) p_texture,
                                                                  &viewDesc,
                                                                  (ID3D11VideoDecoderOutputView**) &dx_sys->hw_surface[dx_sys->surface_count] );
@@ -1114,7 +1117,7 @@ static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id, const video_forma
     decoderDesc.OutputFormat = sys->render;
 
     UINT cfg_count;
-    hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( (ID3D11VideoDevice*) dx_sys->d3ddec, &decoderDesc, &cfg_count );
+    hr = ID3D11VideoDevice_GetVideoDecoderConfigCount( VIDEO_DEVICE(dx_sys), &decoderDesc, &cfg_count );
     if (FAILED(hr)) {
         msg_Err(va, "GetVideoDecoderConfigCount failed. (hr=0x%lX)", hr);
         return VLC_EGENERIC;
@@ -1123,7 +1126,7 @@ static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id, const video_forma
     /* List all configurations available for the decoder */
     D3D11_VIDEO_DECODER_CONFIG cfg_list[cfg_count];
     for (unsigned i = 0; i < cfg_count; i++) {
-        hr = ID3D11VideoDevice_GetVideoDecoderConfig( (ID3D11VideoDevice*) dx_sys->d3ddec, &decoderDesc, i, &cfg_list[i] );
+        hr = ID3D11VideoDevice_GetVideoDecoderConfig( VIDEO_DEVICE(dx_sys), &decoderDesc, i, &cfg_list[i] );
         if (FAILED(hr)) {
             msg_Err(va, "GetVideoDecoderConfig failed. (hr=0x%lX)", hr);
             return VLC_EGENERIC;
@@ -1164,7 +1167,7 @@ static int DxCreateDecoderSurfaces(vlc_va_t *va, int codec_id, const video_forma
 
     /* Create the decoder */
     ID3D11VideoDecoder *decoder;
-    hr = ID3D11VideoDevice_CreateVideoDecoder( (ID3D11VideoDevice*) dx_sys->d3ddec, &decoderDesc, &sys->cfg, &decoder );
+    hr = ID3D11VideoDevice_CreateVideoDecoder( VIDEO_DEVICE(dx_sys), &decoderDesc, &sys->cfg, &decoder );
     if (FAILED(hr)) {
         msg_Err(va, "ID3D11VideoDevice_CreateVideoDecoder failed. (hr=0x%lX)", hr);
         dx_sys->decoder = NULL;
@@ -1223,7 +1226,7 @@ static picture_t *DxAllocPicture(vlc_va_t *va, const video_format_t *fmt, unsign
             .Texture2D.ArraySlice = index,
         };
 
-        HRESULT hr = ID3D11VideoDevice_CreateVideoProcessorInputView((ID3D11VideoDevice*) sys->dx_sys.d3ddec,
+        HRESULT hr = ID3D11VideoDevice_CreateVideoProcessorInputView(VIDEO_DEVICE(&sys->dx_sys),
                                                         pic_sys->resource[KNOWN_DXGI_INDEX],
                                                         sys->procEnumerator,
                                                         &inDesc,
diff --git a/modules/codec/avcodec/dxva2.c b/modules/codec/avcodec/dxva2.c
index f19a7c392d..58fe895524 100644
--- a/modules/codec/avcodec/dxva2.c
+++ b/modules/codec/avcodec/dxva2.c
@@ -79,7 +79,6 @@ DEFINE_GUID(DXVA2_NoEncrypt,                        0x1b81bed0, 0xa0c7, 0x11d3,
 
 DEFINE_GUID(DXVA_Intel_H264_NoFGT_ClearVideo,       0x604F8E68, 0x4951, 0x4c54, 0x88, 0xFE, 0xAB, 0xD2, 0x5C, 0x15, 0xB3, 0xD6);
 
-
 /* */
 typedef struct {
     const char   *name;
@@ -134,6 +133,9 @@ struct vlc_va_sys_t
     struct dxva_context hw;
 };
 
+#define VIDEO_DEVICE(x)  ((IDirectXVideoDecoderService*) (x)->d3ddec)
+#define D3D_DEVICE(x)    ((IDirect3DDevice9*)            (x)->d3ddev)
+
 struct picture_sys_t
 {
     LPDIRECT3DSURFACE9 surface;
@@ -262,7 +264,7 @@ static int Extract(vlc_va_t *va, picture_t *picture, uint8_t *data)
         visibleSource.top = 0;
         visibleSource.right = picture->format.i_visible_width;
         visibleSource.bottom = picture->format.i_visible_height;
-        hr = IDirect3DDevice9_StretchRect( (IDirect3DDevice9*) dx_sys->d3ddev, d3d, &visibleSource, output, &visibleSource, D3DTEXF_NONE);
+        hr = IDirect3DDevice9_StretchRect( D3D_DEVICE(dx_sys), d3d, &visibleSource, output, &visibleSource, D3DTEXF_NONE);
         if (FAILED(hr)) {
             msg_Err(va, "Failed to copy the hw surface to the decoder surface (hr=0x%0lx)", hr );
             return VLC_EGENERIC;
@@ -382,7 +384,7 @@ static int Open(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
         D3DSURFACE_DESC src;
         if (SUCCEEDED(IDirect3DSurface9_GetDesc(p_sys->surface, &src)))
             sys->render = src.Format;
-        IDirect3DSurface9_GetDevice(p_sys->surface, (IDirect3DDevice9**) &dx_sys->d3ddev );
+        IDirect3DSurface9_GetDevice(p_sys->surface, (IDirect3DDevice9**) &dx_sys->d3ddev);
     }
 
     sys->i_chroma = d3d9va_fourcc(ctx->sw_pix_fmt);
@@ -559,7 +561,7 @@ static int D3dCreateDeviceManager(vlc_va_t *va)
     sys->devmng = devmng;
     msg_Info(va, "obtained IDirect3DDeviceManager9");
 
-    HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, (IDirect3DDevice9*) dx_sys->d3ddev, token);
+    HRESULT hr = IDirect3DDeviceManager9_ResetDevice(devmng, D3D_DEVICE(dx_sys), token);
     if (FAILED(hr)) {
         msg_Err(va, "IDirect3DDeviceManager9_ResetDevice failed: %08x", (unsigned)hr);
         return VLC_EGENERIC;
@@ -633,10 +635,9 @@ static void ReleaseInputList(input_list_t *p_list)
 
 static int DxGetInputList(vlc_va_t *va, input_list_t *p_list)
 {
-    directx_sys_t *dx_sys = &va->sys->dx_sys;
     UINT input_count = 0;
     GUID *input_list = NULL;
-    if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids((IDirectXVideoDecoderService*) dx_sys->d3ddec,
+    if (FAILED(IDirectXVideoDecoderService_GetDecoderDeviceGuids(VIDEO_DEVICE(&va->sys->dx_sys),
                                                                  &input_count,
                                                                  &input_list))) {
         msg_Err(va, "IDirectXVideoDecoderService_GetDecoderDeviceGuids failed");
@@ -655,7 +656,7 @@ static int DxSetupOutput(vlc_va_t *va, const GUID *input, const video_format_t *
     int err = VLC_EGENERIC;
     UINT      output_count = 0;
     D3DFORMAT *output_list = NULL;
-    if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets((IDirectXVideoDecoderService*) va->sys->dx_sys.d3ddec,
+    if (FAILED(IDirectXVideoDecoderService_GetDecoderRenderTargets(VIDEO_DEVICE(&va->sys->dx_sys),
                                                                    input,
                                                                    &output_count,
                                                                    &output_list))) {
@@ -709,7 +710,7 @@ static int DxCreateVideoDecoder(vlc_va_t *va, int codec_id, const video_format_t
     directx_sys_t *sys = &va->sys->dx_sys;
     HRESULT hr;
 
-    hr = IDirectXVideoDecoderService_CreateSurface((IDirectXVideoDecoderService*) sys->d3ddec,
+    hr = IDirectXVideoDecoderService_CreateSurface(VIDEO_DEVICE(sys),
                                                          sys->surface_width,
                                                          sys->surface_height,
                                                          sys->surface_count - 1,
@@ -728,7 +729,7 @@ static int DxCreateVideoDecoder(vlc_va_t *va, int codec_id, const video_format_t
             sys->surface_count, sys->surface_width, sys->surface_height);
 
     IDirect3DSurface9 *tstCrash;
-    hr = IDirectXVideoDecoderService_CreateSurface((IDirectXVideoDecoderService*) sys->d3ddec,
+    hr = IDirectXVideoDecoderService_CreateSurface(VIDEO_DEVICE(sys),
                                                          sys->surface_width,
                                                          sys->surface_height,
                                                          0,
@@ -777,7 +778,7 @@ static int DxCreateVideoDecoder(vlc_va_t *va, int codec_id, const video_format_t
     /* List all configurations available for the decoder */
     UINT                      cfg_count = 0;
     DXVA2_ConfigPictureDecode *cfg_list = NULL;
-    if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations((IDirectXVideoDecoderService*) sys->d3ddec,
+    if (FAILED(IDirectXVideoDecoderService_GetDecoderConfigurations(VIDEO_DEVICE(sys),
                                                                     &sys->input,
                                                                     &dsc,
                                                                     NULL,
@@ -824,7 +825,7 @@ static int DxCreateVideoDecoder(vlc_va_t *va, int codec_id, const video_format_t
 
     /* Create the decoder */
     IDirectXVideoDecoder *decoder;
-    if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder((IDirectXVideoDecoderService*) sys->d3ddec,
+    if (FAILED(IDirectXVideoDecoderService_CreateVideoDecoder(VIDEO_DEVICE(sys),
                                                               &sys->input,
                                                               &dsc,
                                                               &p_sys->cfg,
-- 
2.12.1



More information about the vlc-devel mailing list