Skip to content

Commit

Permalink
hwcontext_vaapi: Add support for vaGetDisplayWin32 initialization
Browse files Browse the repository at this point in the history
These changes add support for VAAPI on Windows in hwcontext_vaapi
using vaGetDisplayWin32, also allowing for DirectX adapter index
selection via the "-device" command line argument.

Libva 2.17+ adds a new libva-win32 node and Mesa 22.3 adds
a VAAPI driver based on Direct3D 12 for Windows, both available at:
https://www.nuget.org/packages/Microsoft.Direct3D.VideoAccelerationCompatibilityPack

Initial review at #619

Additional changes were made to the changelog and minor version bump
as well as to QSV to not break with this new change:

- qsv_internal.h: Remove unnecessary include va_drm.h
- qsv_internal.h: Enable AVCODEC_QSV_LINUX_SESSION_HANDLE on Linux/VA only
- hwcontext_qsv.c: Do not allow child_device_type VAAPI for Windows
  until support is added, keep D3D11/DXVA2 as more prioritary defaults.

Signed-off-by: Sil Vilerino <sivileri@microsoft.com>
Reviewed-by: Dmitry Rogozhkin <dmitry.v.rogozhkin@intel.com>
Reviewed-by: Wu, Tong1 <tong1.wu@intel.com>
  • Loading branch information
Sil Vilerino authored and xhaihao committed Mar 30, 2023
1 parent 3dfce7f commit ff146c8
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Changelog
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ version 6.0:
- RKA decoder and demuxer
- filtergraph syntax in ffmpeg CLI now supports passing file contents
as option values, by prefixing option name with '/'
- hstack_qsv, vstack_qsv and xstack_qsv filters
- Extend VAAPI support for libva-win32 on Windows


version 5.1:
Expand Down
27 changes: 25 additions & 2 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -2316,6 +2316,7 @@ SYSTEM_LIBRARIES="
bcrypt
vaapi_drm
vaapi_x11
vaapi_win32
vdpau_x11
"

Expand Down Expand Up @@ -3830,7 +3831,7 @@ swscale_suggest="libm stdatomic"

avcodec_extralibs="pthreads_extralibs iconv_extralibs dxva2_extralibs lcms2_extralibs"
avfilter_extralibs="pthreads_extralibs"
avutil_extralibs="d3d11va_extralibs nanosleep_extralibs pthreads_extralibs vaapi_drm_extralibs vaapi_x11_extralibs vdpau_x11_extralibs"
avutil_extralibs="d3d11va_extralibs nanosleep_extralibs pthreads_extralibs vaapi_drm_extralibs vaapi_x11_extralibs vaapi_win32_extralibs vdpau_x11_extralibs"

# programs
ffmpeg_deps="avcodec avfilter avformat threads"
Expand Down Expand Up @@ -6951,6 +6952,21 @@ test_cpp <<EOF && enable uwp && d3d11va_extralibs="-ldxgi -ld3d11"
#endif
EOF

# vaapi_win32 requires linking directly to dxgi if not building for
# the desktop api partition
test_cpp <<EOF && enable uwp && vaapi_win32_extralibs="-ldxgi"
#ifdef WINAPI_FAMILY
#include <winapifamily.h>
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
#error desktop, not uwp
#else
// WINAPI_FAMILY_APP, WINAPI_FAMILY_PHONE_APP => UWP
#endif
#else
#error no family set
#endif
EOF

# mediafoundation requires linking directly to mfplat if building for uwp target
enabled uwp && mediafoundation_extralibs="-lmfplat -lmfuuid -lole32 -lstrmiids" || mediafoundation_extralibs="-lmfuuid -lole32 -lstrmiids"

Expand All @@ -6961,7 +6977,14 @@ enabled vaapi &&
check_pkg_config vaapi "libva >= 0.35.0" "va/va.h" vaInitialize

if enabled vaapi; then
check_pkg_config vaapi_drm "libva-drm" "va/va_drm.h" vaGetDisplayDRM
case $target_os in
mingw32*|mingw64*|win32|win64)
check_pkg_config vaapi_win32 "libva-win32" "va/va_win32.h" vaGetDisplayWin32
;;
*)
check_pkg_config vaapi_drm "libva-drm" "va/va_drm.h" vaGetDisplayDRM
;;
esac

if enabled xlib_x11; then
check_pkg_config vaapi_x11 "libva-x11" "va/va_x11.h" vaGetDisplay
Expand Down
2 changes: 1 addition & 1 deletion fftools/ffmpeg_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1758,7 +1758,7 @@ const OptionDef options[] = {

#if CONFIG_VAAPI
{ "vaapi_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_vaapi_device },
"set VAAPI hardware device (DRM path or X11 display name)", "device" },
"set VAAPI hardware device (DirectX adapter index, DRM path or X11 display name)", "device" },
#endif

#if CONFIG_QSV
Expand Down
5 changes: 2 additions & 3 deletions libavcodec/qsv_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

#include "config.h"

#if CONFIG_VAAPI
#if CONFIG_VAAPI && !_WIN32 // Do not enable for libva-win32 on Windows
#define AVCODEC_QSV_LINUX_SESSION_HANDLE
#endif //CONFIG_VAAPI
#endif //CONFIG_VAAPI && !_WIN32

#ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
#include <stdio.h>
Expand All @@ -35,7 +35,6 @@
#endif
#include <fcntl.h>
#include <va/va.h>
#include <va/va_drm.h>
#include "libavutil/hwcontext_vaapi.h"
#endif

Expand Down
14 changes: 12 additions & 2 deletions libavutil/hwcontext_qsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -2126,8 +2126,6 @@ static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
"\"%s\".\n", e->value);
return AVERROR(EINVAL);
}
} else if (CONFIG_VAAPI) {
child_device_type = AV_HWDEVICE_TYPE_VAAPI;
#if QSV_ONEVPL
} else if (CONFIG_D3D11VA) { // Use D3D11 by default if d3d11va is enabled
av_log(ctx, AV_LOG_VERBOSE,
Expand All @@ -2147,11 +2145,23 @@ static int qsv_device_create(AVHWDeviceContext *ctx, const char *device,
} else if (CONFIG_D3D11VA) {
child_device_type = AV_HWDEVICE_TYPE_D3D11VA;
#endif
} else if (CONFIG_VAAPI) {
child_device_type = AV_HWDEVICE_TYPE_VAAPI;
} else {
av_log(ctx, AV_LOG_ERROR, "No supported child device type is enabled\n");
return AVERROR(ENOSYS);
}

#if CONFIG_VAAPI && _WIN32
/* AV_HWDEVICE_TYPE_VAAPI on Windows/Libva-win32 not supported */
/* Reject user specified child_device_type or CONFIG_VAAPI on Windows */
if (child_device_type == AV_HWDEVICE_TYPE_VAAPI) {
av_log(ctx, AV_LOG_ERROR, "VAAPI child device type not supported for oneVPL on Windows"
"\"%s\".\n", e->value);
return AVERROR(EINVAL);
}
#endif

child_device_opts = NULL;
switch (child_device_type) {
#if CONFIG_VAAPI
Expand Down
75 changes: 74 additions & 1 deletion libavutil/hwcontext_vaapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@

#include "config.h"

#if HAVE_VAAPI_WIN32
# include <windows.h>
#define COBJMACROS
# include <initguid.h>
# include <dxgi1_2.h>
# include "compat/w32dlfcn.h"
# include <va/va_win32.h>
typedef HRESULT (WINAPI *PFN_CREATE_DXGI_FACTORY)(REFIID riid, void **ppFactory);
#endif
#if HAVE_VAAPI_X11
# include <va/va_x11.h>
#endif
Expand Down Expand Up @@ -1663,7 +1672,7 @@ static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
VAAPIDevicePriv *priv;
VADisplay display = NULL;
const AVDictionaryEntry *ent;
int try_drm, try_x11, try_all;
int try_drm, try_x11, try_win32, try_all;

priv = av_mallocz(sizeof(*priv));
if (!priv)
Expand All @@ -1681,6 +1690,8 @@ static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
try_drm = 1;
} else if (!strcmp(ent->value, "x11")) {
try_x11 = 1;
} else if (!strcmp(ent->value, "win32")) {
try_win32 = 1;
} else {
av_log(ctx, AV_LOG_ERROR, "Invalid connection type %s.\n",
ent->value);
Expand All @@ -1690,6 +1701,7 @@ static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
try_all = 1;
try_drm = HAVE_VAAPI_DRM;
try_x11 = HAVE_VAAPI_X11;
try_win32 = HAVE_VAAPI_WIN32;
}

#if HAVE_VAAPI_DRM
Expand Down Expand Up @@ -1797,6 +1809,67 @@ static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
}
#endif

#if HAVE_VAAPI_WIN32
if (!display && try_win32) {
// Try to create a display from the specified device, if any.
if (!device) {
display = vaGetDisplayWin32(NULL);
} else {
IDXGIFactory2 *pDXGIFactory = NULL;
IDXGIAdapter *pAdapter = NULL;
#if !HAVE_UWP
HANDLE dxgi = dlopen("dxgi.dll", 0);
if (!dxgi) {
av_log(ctx, AV_LOG_ERROR, "Failed to load dxgi.dll\n");
return AVERROR_UNKNOWN;
}
PFN_CREATE_DXGI_FACTORY pfnCreateDXGIFactory =
(PFN_CREATE_DXGI_FACTORY)dlsym(dxgi, "CreateDXGIFactory");
if (!pfnCreateDXGIFactory) {
av_log(ctx, AV_LOG_ERROR, "CreateDXGIFactory load failed\n");
return AVERROR_UNKNOWN;
}
#else
// In UWP (which lacks LoadLibrary), CreateDXGIFactory isn't
// available, only CreateDXGIFactory1
PFN_CREATE_DXGI_FACTORY pfnCreateDXGIFactory =
(PFN_CREATE_DXGI_FACTORY)CreateDXGIFactory1;
#endif
if (SUCCEEDED(pfnCreateDXGIFactory(&IID_IDXGIFactory2,
(void **)&pDXGIFactory))) {
int adapter = atoi(device);
if (SUCCEEDED(IDXGIFactory2_EnumAdapters(pDXGIFactory,
adapter,
&pAdapter))) {
DXGI_ADAPTER_DESC desc;
if (SUCCEEDED(IDXGIAdapter2_GetDesc(pAdapter, &desc))) {
av_log(ctx, AV_LOG_INFO,
"Using device %04x:%04x (%ls) - LUID %lu %ld.\n",
desc.VendorId, desc.DeviceId, desc.Description,
desc.AdapterLuid.LowPart,
desc.AdapterLuid.HighPart);
display = vaGetDisplayWin32(&desc.AdapterLuid);
}
IDXGIAdapter_Release(pAdapter);
}
IDXGIFactory2_Release(pDXGIFactory);
}
#if !HAVE_UWP
dlclose(dxgi);
#endif
}

if (!display) {
av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
"from Win32 display.\n");
return AVERROR_UNKNOWN;
}

av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
"Win32 display.\n");
}
#endif

if (!display) {
if (device)
av_log(ctx, AV_LOG_ERROR, "No VA display found for "
Expand Down
2 changes: 1 addition & 1 deletion libavutil/tests/hwdevice.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ static const struct {
{ AV_HWDEVICE_TYPE_OPENCL,
{ "0.0", "0.1", "1.0", "1.1" } },
{ AV_HWDEVICE_TYPE_VAAPI,
{ "/dev/dri/renderD128", "/dev/dri/renderD129", ":0" } },
{ "/dev/dri/renderD128", "/dev/dri/renderD129", ":0", "0", "1" } },
};

static int test_device_type(enum AVHWDeviceType type)
Expand Down
2 changes: 1 addition & 1 deletion libavutil/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
*/

#define LIBAVUTIL_VERSION_MAJOR 58
#define LIBAVUTIL_VERSION_MINOR 5
#define LIBAVUTIL_VERSION_MINOR 6
#define LIBAVUTIL_VERSION_MICRO 100

#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
Expand Down

0 comments on commit ff146c8

Please sign in to comment.