Skip to content

Display and control your Android device

Add D3D11VA hardware decoding (Windows)

Juhyung Park2e014375016d
diff --git a/app/data/bash-completion/scrcpy b/app/data/bash-completion/scrcpy
index 482274ea..e7173e21 100644
--- a/app/data/bash-completion/scrcpy
+++ b/app/data/bash-completion/scrcpy
@@ -207,7 +207,7 @@ _scrcpy() {
return
;;
--hwdec)
- COMPREPLY=($(compgen -W 'auto disabled vaapi' -- "$cur"))
+ COMPREPLY=($(compgen -W 'auto disabled vaapi d3d11va' -- "$cur"))
return
;;
--audio-bit-rate \
diff --git a/app/data/zsh-completion/_scrcpy b/app/data/zsh-completion/_scrcpy
index dd8df81e..b036645f 100644
--- a/app/data/zsh-completion/_scrcpy
+++ b/app/data/zsh-completion/_scrcpy
@@ -41,7 +41,7 @@ arguments=(
'-G[Use UHID/AOA gamepad \(same as --gamepad=uhid or --gamepad=aoa, depending on OTG mode\)]'
'--gamepad=[Set the gamepad input mode]:mode:(disabled uhid aoa)'
{-h,--help}'[Print the help]'
- '--hwdec=[Configure hardware video decoding on the computer]:mode:(auto disabled vaapi)'
+ '--hwdec=[Configure hardware video decoding on the computer]:mode:(auto disabled vaapi d3d11va)'
'--ignore-video-encoder-constraints[Do not consider video encoder capabilities]'
'-K[Use UHID/AOA keyboard \(same as --keyboard=uhid or --keyboard=aoa, depending on OTG mode\)]'
'--keep-active[Keep the screen on by simulating user activity]'
diff --git a/app/deps/ffmpeg.sh b/app/deps/ffmpeg.sh
index 8039bd43..2fa60ec3 100755
--- a/app/deps/ffmpeg.sh
+++ b/app/deps/ffmpeg.sh
@@ -104,6 +104,24 @@ else
)
fi
+ if [[ "$HOST" == win* ]]
+ then
+ conf+=(
+ --enable-d3d11va
+ # Only the *_d3d11va2 hwaccels are used, but the FFmpeg Makefile
+ # compiles their source files only for the *_d3d11va hwaccels
+ # (legacy API), so both must be enabled.
+ --enable-hwaccel=h264_d3d11va
+ --enable-hwaccel=h264_d3d11va2
+ --enable-hwaccel=hevc_d3d11va
+ --enable-hwaccel=hevc_d3d11va2
+ --enable-hwaccel=av1_d3d11va
+ --enable-hwaccel=av1_d3d11va2
+ --enable-hwaccel=vp9_d3d11va
+ --enable-hwaccel=vp9_d3d11va2
+ )
+ fi
+
if [[ "$LINK_TYPE" == static ]]
then
conf+=(
diff --git a/app/meson.build b/app/meson.build
index b453b378..697049c7 100644
--- a/app/meson.build
+++ b/app/meson.build
@@ -115,6 +115,11 @@ if vaapi_support
src += [ 'src/interop/interop_vaapi.c' ]
endif
+d3d11va_support = get_option('d3d11va') and host_machine.system() == 'windows'
+if d3d11va_support
+ src += [ 'src/interop/interop_d3d11va.c' ]
+endif
+
usb_support = get_option('usb')
if usb_support
src += [
@@ -211,6 +216,9 @@ conf.set('HAVE_V4L2', v4l2_support)
# enable VA-API hardware decoding (linux only)
conf.set('HAVE_VAAPI', vaapi_support)
+# enable D3D11 hardware decoding (windows only)
+conf.set('HAVE_D3D11VA', d3d11va_support)
+
# enable HID over AOA support (linux only)
conf.set('HAVE_USB', usb_support)
diff --git a/app/scrcpy.1 b/app/scrcpy.1
index f1211dc9..9bd3e336 100644
--- a/app/scrcpy.1
+++ b/app/scrcpy.1
@@ -239,7 +239,7 @@ Print this help.
.BI "\-\-hwdec " mode
Configure hardware video decoding on the computer.
-Possible values are "auto" (the first available hardware decoder, software decoding otherwise), "disabled" (always use software decoding) and "vaapi" (Linux only).
+Possible values are "auto" (the first available hardware decoder, software decoding otherwise), "disabled" (always use software decoding), "vaapi" (Linux only) and "d3d11va" (Windows only).
Default is "auto".
diff --git a/app/src/cli.c b/app/src/cli.c
index bee364e9..eefa7db6 100644
--- a/app/src/cli.c
+++ b/app/src/cli.c
@@ -438,7 +438,8 @@ static const struct sc_option options[] = {
.text = "Configure hardware video decoding on the computer.\n"
"Possible values are \"auto\" (the first available hardware "
"decoder, software decoding otherwise), \"disabled\" "
- "(always use software decoding) and \"vaapi\" (Linux only).\n"
+ "(always use software decoding), \"vaapi\" (Linux only) and "
+ "\"d3d11va\" (Windows only).\n"
"Default is \"auto\".",
},
{
@@ -2520,8 +2521,18 @@ parse_hwdec_mode(const char *optarg, enum sc_hwdec_mode *mode) {
#endif
}
- LOGE("Unsupported hwdec mode: %s (expected auto, disabled or vaapi)",
- optarg);
+ if (!strcmp(optarg, "d3d11va")) {
+#ifdef HAVE_D3D11VA
+ *mode = SC_HWDEC_MODE_D3D11VA;
+ return true;
+#else
+ LOGE("D3D11VA support is disabled on this platform.");
+ return false;
+#endif
+ }
+
+ LOGE("Unsupported hwdec mode: %s (expected auto, disabled, vaapi or "
+ "d3d11va)", optarg);
return false;
}
diff --git a/app/src/hwdec.c b/app/src/hwdec.c
index bd981c5b..68c76cb1 100644
--- a/app/src/hwdec.c
+++ b/app/src/hwdec.c
@@ -5,8 +5,72 @@
#include <libavutil/error.h>
#include <libavutil/pixdesc.h>
+#ifdef HAVE_D3D11VA
+// Define the GUIDs (IID_ID3D10Multithread)
+# define INITGUID
+# define COBJMACROS
+# include <d3d11.h>
+# include <libavutil/hwcontext_d3d11va.h>
+#endif
+
#include "util/log.h"
+#ifdef HAVE_D3D11VA
+static bool
+sc_hwdec_init_d3d11va(struct sc_hwdec *hwdec, SDL_Renderer *renderer) {
+ assert(renderer);
+
+ // The properties are owned by the renderer
+ SDL_PropertiesID props = SDL_GetRendererProperties(renderer);
+ ID3D11Device *device =
+ SDL_GetPointerProperty(props, SDL_PROP_RENDERER_D3D11_DEVICE_POINTER,
+ NULL);
+ if (!device) {
+ LOGE("D3D11VA: SDL did not expose its Direct3D 11 device");
+ return false;
+ }
+
+ UINT flags = ID3D11Device_GetCreationFlags(device);
+ if (flags & D3D11_CREATE_DEVICE_SINGLETHREADED) {
+ LOGE("D3D11VA: the Direct3D 11 device is single-threaded");
+ return false;
+ }
+
+ ID3D10Multithread *multithread = NULL;
+ HRESULT hr = ID3D11Device_QueryInterface(device, &IID_ID3D10Multithread,
+ (void **) &multithread);
+ if (FAILED(hr)) {
+ LOGE("D3D11VA: could not enable multithread protection");
+ return false;
+ }
+ ID3D10Multithread_SetMultithreadProtected(multithread, TRUE);
+ ID3D10Multithread_Release(multithread);
+
+ AVBufferRef *hw_device_ctx =
+ av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_D3D11VA);
+ if (!hw_device_ctx) {
+ LOG_OOM();
+ return false;
+ }
+
+ // FFmpeg releases its reference when the device context is freed
+ AVHWDeviceContext *device_ctx = (AVHWDeviceContext *) hw_device_ctx->data;
+ AVD3D11VADeviceContext *hwctx = device_ctx->hwctx;
+ hwctx->device = device;
+ ID3D11Device_AddRef(device);
+
+ int ret = av_hwdevice_ctx_init(hw_device_ctx);
+ if (ret < 0) {
+ LOGE("Could not create D3D11VA device: %s", av_err2str(ret));
+ av_buffer_unref(&hw_device_ctx);
+ return false;
+ }
+
+ hwdec->hw_device_ctx = hw_device_ctx;
+ return true;
+}
+#endif
+
bool
sc_hwdec_init(struct sc_hwdec *hwdec, enum AVHWDeviceType hw_type,
bool hw_forced, SDL_Renderer *renderer) {
@@ -31,6 +95,15 @@ sc_hwdec_init(struct sc_hwdec *hwdec, enum AVHWDeviceType hw_type,
hwdec->name = "vaapi";
break;
}
+#endif
+#ifdef HAVE_D3D11VA
+ case AV_HWDEVICE_TYPE_D3D11VA:
+ if (!sc_hwdec_init_d3d11va(hwdec, renderer)) {
+ return false;
+ }
+
+ hwdec->name = "d3d11va";
+ break;
#endif
default:
LOGE("No decoder for hardware device type: %s",
diff --git a/app/src/interop/interop.c b/app/src/interop/interop.c
index 76d3797e..c018d757 100644
--- a/app/src/interop/interop.c
+++ b/app/src/interop/interop.c
@@ -6,6 +6,9 @@
#ifdef HAVE_VAAPI
# include "interop_vaapi.h"
#endif
+#ifdef HAVE_D3D11VA
+# include "interop_d3d11va.h"
+#endif
#include "util/log.h"
struct sc_interop *
@@ -28,6 +31,20 @@ sc_interop_new(enum sc_hwdec_mode hwdec_mode, SDL_Renderer *renderer,
}
#endif
+#ifdef HAVE_D3D11VA
+ if (any || hwdec_mode == SC_HWDEC_MODE_D3D11VA) {
+ struct sc_interop_d3d11va *d3d11va = sc_interop_d3d11va_new(renderer);
+ if (d3d11va) {
+ return &d3d11va->interop;
+ }
+ if (!any) {
+ LOGE("D3D11VA interop unavailable");
+ return NULL;
+ }
+ LOGI("D3D11VA interop unavailable");
+ }
+#endif
+
if (any || hwdec_mode == SC_HWDEC_MODE_DISABLED) {
struct sc_interop_software *sw =
sc_interop_software_new(renderer, gl, mipmaps);
diff --git a/app/src/interop/interop_d3d11va.c b/app/src/interop/interop_d3d11va.c
new file mode 100644
index 00000000..90b9d1e4
--- /dev/null
+++ b/app/src/interop/interop_d3d11va.c
@@ -0,0 +1,199 @@
+#include "interop_d3d11va.h"
+
+#include <assert.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libavutil/hwcontext.h>
+#include <libavutil/pixdesc.h>
+
+#include "util/log.h"
+
+/** Downcast interop to sc_interop_d3d11va */
+#define DOWNCAST(INTEROP) \
+ container_of(INTEROP, struct sc_interop_d3d11va, interop)
+
+static void
+sc_interop_d3d11va_reset(struct sc_interop *interop) {
+ if (interop->texture) {
+ SDL_DestroyTexture(interop->texture);
+ interop->texture = NULL;
+ }
+}
+
+static void
+sc_interop_d3d11va_destroy(struct sc_interop *interop) {
+ struct sc_interop_d3d11va *d3d11va = DOWNCAST(interop);
+ if (interop->texture) {
+ SDL_DestroyTexture(interop->texture);
+ }
+ ID3D11DeviceContext_Release(d3d11va->device_ctx);
+}
+
+static bool
+sc_interop_d3d11va_create_texture(struct sc_interop_d3d11va *d3d11va,
+ struct sc_size size,
+ SDL_Colorspace colorspace) {
+ LOGV("Creating new D3D11VA texture: size=%" PRIu16 "x%" PRIu16
+ " color_space=%d ", size.width, size.height, colorspace);
+
+ SDL_PropertiesID props = SDL_CreateProperties();
+ if (!props) {
+ LOG_OOM();
+ return false;
+ }
+
+ bool ok =
+ SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER,
+ SDL_PIXELFORMAT_NV12);
+ ok &= SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER,
+ SDL_TEXTUREACCESS_STATIC);
+ ok &= SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER,
+ size.width);
+ ok &= SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER,
+ size.height);
+ ok &= SDL_SetNumberProperty(props,
+ SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER,
+ colorspace);
+ if (!ok) {
+ LOGE("Could not set texture properties");
+ SDL_DestroyProperties(props);
+ return false;
+ }
+
+ SDL_Texture *texture =
+ SDL_CreateTextureWithProperties(d3d11va->renderer, props);
+ SDL_DestroyProperties(props);
+ if (!texture) {
+ LOGD("Could not create texture: %s", SDL_GetError());
+ return false;
+ }
+
+ d3d11va->interop.texture = texture;
+ d3d11va->texture_size = size;
+ LOGI("Texture (D3D11VA): %" PRIu16 "x%" PRIu16, size.width, size.height);
+ return true;
+}
+
+static bool
+sc_interop_d3d11va_import(struct sc_interop *interop, const AVFrame *frame) {
+ struct sc_interop_d3d11va *d3d11va = DOWNCAST(interop);
+
+ assert(frame->format == AV_PIX_FMT_D3D11);
+ assert(frame->hw_frames_ctx);
+
+ const AVHWFramesContext *frames =
+ (const AVHWFramesContext *) frame->hw_frames_ctx->data;
+ if (frames->sw_format != AV_PIX_FMT_NV12) {
+ LOGE("D3D11VA: unsupported pixel format %s",
+ av_get_pix_fmt_name(frames->sw_format));
+ return false;
+ }
+
+ struct sc_size frame_size = {frame->width, frame->height};
+ assert(frame_size.width && frame_size.height);
+
+ struct sc_size surface_size = {frames->width, frames->height};
+
+ if (!interop->texture
+ || d3d11va->texture_size.width != surface_size.width
+ || d3d11va->texture_size.height != surface_size.height) {
+ // Incompatible texture, recreate it
+ sc_interop_d3d11va_reset(interop);
+
+ SDL_Colorspace colorspace =
+ sc_interop_to_sdl_color_space(frame->colorspace,
+ frame->color_range);
+ bool ok = sc_interop_d3d11va_create_texture(d3d11va, surface_size,
+ colorspace);
+ if (!ok) {
+ return false;
+ }
+ }
+
+ // The properties are owned by the texture
+ SDL_PropertiesID props = SDL_GetTextureProperties(interop->texture);
+ if (!props) {
+ LOGE("Could not get texture properties: %s", SDL_GetError());
+ return false;
+ }
+
+ ID3D11Resource *dst =
+ SDL_GetPointerProperty(props, SDL_PROP_TEXTURE_D3D11_TEXTURE_POINTER,
+ NULL);
+ if (!dst) {
+ LOGE("D3D11VA: SDL did not expose its texture");
+ return false;
+ }
+
+ // The decoded surface is a slice of a texture array
+ ID3D11Resource *src = (ID3D11Resource *) frame->data[0];
+ UINT slice = (UINT) (uintptr_t) frame->data[1];
+ assert(src);
+
+ if (!SDL_FlushRenderer(d3d11va->renderer)) {
+ LOGD("Could not flush renderer before D3D11VA surface copy: %s",
+ SDL_GetError());
+ }
+
+ ID3D11DeviceContext_CopySubresourceRegion(d3d11va->device_ctx, dst,
+ 0, 0, 0, 0, src, slice, NULL);
+
+ interop->frame_size = frame_size;
+ return true;
+}
+
+// The reasons why D3D11VA is unavailable are logged at info level: whether it
+// is an error depends on the caller (only if D3D11VA was explicitly requested)
+struct sc_interop_d3d11va *
+sc_interop_d3d11va_new(SDL_Renderer *renderer) {
+ const char *renderer_name = SDL_GetRendererName(renderer);
+ if (!renderer_name || strcmp(renderer_name, "direct3d11")) {
+ LOGI("D3D11VA not available: the renderer is not Direct3D 11");
+ return NULL;
+ }
+
+ // The properties are owned by the renderer
+ SDL_PropertiesID props = SDL_GetRendererProperties(renderer);
+ ID3D11Device *device =
+ SDL_GetPointerProperty(props, SDL_PROP_RENDERER_D3D11_DEVICE_POINTER,
+ NULL);
+ if (!device) {
+ LOGI("D3D11VA not available: SDL did not expose its Direct3D 11 "
+ "device");
+ return NULL;
+ }
+
+ ID3D11DeviceContext *device_ctx = NULL;
+ ID3D11Device_GetImmediateContext(device, &device_ctx);
+ if (!device_ctx) {
+ LOGI("D3D11VA not available: could not get the Direct3D 11 immediate "
+ "context");
+ return NULL;
+ }
+
+ struct sc_interop_d3d11va *d3d11va = malloc(sizeof(*d3d11va));
+ if (!d3d11va) {
+ LOG_OOM();
+ ID3D11DeviceContext_Release(device_ctx);
+ return NULL;
+ }
+
+ static const struct sc_interop_ops ops = {
+ .import = sc_interop_d3d11va_import,
+ .reset = sc_interop_d3d11va_reset,
+ .destroy = sc_interop_d3d11va_destroy,
+ };
+
+ d3d11va->interop.name = "d3d11va";
+ d3d11va->interop.hw_type = AV_HWDEVICE_TYPE_D3D11VA;
+ d3d11va->interop.pix_fmt = AV_PIX_FMT_D3D11;
+ d3d11va->interop.texture = NULL;
+ d3d11va->interop.ops = &ops;
+
+ d3d11va->renderer = renderer;
+ d3d11va->device_ctx = device_ctx;
+
+ return d3d11va;
+}
diff --git a/app/src/interop/interop_d3d11va.h b/app/src/interop/interop_d3d11va.h
new file mode 100644
index 00000000..aca1fcc7
--- /dev/null
+++ b/app/src/interop/interop_d3d11va.h
@@ -0,0 +1,30 @@
+#ifndef SC_INTEROP_D3D11VA_H
+#define SC_INTEROP_D3D11VA_H
+
+#include "common.h"
+
+#include <SDL3/SDL.h>
+
+#define COBJMACROS
+#include <d3d11.h>
+
+#include "coords.h"
+#include "interop.h"
+
+struct sc_interop_d3d11va {
+ struct sc_interop interop; // interop trait
+
+ SDL_Renderer *renderer; // owned by the screen
+ ID3D11DeviceContext *device_ctx;
+
+ // Only valid if interop.texture != NULL
+ // The decoder surfaces may be larger than the frame for alignment purposes:
+ // the texture has their size to receive a whole copy, and only the frame
+ // area is rendered
+ struct sc_size texture_size;
+};
+
+struct sc_interop_d3d11va *
+sc_interop_d3d11va_new(SDL_Renderer *renderer);
+
+#endif
diff --git a/app/src/options.h b/app/src/options.h
index 995f43c2..9bbda297 100644
--- a/app/src/options.h
+++ b/app/src/options.h
@@ -75,6 +75,7 @@ enum sc_hwdec_mode {
SC_HWDEC_MODE_AUTO,
SC_HWDEC_MODE_DISABLED,
SC_HWDEC_MODE_VAAPI,
+ SC_HWDEC_MODE_D3D11VA,
};
static inline const char *
@@ -86,6 +87,8 @@ sc_hwdec_mode_get_name(enum sc_hwdec_mode mode) {
return "disabled";
case SC_HWDEC_MODE_VAAPI:
return "vaapi";
+ case SC_HWDEC_MODE_D3D11VA:
+ return "d3d11va";
default:
return "(unknown)";
}
diff --git a/app/src/sdl_hints.c b/app/src/sdl_hints.c
index 240806cc..83173b14 100644
--- a/app/src/sdl_hints.c
+++ b/app/src/sdl_hints.c
@@ -53,5 +53,13 @@ sc_sdl_set_hints(const char *render_driver, bool disable_screensaver) {
if (!SDL_SetHint(SDL_HINT_VIDEO_FORCE_EGL, "1")) {
LOGW("Could not force EGL");
}
+
+ // On Windows, SDL creates the Direct3D 11 device single-threaded by
+ // default, but hardware decoding (D3D11VA) uses it from the decoder thread.
+ // Always create it thread-safe, so that the rendering does not depend on
+ // the hardware decoding mode. It has no effect on other platforms.
+ if (!SDL_SetHint(SDL_HINT_RENDER_DIRECT3D_THREADSAFE, "1")) {
+ LOGW("Could not enable Direct3D thread safety");
+ }
}
diff --git a/doc/video.md b/doc/video.md
index ed35fda0..a2e59519 100644
--- a/doc/video.md
+++ b/doc/video.md
@@ -123,6 +123,7 @@ Hardware decoding can be configured explicitly:
scrcpy --hwdec=auto # use hardware decoding if possible (default)
scrcpy --hwdec=disabled # software decoding
scrcpy --hwdec=vaapi # VA-API, Linux only
+scrcpy --hwdec=d3d11va # D3D11VA, Windows only
```
Hardware decoding is only supported when video playback is enabled and V4L2 is
@@ -136,6 +137,7 @@ The available hardware decoders are:
(`va-driver-all` on Debian/Ubuntu). The prebuilt binary uses the libva
installed on the system. Trilinear filtering (mipmaps) is not available for
hardware frames.
+ - `d3d11va` (Windows only).
With `--hwdec=auto`, if the hardware decoder cannot decode the stream, FFmpeg
falls back to software decoding. It fails instead if a specific hardware decoder
diff --git a/meson_options.txt b/meson_options.txt
index 5e389e99..dc19943f 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -7,3 +7,4 @@ option('server_debugger', type: 'boolean', value: false, description: 'Run a ser
option('v4l2', type: 'boolean', value: true, description: 'Enable V4L2 feature when supported')
option('usb', type: 'boolean', value: true, description: 'Enable HID/OTG features when supported')
option('vaapi', type: 'boolean', value: true, description: 'Enable VA-API hardware decoding (Linux)')
+option('d3d11va', type: 'boolean', value: true, description: 'Enable D3D11VA hardware decoding (Windows)')