[vlc-commits] opengl: dxva2: use DXVA-HD instead of StretchRect for NVIDIA GPUs

Steve Lhomme git at videolan.org
Tue Mar 17 07:49:05 CET 2020


vlc | branch: master | Steve Lhomme <robux4 at ycbcr.xyz> | Mon Mar 16 13:24:43 2020 +0100| [e595beb4e97391fd0b86caa1cc02955238908cb3] | committer: Steve Lhomme

opengl: dxva2: use DXVA-HD instead of StretchRect for NVIDIA GPUs

The StretchRect we're using has the same issue as with the D3D9 vout.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=e595beb4e97391fd0b86caa1cc02955238908cb3
---

 modules/video_output/opengl/interop_dxva2.c | 231 ++++++++++++++++++++++++++++
 1 file changed, 231 insertions(+)

diff --git a/modules/video_output/opengl/interop_dxva2.c b/modules/video_output/opengl/interop_dxva2.c
index b42ab8c6eb..f791a1e47e 100644
--- a/modules/video_output/opengl/interop_dxva2.c
+++ b/modules/video_output/opengl/interop_dxva2.c
@@ -85,6 +85,12 @@ struct glpriv
     IDirect3DSurface9 *dx_render;
 
     D3DFORMAT OutputFormat;
+
+    /* range converter */
+    struct {
+        HMODULE                 dll;
+        IDXVAHD_VideoProcessor *proc;
+    } processor;
 };
 
 static int
@@ -108,6 +114,25 @@ GLConvUpdate(const struct vlc_gl_interop *interop, GLuint *textures,
 
     d3d9_decoder_device_t *d3d9_decoder = GetD3D9OpaqueContext(interop->vctx);
 
+    if (priv->processor.proc)
+    {
+        DXVAHD_STREAM_DATA inputStream = { 0 };
+        inputStream.Enable = TRUE;
+        inputStream.pInputSurface = picsys->surface;
+        hr = IDXVAHD_VideoProcessor_VideoProcessBltHD( priv->processor.proc, priv->dx_render, 0, 1, &inputStream );
+        if (FAILED(hr)) {
+            D3DSURFACE_DESC srcDesc, dstDesc;
+            IDirect3DSurface9_GetDesc(picsys->surface, &srcDesc);
+            IDirect3DSurface9_GetDesc(priv->dx_render, &dstDesc);
+
+            msg_Dbg(interop->gl, "Failed VideoProcessBltHD src:%4.4s (%d) dst:%4.4s (%d) (hr=0x%lX)",
+                    (const char*)&srcDesc.Format, srcDesc.Format,
+                    (const char*)&dstDesc.Format, dstDesc.Format, hr);
+            return VLC_EGENERIC;
+        }
+    }
+    else
+    {
     const RECT rect = {
         .left = 0,
         .top = 0,
@@ -121,6 +146,7 @@ GLConvUpdate(const struct vlc_gl_interop *interop, GLuint *textures,
         msg_Warn(interop->gl, "IDirect3DDevice9Ex_StretchRect failed. (0x%lX)", hr);
         return VLC_EGENERIC;
     }
+    }
 
     if (!priv->vt.DXLockObjectsNV(priv->gl_handle_d3d, 1, &priv->gl_render))
     {
@@ -176,6 +202,11 @@ GLConvClose(vlc_object_t *obj)
 
         priv->vt.DXCloseDeviceNV(priv->gl_handle_d3d);
     }
+    if (priv->processor.proc)
+    {
+        IDXVAHD_VideoProcessor_Release(priv->processor.proc);
+        FreeLibrary(priv->processor.dll);
+    }
 
     if (priv->dx_render)
         IDirect3DSurface9_Release(priv->dx_render);
@@ -183,6 +214,192 @@ GLConvClose(vlc_object_t *obj)
     free(priv);
 }
 
+static void SetupProcessorInput(struct vlc_gl_interop *interop, const video_format_t *fmt, D3DFORMAT src_format)
+{
+    struct glpriv *sys = interop->priv;
+    HRESULT hr;
+    DXVAHD_STREAM_STATE_D3DFORMAT_DATA d3dformat = { src_format };
+    hr = IDXVAHD_VideoProcessor_SetVideoProcessStreamState( sys->processor.proc, 0, DXVAHD_STREAM_STATE_D3DFORMAT, sizeof(d3dformat), &d3dformat );
+
+    DXVAHD_STREAM_STATE_FRAME_FORMAT_DATA frame_format = { DXVAHD_FRAME_FORMAT_PROGRESSIVE };
+    hr = IDXVAHD_VideoProcessor_SetVideoProcessStreamState( sys->processor.proc, 0, DXVAHD_STREAM_STATE_FRAME_FORMAT, sizeof(frame_format), &frame_format );
+
+    DXVAHD_STREAM_STATE_INPUT_COLOR_SPACE_DATA colorspace = { 0 };
+    colorspace.RGB_Range = fmt->color_range == COLOR_RANGE_FULL ? 0 : 1;
+    colorspace.YCbCr_xvYCC = fmt->color_range == COLOR_RANGE_FULL ? 1 : 0;
+    colorspace.YCbCr_Matrix = fmt->space == COLOR_SPACE_BT601 ? 0 : 1;
+    hr = IDXVAHD_VideoProcessor_SetVideoProcessStreamState( sys->processor.proc, 0, DXVAHD_STREAM_STATE_INPUT_COLOR_SPACE, sizeof(colorspace), &colorspace );
+
+    DXVAHD_STREAM_STATE_SOURCE_RECT_DATA srcRect;
+    srcRect.Enable = TRUE;
+    srcRect.SourceRect = (RECT) {
+        .left   = interop->fmt.i_x_offset,
+        .right  = interop->fmt.i_x_offset + interop->fmt.i_visible_width,
+        .top    = interop->fmt.i_y_offset,
+        .bottom = interop->fmt.i_y_offset + interop->fmt.i_visible_height,
+    };;
+    hr = IDXVAHD_VideoProcessor_SetVideoProcessStreamState( sys->processor.proc, 0, DXVAHD_STREAM_STATE_SOURCE_RECT, sizeof(srcRect), &srcRect );
+
+    DXVAHD_BLT_STATE_TARGET_RECT_DATA dstRect;
+    dstRect.Enable = TRUE;
+    dstRect.TargetRect = (RECT) {
+        .left   = 0,
+        .right  = interop->fmt.i_visible_width,
+        .top    = 0,
+        .bottom = interop->fmt.i_visible_height,
+    };
+    hr = IDXVAHD_VideoProcessor_SetVideoProcessBltState( sys->processor.proc, DXVAHD_BLT_STATE_TARGET_RECT, sizeof(dstRect), &dstRect);
+}
+
+static void GetFrameRate(DXVAHD_RATIONAL *r, const video_format_t *fmt)
+{
+    if (fmt->i_frame_rate && fmt->i_frame_rate_base)
+    {
+        r->Numerator   = fmt->i_frame_rate;
+        r->Denominator = fmt->i_frame_rate_base;
+    }
+    else
+    {
+        r->Numerator   = 0;
+        r->Denominator = 0;
+    }
+}
+
+static int InitRangeProcessor(struct vlc_gl_interop *interop, IDirect3DDevice9Ex *devex, D3DFORMAT src_format)
+{
+    struct glpriv *sys = interop->priv;
+
+    HRESULT hr;
+
+    sys->processor.dll = LoadLibrary(TEXT("DXVA2.DLL"));
+    if (unlikely(!sys->processor.dll))
+    {
+        msg_Err(interop, "Failed to load DXVA2.DLL");
+        return VLC_EGENERIC;
+    }
+
+    D3DFORMAT *formatsList = NULL;
+    DXVAHD_VPCAPS *capsList = NULL;
+    IDXVAHD_Device *hd_device = NULL;
+
+    HRESULT (WINAPI *CreateDevice)(IDirect3DDevice9Ex *,const DXVAHD_CONTENT_DESC *,DXVAHD_DEVICE_USAGE,PDXVAHDSW_Plugin,IDXVAHD_Device **);
+    CreateDevice = (void *)GetProcAddress(sys->processor.dll, "DXVAHD_CreateDevice");
+    if (CreateDevice == NULL)
+    {
+        msg_Err(interop, "Can't create HD device (not Windows 7+)");
+        goto error;
+    }
+
+    DXVAHD_CONTENT_DESC desc;
+    desc.InputFrameFormat = DXVAHD_FRAME_FORMAT_PROGRESSIVE;
+    GetFrameRate( &desc.InputFrameRate, &interop->fmt );
+    desc.InputWidth       = interop->fmt.i_visible_width;
+    desc.InputHeight      = interop->fmt.i_visible_height;
+    desc.OutputFrameRate  = desc.InputFrameRate;
+    desc.OutputWidth      = interop->fmt.i_visible_width;
+    desc.OutputHeight     = interop->fmt.i_visible_height;
+
+    hr = CreateDevice(devex, &desc, DXVAHD_DEVICE_USAGE_PLAYBACK_NORMAL, NULL, &hd_device);
+    if (FAILED(hr))
+    {
+        msg_Dbg(interop, "Failed to create the device (error 0x%lX)", hr);
+        goto error;
+    }
+
+    DXVAHD_VPDEVCAPS devcaps = { 0 };
+    hr = IDXVAHD_Device_GetVideoProcessorDeviceCaps( hd_device, &devcaps );
+    if (unlikely(FAILED(hr)))
+    {
+        msg_Err(interop, "Failed to get the device capabilities (error 0x%lX)", hr);
+        goto error;
+    }
+    if (devcaps.VideoProcessorCount == 0)
+    {
+        msg_Warn(interop, "No good video processor found for range conversion");
+        goto error;
+    }
+
+    formatsList = malloc(devcaps.InputFormatCount * sizeof(*formatsList));
+    if (unlikely(formatsList == NULL))
+    {
+        msg_Dbg(interop, "Failed to allocate %u input formats", devcaps.InputFormatCount);
+        goto error;
+    }
+
+    hr = IDXVAHD_Device_GetVideoProcessorInputFormats( hd_device, devcaps.InputFormatCount, formatsList);
+    UINT i;
+    for (i=0; i<devcaps.InputFormatCount; i++)
+    {
+        if (formatsList[i] == src_format)
+            break;
+    }
+    if (i == devcaps.InputFormatCount)
+    {
+        msg_Warn(interop, "Input format %4.4s not supported for range conversion", (const char*)&src_format);
+        goto error;
+    }
+
+    free(formatsList);
+    formatsList = malloc(devcaps.OutputFormatCount * sizeof(*formatsList));
+    if (unlikely(formatsList == NULL))
+    {
+        msg_Dbg(interop, "Failed to allocate %u output formats", devcaps.OutputFormatCount);
+        goto error;
+    }
+
+    hr = IDXVAHD_Device_GetVideoProcessorOutputFormats( hd_device, devcaps.OutputFormatCount, formatsList);
+    for (i=0; i<devcaps.OutputFormatCount; i++)
+    {
+        if (formatsList[i] == sys->OutputFormat)
+            break;
+    }
+    if (i == devcaps.OutputFormatCount)
+    {
+        msg_Warn(interop, "Output format %d not supported for range conversion", sys->OutputFormat);
+        goto error;
+    }
+
+    capsList = malloc(devcaps.VideoProcessorCount * sizeof(*capsList));
+    if (unlikely(capsList == NULL))
+    {
+        msg_Dbg(interop, "Failed to allocate %u video processors", devcaps.VideoProcessorCount);
+        goto error;
+    }
+    hr = IDXVAHD_Device_GetVideoProcessorCaps( hd_device, devcaps.VideoProcessorCount, capsList);
+    if (FAILED(hr))
+    {
+        msg_Dbg(interop, "Failed to get the processor caps (error 0x%lX)", hr);
+        goto error;
+    }
+
+    hr = IDXVAHD_Device_CreateVideoProcessor( hd_device, &capsList->VPGuid, &sys->processor.proc );
+    if (FAILED(hr))
+    {
+        msg_Dbg(interop, "Failed to create the processor (error 0x%lX)", hr);
+        goto error;
+    }
+    IDXVAHD_Device_Release( hd_device );
+
+    SetupProcessorInput(interop, &interop->fmt, src_format);
+
+    DXVAHD_BLT_STATE_OUTPUT_COLOR_SPACE_DATA colorspace;
+    colorspace.Usage = 0; // playback
+    colorspace.RGB_Range = true ? 0 : 1;
+    colorspace.YCbCr_xvYCC = true ? 1 : 0;
+    colorspace.YCbCr_Matrix = false ? 0 : 1;
+    hr = IDXVAHD_VideoProcessor_SetVideoProcessBltState( sys->processor.proc, DXVAHD_BLT_STATE_OUTPUT_COLOR_SPACE, sizeof(colorspace), &colorspace);
+
+    return VLC_SUCCESS;
+
+error:
+    free(capsList);
+    free(formatsList);
+    if (hd_device)
+        IDXVAHD_Device_Release(hd_device);
+    FreeLibrary(sys->processor.dll);
+    return VLC_EGENERIC;
+}
+
 static int
 GLConvOpen(vlc_object_t *obj)
 {
@@ -236,6 +453,18 @@ GLConvOpen(vlc_object_t *obj)
     priv->vt = vt;
 
     HRESULT hr;
+    bool force_dxva_hd = false;
+    if ( interop->fmt.color_range != COLOR_RANGE_FULL &&
+         d3d9_decoder->d3ddev.identifier.VendorId == GPU_MANUFACTURER_NVIDIA )
+    {
+        // NVIDIA bug, YUV to RGB internal conversion in StretchRect always converts from limited to limited range
+        msg_Dbg(interop->gl, "init DXVA-HD processor from %4.4s to RGB", (const char*)&vctx_sys->format);
+        int err = InitRangeProcessor(interop, d3d9_decoder->d3ddev.devex, vctx_sys->format);
+        if (err == VLC_SUCCESS)
+            force_dxva_hd = true;
+    }
+    if (!force_dxva_hd)
+    {
     // test whether device can perform color-conversion from that format to target format
     hr = IDirect3D9_CheckDeviceFormatConversion(d3d9_decoder->hd3d.obj,
                                                 d3d9_decoder->d3ddev.adapterId,
@@ -246,6 +475,8 @@ GLConvOpen(vlc_object_t *obj)
         msg_Dbg(interop->gl, "Unsupported conversion from %4.4s to RGB", (const char*)&vctx_sys->format );
         goto error;
     }
+    msg_Dbg(interop->gl, "using StrecthRect from %4.4s to RGB", (const char*)&vctx_sys->format );
+    }
 
     HANDLE shared_handle = NULL;
     hr = IDirect3DDevice9Ex_CreateRenderTarget(d3d9_decoder->d3ddev.devex,



More information about the vlc-commits mailing list