Skip to content

Display and control your Android device

Add VA-API hardware decoding (Linux)

Juhyung Park8afab472b602
diff --git a/app/data/bash-completion/scrcpy b/app/data/bash-completion/scrcpy
index 896d8068..482274ea 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' -- "$cur"))
+ COMPREPLY=($(compgen -W 'auto disabled vaapi' -- "$cur"))
return
;;
--audio-bit-rate \
diff --git a/app/data/zsh-completion/_scrcpy b/app/data/zsh-completion/_scrcpy
index 5b1aeb87..dd8df81e 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)'
+ '--hwdec=[Configure hardware video decoding on the computer]:mode:(auto disabled vaapi)'
'--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/meson.build b/app/meson.build
index 61b5f32f..663d0fe6 100644
--- a/app/meson.build
+++ b/app/meson.build
@@ -110,6 +110,11 @@ if v4l2_support
src += [ 'src/v4l2_sink.c' ]
endif
+vaapi_support = get_option('vaapi') and host_machine.system() == 'linux'
+if vaapi_support
+ src += [ 'src/interop/interop_vaapi.c' ]
+endif
+
usb_support = get_option('usb')
if usb_support
src += [
@@ -138,6 +143,12 @@ if v4l2_support
dependencies += dependency('libavdevice', static: static)
endif
+if vaapi_support
+ libdrm_dep = dependency('libdrm')
+ dependencies += libdrm_dep.partial_dependency(compile_args: true,
+ includes: true)
+endif
+
if usb_support
dependencies += dependency('libusb-1.0', static: static)
endif
@@ -187,6 +198,9 @@ conf.set('SERVER_DEBUGGER', get_option('server_debugger'))
# enable V4L2 support (linux only)
conf.set('HAVE_V4L2', v4l2_support)
+# enable VA-API hardware decoding (linux only)
+conf.set('HAVE_VAAPI', vaapi_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 371e8f64..f1211dc9 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) and "disabled" (always use software decoding).
+Possible values are "auto" (the first available hardware decoder, software decoding otherwise), "disabled" (always use software decoding) and "vaapi" (Linux only).
Default is "auto".
diff --git a/app/src/cli.c b/app/src/cli.c
index 0baedc8c..bee364e9 100644
--- a/app/src/cli.c
+++ b/app/src/cli.c
@@ -437,8 +437,8 @@ static const struct sc_option options[] = {
.argdesc = "mode",
.text = "Configure hardware video decoding on the computer.\n"
"Possible values are \"auto\" (the first available hardware "
- "decoder, software decoding otherwise) and \"disabled\" "
- "(always use software decoding).\n"
+ "decoder, software decoding otherwise), \"disabled\" "
+ "(always use software decoding) and \"vaapi\" (Linux only).\n"
"Default is \"auto\".",
},
{
@@ -2510,7 +2510,18 @@ parse_hwdec_mode(const char *optarg, enum sc_hwdec_mode *mode) {
return true;
}
- LOGE("Unsupported hwdec mode: %s (expected auto or disabled)", optarg);
+ if (!strcmp(optarg, "vaapi")) {
+#ifdef HAVE_VAAPI
+ *mode = SC_HWDEC_MODE_VAAPI;
+ return true;
+#else
+ LOGE("VA-API support is disabled on this platform.");
+ return false;
+#endif
+ }
+
+ LOGE("Unsupported hwdec mode: %s (expected auto, disabled or vaapi)",
+ optarg);
return false;
}
diff --git a/app/src/hwdec.c b/app/src/hwdec.c
index fe52f13f..bd981c5b 100644
--- a/app/src/hwdec.c
+++ b/app/src/hwdec.c
@@ -2,6 +2,7 @@
#include <assert.h>
+#include <libavutil/error.h>
#include <libavutil/pixdesc.h>
#include "util/log.h"
@@ -18,6 +19,19 @@ sc_hwdec_init(struct sc_hwdec *hwdec, enum AVHWDeviceType hw_type,
hwdec->name = "software";
hwdec->hw_device_ctx = NULL;
break;
+#ifdef HAVE_VAAPI
+ case AV_HWDEVICE_TYPE_VAAPI: {
+ int ret = av_hwdevice_ctx_create(&hwdec->hw_device_ctx, hw_type,
+ NULL, NULL, 0);
+ if (ret < 0) {
+ LOGE("Could not create VA-API device: %s", av_err2str(ret));
+ return false;
+ }
+
+ hwdec->name = "vaapi";
+ break;
+ }
+#endif
default:
LOGE("No decoder for hardware device type: %s",
av_hwdevice_get_type_name(hw_type));
diff --git a/app/src/interop/interop.c b/app/src/interop/interop.c
index a599e1f0..76d3797e 100644
--- a/app/src/interop/interop.c
+++ b/app/src/interop/interop.c
@@ -3,6 +3,9 @@
#include <assert.h>
#include "interop_software.h"
+#ifdef HAVE_VAAPI
+# include "interop_vaapi.h"
+#endif
#include "util/log.h"
struct sc_interop *
@@ -10,6 +13,21 @@ sc_interop_new(enum sc_hwdec_mode hwdec_mode, SDL_Renderer *renderer,
struct sc_opengl *gl, bool mipmaps) {
bool any = hwdec_mode == SC_HWDEC_MODE_AUTO;
+#ifdef HAVE_VAAPI
+ if (any || hwdec_mode == SC_HWDEC_MODE_VAAPI) {
+ struct sc_interop_vaapi *vaapi =
+ sc_interop_vaapi_new(renderer, gl, mipmaps);
+ if (vaapi) {
+ return &vaapi->interop;
+ }
+ if (!any) {
+ LOGE("VA-API interop unavailable");
+ return NULL;
+ }
+ LOGI("VA-API 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_vaapi.c b/app/src/interop/interop_vaapi.c
new file mode 100644
index 00000000..25f55a03
--- /dev/null
+++ b/app/src/interop/interop_vaapi.c
@@ -0,0 +1,353 @@
+#include "interop_vaapi.h"
+
+#include <assert.h>
+#include <inttypes.h>
+#include <stdlib.h>
+
+#include <drm_fourcc.h>
+#include <libavutil/hwcontext.h>
+#include <libavutil/hwcontext_drm.h>
+#include <libavutil/pixdesc.h>
+#include <libavutil/pixfmt.h>
+
+#include "util/log.h"
+
+/** Downcast interop to sc_interop_vaapi */
+#define DOWNCAST(INTEROP) \
+ container_of(INTEROP, struct sc_interop_vaapi, interop)
+
+static_assert(SC_DRM_FORMAT_MOD_INVALID == DRM_FORMAT_MOD_INVALID,
+ "SC_DRM_FORMAT_MOD_INVALID must match drm_fourcc.h");
+
+static void
+sc_interop_vaapi_destroy_images(struct sc_egl *egl,
+ EGLImageKHR images[static 2]) {
+ for (unsigned i = 0; i < 2; ++i) {
+ if (images[i] != EGL_NO_IMAGE_KHR) {
+ sc_egl_destroy_image(egl, images[i]);
+ images[i] = EGL_NO_IMAGE_KHR;
+ }
+ }
+}
+
+static void
+sc_interop_vaapi_unmap_frame(struct sc_interop_vaapi *vaapi) {
+ sc_interop_vaapi_destroy_images(&vaapi->egl, vaapi->images);
+ av_frame_free(&vaapi->drm_frame);
+}
+
+static void
+sc_interop_vaapi_reset(struct sc_interop *interop) {
+ struct sc_interop_vaapi *vaapi = DOWNCAST(interop);
+ if (interop->texture) {
+ sc_interop_vaapi_unmap_frame(vaapi);
+ SDL_DestroyTexture(interop->texture);
+ interop->texture = NULL;
+ }
+}
+
+static void
+sc_interop_vaapi_destroy(struct sc_interop *interop) {
+ struct sc_interop_vaapi *vaapi = DOWNCAST(interop);
+ if (interop->texture) {
+ sc_interop_vaapi_unmap_frame(vaapi);
+ SDL_DestroyTexture(interop->texture);
+ }
+ SDL_ResetHint("SDL_RENDER_OPENGL_NV12_RG_SHADER");
+}
+
+static bool
+sc_interop_vaapi_check_nv12_layout(const AVDRMFrameDescriptor *desc) {
+ return desc->nb_layers == 2
+ && desc->layers[0].format == DRM_FORMAT_R8
+ && desc->layers[0].nb_planes == 1
+ && desc->layers[1].format == DRM_FORMAT_GR88
+ && desc->layers[1].nb_planes == 1;
+}
+
+static EGLImageKHR
+sc_interop_vaapi_import_plane(struct sc_egl *egl,
+ const AVDRMFrameDescriptor *desc, unsigned layer,
+ int width, int height) {
+ const AVDRMLayerDescriptor *l = &desc->layers[layer];
+ const AVDRMPlaneDescriptor *p = &l->planes[0];
+ assert(p->object_index >= 0 && p->object_index < desc->nb_objects);
+ const AVDRMObjectDescriptor *object = &desc->objects[p->object_index];
+
+ struct sc_egl_dma_buf_plane plane = {
+ .fd = object->fd,
+ .offset = p->offset,
+ .pitch = p->pitch,
+ .modifier = object->format_modifier, // might be DRM_FORMAT_MOD_INVALID
+ };
+
+ return sc_egl_create_image_dma_buf(egl, l->format, width, height, &plane);
+}
+
+static bool
+sc_interop_vaapi_create_texture(struct sc_interop_vaapi *vaapi,
+ struct sc_size size,
+ SDL_Colorspace colorspace) {
+ LOGV("Creating new VA-API 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(vaapi->renderer, props);
+ SDL_DestroyProperties(props);
+ if (!texture) {
+ LOGD("Could not create texture: %s", SDL_GetError());
+ return false;
+ }
+
+ static const char *const opengl_keys[2] = {
+ SDL_PROP_TEXTURE_OPENGL_TEXTURE_NUMBER,
+ SDL_PROP_TEXTURE_OPENGL_TEXTURE_UV_NUMBER,
+ };
+
+ static const char *const opengles2_keys[2] = {
+ SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_NUMBER,
+ SDL_PROP_TEXTURE_OPENGLES2_TEXTURE_UV_NUMBER,
+ };
+
+ const char *const *keys = vaapi->gl->is_opengles ? opengles2_keys
+ : opengl_keys;
+ props = SDL_GetTextureProperties(texture);
+ if (!props) {
+ LOGE("Could not get texture properties: %s", SDL_GetError());
+ SDL_DestroyTexture(texture);
+ return false;
+ }
+
+ for (unsigned i = 0; i < 2; ++i) {
+ int64_t texture_id = SDL_GetNumberProperty(props, keys[i], 0);
+ if (!texture_id) {
+ LOGE("Could not get texture id: %s", SDL_GetError());
+ SDL_DestroyTexture(texture);
+ return false;
+ }
+
+ assert(!(texture_id & ~0xFFFFFFFF)); // fits in uint32_t
+ vaapi->texture_ids[i] = texture_id;
+ }
+
+ vaapi->interop.texture = texture;
+ vaapi->interop.frame_size = size;
+ LOGI("Texture (VA-API): %" PRIu16 "x%" PRIu16, size.width, size.height);
+ return true;
+}
+
+static bool
+sc_interop_vaapi_import(struct sc_interop *interop, const AVFrame *frame) {
+ struct sc_interop_vaapi *vaapi = DOWNCAST(interop);
+
+ assert(frame->format == AV_PIX_FMT_VAAPI);
+ assert(frame->hw_frames_ctx);
+
+ const AVHWFramesContext *frames =
+ (const AVHWFramesContext *) frame->hw_frames_ctx->data;
+ if (frames->sw_format != AV_PIX_FMT_NV12) {
+ LOGE("VA-API: unsupported pixel format %s",
+ av_get_pix_fmt_name(frames->sw_format));
+ return false;
+ }
+
+ struct sc_size size = {frame->width, frame->height};
+ assert(size.width && size.height);
+
+ if (!interop->texture
+ || interop->frame_size.width != size.width
+ || interop->frame_size.height != size.height) {
+ // Incompatible texture, recreate it
+ sc_interop_vaapi_reset(interop);
+
+ SDL_Colorspace colorspace =
+ sc_interop_to_sdl_color_space(frame->colorspace,
+ frame->color_range);
+ bool ok = sc_interop_vaapi_create_texture(vaapi, size, colorspace);
+ if (!ok) {
+ return false;
+ }
+ }
+
+ AVFrame *drm_frame = av_frame_alloc();
+ if (!drm_frame) {
+ LOG_OOM();
+ return false;
+ }
+ drm_frame->format = AV_PIX_FMT_DRM_PRIME;
+ int ret = av_hwframe_map(drm_frame, frame,
+ AV_HWFRAME_MAP_READ | AV_HWFRAME_MAP_DIRECT);
+ if (ret < 0) {
+ LOGE("VA-API: could not export the decoded frame as DMA-BUF: %s",
+ av_err2str(ret));
+ av_frame_free(&drm_frame);
+ return false;
+ }
+
+ const AVDRMFrameDescriptor *desc =
+ (const AVDRMFrameDescriptor *) drm_frame->data[0];
+ assert(desc);
+ if (!sc_interop_vaapi_check_nv12_layout(desc)) {
+ uint32_t format = desc->nb_layers ? desc->layers[0].format : 0;
+ LOGE("VA-API: unsupported DRM PRIME layout (layers=%d, format=%#08x)",
+ desc->nb_layers, format);
+ av_frame_free(&drm_frame);
+ return false;
+ }
+
+ struct sc_egl *egl = &vaapi->egl;
+
+ EGLImageKHR images[2];
+ images[0] = sc_interop_vaapi_import_plane(egl, desc, 0,
+ frame->width, frame->height);
+ images[1] = sc_interop_vaapi_import_plane(egl, desc, 1,
+ (frame->width + 1) / 2,
+ (frame->height + 1) / 2);
+ if (images[0] == EGL_NO_IMAGE_KHR || images[1] == EGL_NO_IMAGE_KHR) {
+ LOGE("VA-API: could not import DMA-BUF as EGLImage (EGL error %#x)",
+ vaapi->egl.GetError());
+ sc_interop_vaapi_destroy_images(egl, images);
+ av_frame_free(&drm_frame);
+ return false;
+ }
+
+ // Invalidate SDL OpenGL cache before touching the textures behind its back
+ if (!SDL_FlushRenderer(vaapi->renderer)) {
+ LOGD("Could not flush renderer before DMA-BUF import: %s",
+ SDL_GetError());
+ }
+
+ struct sc_opengl *gl = vaapi->gl;
+ while (gl->GetError()); // Clear any errors left
+
+ for (unsigned i = 0; i < 2; ++i) {
+ gl->BindTexture(GL_TEXTURE_2D, vaapi->texture_ids[i]);
+ egl->EGLImageTargetTexture2DOES(GL_TEXTURE_2D, images[i]);
+ }
+ gl->BindTexture(GL_TEXTURE_2D, 0);
+
+ GLenum gl_error = gl->GetError();
+ if (gl_error) {
+ LOGE("VA-API: could not attach EGLImages to the SDL texture "
+ "(GL error %#x)", gl_error);
+ sc_interop_vaapi_destroy_images(egl, images);
+ av_frame_free(&drm_frame);
+ return false;
+ }
+
+ // Release the previous frame only once the new one is attached
+ sc_interop_vaapi_unmap_frame(vaapi);
+ vaapi->images[0] = images[0];
+ vaapi->images[1] = images[1];
+ vaapi->drm_frame = drm_frame;
+
+ return true;
+}
+
+// The reasons why VA-API is unavailable are logged at info level: whether it is
+// an error depends on the caller (only if VA-API was explicitly requested)
+static bool
+sc_interop_vaapi_init_egl(struct sc_interop_vaapi *vaapi) {
+ struct sc_egl *egl = &vaapi->egl;
+ if (!sc_egl_init(egl)) {
+ LOGI("VA-API: EGL not available");
+ return false;
+ }
+
+ if (!egl->CreateImageKHR) {
+ LOGI("VA-API: EGL_KHR_image_base not supported");
+ return false;
+ }
+
+ if (!egl->has_dma_buf_import) {
+ LOGI("VA-API: EGL_EXT_image_dma_buf_import not supported");
+ return false;
+ }
+
+ if (!egl->EGLImageTargetTexture2DOES) {
+ LOGI("VA-API: GL_OES_EGL_image not supported");
+ return false;
+ }
+
+ return true;
+}
+
+struct sc_interop_vaapi *
+sc_interop_vaapi_new(SDL_Renderer *renderer, struct sc_opengl *gl,
+ bool mipmaps) {
+ if (!gl) {
+ LOGI("VA-API not available: the renderer is not OpenGL or OpenGL ES");
+ return NULL;
+ }
+
+ struct sc_interop_vaapi *vaapi = malloc(sizeof(*vaapi));
+ if (!vaapi) {
+ LOG_OOM();
+ return NULL;
+ }
+
+ bool ok = sc_interop_vaapi_init_egl(vaapi);
+ if (!ok) {
+ free(vaapi);
+ return NULL;
+ }
+
+ // SDL normally stores NV12 chroma in a GL_LUMINANCE_ALPHA texture and
+ // samples it from .ra. An EGLImage imported from DRM_FORMAT_GR88 is a
+ // two-channel texture, so it must be sampled from .rg instead.
+ ok = SDL_SetHintWithPriority("SDL_RENDER_OPENGL_NV12_RG_SHADER", "1",
+ SDL_HINT_OVERRIDE);
+ if (!ok) {
+ LOGE("Could not set the NV12 shader hint");
+ free(vaapi);
+ return NULL;
+ }
+
+ if (mipmaps) {
+ LOGI("Trilinear filtering disabled for VA-API frames");
+ }
+
+ static const struct sc_interop_ops ops = {
+ .import = sc_interop_vaapi_import,
+ .reset = sc_interop_vaapi_reset,
+ .destroy = sc_interop_vaapi_destroy,
+ };
+
+ vaapi->interop.name = "vaapi";
+ vaapi->interop.hw_type = AV_HWDEVICE_TYPE_VAAPI;
+ vaapi->interop.pix_fmt = AV_PIX_FMT_VAAPI;
+ vaapi->interop.texture = NULL;
+ vaapi->interop.ops = &ops;
+
+ vaapi->renderer = renderer;
+ vaapi->gl = gl;
+ vaapi->images[0] = EGL_NO_IMAGE_KHR;
+ vaapi->images[1] = EGL_NO_IMAGE_KHR;
+ vaapi->drm_frame = NULL;
+
+ return vaapi;
+}
diff --git a/app/src/interop/interop_vaapi.h b/app/src/interop/interop_vaapi.h
new file mode 100644
index 00000000..7e115a51
--- /dev/null
+++ b/app/src/interop/interop_vaapi.h
@@ -0,0 +1,34 @@
+#ifndef SC_INTEROP_VAAPI_H
+#define SC_INTEROP_VAAPI_H
+
+#include "common.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <SDL3/SDL.h>
+#include <libavutil/frame.h>
+
+#include "egl.h"
+#include "interop.h"
+#include "opengl.h"
+
+struct sc_interop_vaapi {
+ struct sc_interop interop; // interop trait
+
+ SDL_Renderer *renderer; // owned by the screen
+
+ struct sc_opengl *gl;
+ struct sc_egl egl;
+
+ // Only valid if interop.texture != NULL
+ uint32_t texture_ids[2]; // OpenGL textures of the Y and UV planes
+
+ EGLImageKHR images[2];
+ AVFrame *drm_frame;
+};
+
+struct sc_interop_vaapi *
+sc_interop_vaapi_new(SDL_Renderer *renderer, struct sc_opengl *gl,
+ bool mipmaps);
+
+#endif
diff --git a/app/src/options.h b/app/src/options.h
index fd6ce355..995f43c2 100644
--- a/app/src/options.h
+++ b/app/src/options.h
@@ -74,6 +74,7 @@ enum sc_audio_source {
enum sc_hwdec_mode {
SC_HWDEC_MODE_AUTO,
SC_HWDEC_MODE_DISABLED,
+ SC_HWDEC_MODE_VAAPI,
};
static inline const char *
@@ -83,6 +84,8 @@ sc_hwdec_mode_get_name(enum sc_hwdec_mode mode) {
return "auto";
case SC_HWDEC_MODE_DISABLED:
return "disabled";
+ case SC_HWDEC_MODE_VAAPI:
+ return "vaapi";
default:
return "(unknown)";
}
diff --git a/app/src/sdl_hints.c b/app/src/sdl_hints.c
index b7fc55b4..240806cc 100644
--- a/app/src/sdl_hints.c
+++ b/app/src/sdl_hints.c
@@ -43,5 +43,15 @@ sc_sdl_set_hints(const char *render_driver, bool disable_screensaver) {
&& !SDL_SetHint(SDL_HINT_VIDEO_ALLOW_SCREENSAVER, "1")) {
LOGW("Could not enable screensaver");
}
+
+ // On X11, SDL creates the OpenGL contexts with GLX by default, but
+ // importing hardware decoded frames (VA-API) requires EGL. Always use EGL,
+ // so that the rendering does not depend on the hardware decoding mode. It
+ // has no effect on other platforms.
+ // The environment variable SDL_VIDEO_FORCE_EGL=0 takes priority to restore
+ // GLX (hardware decoding is then unavailable).
+ if (!SDL_SetHint(SDL_HINT_VIDEO_FORCE_EGL, "1")) {
+ LOGW("Could not force EGL");
+ }
}
diff --git a/doc/build.md b/doc/build.md
index d31554ea..e12fad1a 100644
--- a/doc/build.md
+++ b/doc/build.md
@@ -50,12 +50,12 @@ Install the required packages from your package manager.
```bash
# runtime dependencies
-sudo apt install ffmpeg libsdl3-0 adb libusb-1.0-0
+sudo apt install ffmpeg libsdl3-0 adb libusb-1.0-0 va-driver-all
# client build dependencies
sudo apt install gcc git pkg-config meson ninja-build libsdl3-dev \
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev \
- libswresample-dev libusb-1.0-0-dev libv4l-dev
+ libswresample-dev libusb-1.0-0-dev libv4l-dev libdrm-dev
# server build dependencies
sudo apt install openjdk-17-jdk
@@ -77,7 +77,8 @@ pip3 install meson
sudo dnf install https://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm
# client build dependencies
-sudo dnf install SDL3-devel ffms2-devel libusb1-devel libavdevice-free-devel meson gcc make
+sudo dnf install SDL3-devel ffms2-devel libusb1-devel libavdevice-free-devel \
+ libdrm-devel meson gcc make
# server build dependencies
sudo dnf install java-devel
diff --git a/doc/linux.md b/doc/linux.md
index 999625ae..d314a7fc 100644
--- a/doc/linux.md
+++ b/doc/linux.md
@@ -37,10 +37,10 @@ First, you need to install the required packages:
```bash
# for Debian/Ubuntu
-sudo apt install ffmpeg libsdl3-0 libusb-1.0-0 adb wget \
+sudo apt install ffmpeg libsdl3-0 libusb-1.0-0 adb wget va-driver-all \
gcc git pkg-config meson ninja-build libsdl3-dev \
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev \
- libswresample-dev libusb-1.0-0-dev libv4l-dev
+ libswresample-dev libusb-1.0-0-dev libv4l-dev libdrm-dev
```
Then clone the repo and execute the installation script
diff --git a/doc/video.md b/doc/video.md
index 837c70ab..26ef476a 100644
--- a/doc/video.md
+++ b/doc/video.md
@@ -120,14 +120,25 @@ hardware decoder is used, and software decoding is used otherwise.
Hardware decoding can be configured explicitly:
```bash
-scrcpy --hwdec=auto # default
+scrcpy --hwdec=auto # use hardware decoding if possible (default)
scrcpy --hwdec=disabled # software decoding
+scrcpy --hwdec=vaapi # VA-API, Linux only
```
Hardware decoding is only supported when video playback is enabled and V4L2 is
disabled.
-For now, only software decoding is available.
+The available hardware decoders are:
+ - `vaapi` (Linux only). This requires the renderer to use EGL: this is always
+ the case on Wayland, and scrcpy also requests EGL on X11 (where SDL uses GLX
+ by default). Set `SDL_VIDEO_FORCE_EGL=0` to force GLX (hardware decoding is
+ then unavailable). The VA-API driver for the GPU must be installed
+ (`va-driver-all` on Debian/Ubuntu). Trilinear filtering (mipmaps) is not
+ available for hardware frames.
+
+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
+is explicitly requested (e.g. `--hwdec=vaapi`).
## Orientation
diff --git a/meson_options.txt b/meson_options.txt
index fd347734..5e389e99 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -6,3 +6,4 @@ option('static', type: 'boolean', value: false, description: 'Use static depende
option('server_debugger', type: 'boolean', value: false, description: 'Run a server debugger and wait for a client to be attached')
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)')
diff --git a/release/build_linux.sh b/release/build_linux.sh
index 7810c803..371f0af1 100755
--- a/release/build_linux.sh
+++ b/release/build_linux.sh
@@ -27,6 +27,7 @@ unset PKG_CONFIG_PATH
export PKG_CONFIG_LIBDIR="$DEPS_INSTALL_DIR/lib/pkgconfig"
rm -rf "$LINUX_BUILD_DIR"
+# VA-API is not available in the static build (it requires libva and libdrm)
meson setup "$LINUX_BUILD_DIR" \
-Dc_args="-I$DEPS_INSTALL_DIR/include" \
-Dc_link_args="-L$DEPS_INSTALL_DIR/lib" \
@@ -35,7 +36,8 @@ meson setup "$LINUX_BUILD_DIR" \
-Db_lto=true \
-Dcompile_server=false \
-Dportable=true \
- -Dstatic=true
+ -Dstatic=true \
+ -Dvaapi=false
ninja -C "$LINUX_BUILD_DIR"
# Group intermediate outputs into a 'dist' directory