[vlc-commits] direct3d9: add an internal VideoProcessor to do the range conversion

Steve Lhomme git at videolan.org
Wed Mar 4 16:06:48 CET 2020


vlc | branch: master | Steve Lhomme <robux4 at ycbcr.xyz> | Wed Mar  4 13:32:44 2020 +0100| [a9474e0adc4d481f5024edbfd62be926813be4d2] | committer: Steve Lhomme

direct3d9: add an internal VideoProcessor to do the range conversion

On NVIDIA the YUV to RGB StretchRect doesn't do limited->full conversion unlike
other vendors so we need to do it ourself.

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

 modules/video_output/win32/direct3d9.c | 226 ++++++++++++++++++++++++++++++++-
 1 file changed, 224 insertions(+), 2 deletions(-)

diff --git a/modules/video_output/win32/direct3d9.c b/modules/video_output/win32/direct3d9.c
index d340a6f977..71bf83dfe6 100644
--- a/modules/video_output/win32/direct3d9.c
+++ b/modules/video_output/win32/direct3d9.c
@@ -56,6 +56,7 @@
 #include <d3dx9effect.h>
 #endif
 #include "../../video_chroma/d3d9_fmt.h"
+#include <dxvahd.h>
 
 #include "common.h"
 #include "builtin_shaders.h"
@@ -176,6 +177,12 @@ struct vout_display_sys_t
     libvlc_video_update_output_cb            updateOutputCb;
     libvlc_video_swap_cb                     swapCb;
     libvlc_video_makeCurrent_cb              startEndRenderingCb;
+
+    /* range converter */
+    struct {
+        HMODULE                 dll;
+        IDXVAHD_VideoProcessor *proc;
+    } processor;
 };
 
 /* */
@@ -353,6 +360,15 @@ static int Direct3D9ImportPicture(vout_display_t *vd,
         return VLC_EGENERIC;
     }
 
+    if (sys->processor.proc)
+    {
+        DXVAHD_STREAM_DATA inputStream = { 0 };
+        inputStream.Enable = TRUE;
+        inputStream.pInputSurface = source;
+        hr = IDXVAHD_VideoProcessor_VideoProcessBltHD( sys->processor.proc, destination, 0, 1, &inputStream );
+    }
+    else
+    {
     /* Copy picture surface into texture surface
      * color space conversion happen here */
     RECT source_visible_rect = {
@@ -384,6 +400,7 @@ static int Direct3D9ImportPicture(vout_display_t *vd,
     hr = IDirect3DDevice9_StretchRect(sys->d3d9_device->d3ddev.dev, source, &source_visible_rect,
                                       destination, &texture_visible_rect,
                                       D3DTEXF_NONE);
+    }
     IDirect3DSurface9_Release(destination);
     if (FAILED(hr)) {
         msg_Dbg(vd, "Failed StretchRect: source 0x%p 0x%lX",
@@ -405,8 +422,12 @@ static int Direct3D9ImportPicture(vout_display_t *vd,
         .top    = sys->area.place.y,
         .bottom = sys->area.place.y + sys->area.place.height,
     };
-    texture_visible_rect.right  = vd->source.i_visible_width;
-    texture_visible_rect.bottom = vd->source.i_visible_height;
+    RECT texture_visible_rect = {
+        .left   = 0,
+        .right  = vd->source.i_visible_width,
+        .top    = 0,
+        .bottom = vd->source.i_visible_height,
+    };
     Direct3D9SetupVertices(region->vertex, &texture_rect, &texture_visible_rect,
                            &rect_in_display, 255, vd->source.orientation);
     return VLC_SUCCESS;
@@ -1273,6 +1294,11 @@ static void Display(vout_display_t *vd, picture_t *picture)
  */
 static void Direct3D9Destroy(vout_display_sys_t *sys)
 {
+    if (sys->processor.proc)
+    {
+        IDXVAHD_VideoProcessor_Release(sys->processor.proc);
+        FreeLibrary(sys->processor.dll);
+    }
     if (sys->dec_device)
         vlc_decoder_device_Release(sys->dec_device);
     else if (sys->d3d9_device)
@@ -1336,6 +1362,19 @@ static const d3d9_format_t d3d_formats[] = {
     { NULL, 0, 0, 0,0,0}
 };
 
+static const d3d9_format_t *FindBufferFormat(vout_display_t *vd, D3DFORMAT fmt)
+{
+    for (unsigned j = 0; d3d_formats[j].name; j++) {
+        const d3d9_format_t *format = &d3d_formats[j];
+
+        if (format->format != fmt)
+            continue;
+
+        return format;
+    }
+    return NULL;
+}
+
 /**
  * It returns the format (closest to chroma) that can be converted to target */
 static const d3d9_format_t *Direct3DFindFormat(vout_display_t *vd, const video_format_t *fmt)
@@ -1378,6 +1417,181 @@ static const d3d9_format_t *Direct3DFindFormat(vout_display_t *vd, const video_f
     return NULL;
 }
 
+static void SetupProcessorInput(vout_display_t *vd, const video_format_t *fmt, const d3d9_format_t *d3dfmt)
+{
+    vout_display_sys_t *sys = vd->sys;
+    HRESULT hr;
+    DXVAHD_STREAM_STATE_D3DFORMAT_DATA d3dformat = { d3dfmt->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   = vd->source.i_x_offset,
+        .right  = vd->source.i_x_offset + vd->source.i_visible_width,
+        .top    = vd->source.i_y_offset,
+        .bottom = vd->source.i_y_offset + vd->source.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  = vd->source.i_visible_width,
+        .top    = 0,
+        .bottom = vd->source.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(vout_display_t *vd, const d3d9_format_t *d3dfmt,
+                              const libvlc_video_output_cfg_t *render_out)
+{
+    vout_display_sys_t *sys = vd->sys;
+
+    HRESULT hr;
+
+    sys->processor.dll = LoadLibrary(TEXT("DXVA2.DLL"));
+    if (!sys->processor.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(vd, "Can't create HD device (not Windows 7+)");
+        goto error;
+    }
+
+    DXVAHD_CONTENT_DESC desc;
+    desc.InputFrameFormat = DXVAHD_FRAME_FORMAT_PROGRESSIVE;
+    GetFrameRate( &desc.InputFrameRate, &vd->source );
+    desc.InputWidth       = vd->source.i_visible_width;
+    desc.InputHeight      = vd->source.i_visible_height;
+    desc.OutputFrameRate  = desc.InputFrameRate;
+    desc.OutputWidth      = vd->source.i_visible_width;
+    desc.OutputHeight     = vd->source.i_visible_height;
+
+    hr = CreateDevice(sys->d3d9_device->d3ddev.devex, &desc, DXVAHD_DEVICE_USAGE_PLAYBACK_NORMAL, NULL, &hd_device);
+    if (FAILED(hr))
+    {
+        msg_Dbg(vd, "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(vd, "Failed to get the device capabilities (error 0x%lX)", hr);
+        goto error;
+    }
+    if (devcaps.VideoProcessorCount == 0)
+    {
+        msg_Warn(vd, "No good video processor found for range conversion");
+        goto error;
+    }
+
+    formatsList = malloc(devcaps.InputFormatCount * sizeof(*formatsList));
+    if (unlikely(formatsList == NULL))
+        goto error;
+
+    hr = IDXVAHD_Device_GetVideoProcessorInputFormats( hd_device, devcaps.InputFormatCount, formatsList);
+    UINT i;
+    for (i=0; i<devcaps.InputFormatCount; i++)
+    {
+        if (formatsList[i] == d3dfmt->format)
+            break;
+    }
+    if (i == devcaps.InputFormatCount)
+    {
+        msg_Warn(vd, "Input format %s not supported for range conversion", d3dfmt->name);
+        goto error;
+    }
+
+    free(formatsList);
+    formatsList = malloc(devcaps.OutputFormatCount * sizeof(*formatsList));
+    if (unlikely(formatsList == NULL))
+        goto error;
+
+    hr = IDXVAHD_Device_GetVideoProcessorOutputFormats( hd_device, devcaps.OutputFormatCount, formatsList);
+    for (i=0; i<devcaps.OutputFormatCount; i++)
+    {
+        if (formatsList[i] == sys->BufferFormat)
+            break;
+    }
+    if (i == devcaps.OutputFormatCount)
+    {
+        msg_Warn(vd, "Output format %s not supported for range conversion", d3dfmt->name);
+        goto error;
+    }
+
+    capsList = malloc(devcaps.VideoProcessorCount * sizeof(*capsList));
+    if (unlikely(capsList == NULL))
+        goto error;
+    hr = IDXVAHD_Device_GetVideoProcessorCaps( hd_device, devcaps.VideoProcessorCount, capsList);
+    if (FAILED(hr))
+    {
+        msg_Dbg(vd, "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(vd, "Failed to create the processor (error 0x%lX)", hr);
+        goto error;
+    }
+    IDXVAHD_Device_Release( hd_device );
+
+    SetupProcessorInput(vd, &vd->source, d3dfmt);
+
+    DXVAHD_BLT_STATE_OUTPUT_COLOR_SPACE_DATA colorspace;
+    colorspace.Usage = 0; // playback
+    colorspace.RGB_Range = render_out->full_range ? 0 : 1;
+    colorspace.YCbCr_xvYCC = render_out->full_range ? 1 : 0;
+    colorspace.YCbCr_Matrix = render_out->colorspace == libvlc_video_colorspace_BT601 ? 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;
+}
+
 /**
  * It creates a Direct3D9 device and the associated resources.
  */
@@ -1399,6 +1613,14 @@ static int Direct3D9Open(vout_display_t *vd, video_format_t *fmt)
         msg_Err(vd, "surface pixel format is not supported.");
         goto error;
     }
+    const d3d9_format_t *d3dbuffer = FindBufferFormat(vd, sys->BufferFormat);
+    if (!d3dbuffer)
+        msg_Warn(vd, "Unknown back buffer format 0x%X", sys->BufferFormat);
+    else if (vd->source.color_range != COLOR_RANGE_FULL && d3dbuffer->rmask && !d3dfmt->rmask)
+    {
+        // NVIDIA bug, YUV to RGB internal conversion in StretchRect always converts from limited to limited range
+        InitRangeProcessor( vd, d3dfmt, &render_out );
+    }
 
     /* */
     *fmt = vd->source;



More information about the vlc-commits mailing list