Display and control your Android device
Add VideoToolbox hardware decoding (macOS)
Juhyung Park0d0ccde739b3
diff --git a/app/data/bash-completion/scrcpy b/app/data/bash-completion/scrcpyindex e7173e21..e08c4b63 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 d3d11va' -- "$cur"))+ COMPREPLY=($(compgen -W 'auto disabled vaapi d3d11va videotoolbox' -- "$cur"))return;;--audio-bit-rate \diff --git a/app/data/zsh-completion/_scrcpy b/app/data/zsh-completion/_scrcpyindex b036645f..06e74ec0 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 d3d11va)'+ '--hwdec=[Configure hardware video decoding on the computer]:mode:(auto disabled vaapi d3d11va videotoolbox)''--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.shindex 2fa60ec3..54efe3c0 100755--- a/app/deps/ffmpeg.sh+++ b/app/deps/ffmpeg.sh@@ -120,6 +120,15 @@ else--enable-hwaccel=vp9_d3d11va--enable-hwaccel=vp9_d3d11va2)+ elif [[ "$HOST" == macos ]]+ then+ conf+=(+ --enable-videotoolbox+ --enable-hwaccel=h264_videotoolbox+ --enable-hwaccel=hevc_videotoolbox+ --enable-hwaccel=av1_videotoolbox+ --enable-hwaccel=vp9_videotoolbox+ )fiif [[ "$LINK_TYPE" == static ]]diff --git a/app/meson.build b/app/meson.buildindex 697049c7..1c2021a8 100644--- a/app/meson.build+++ b/app/meson.build@@ -120,6 +120,12 @@ if d3d11va_supportsrc += [ 'src/interop/interop_d3d11va.c' ]endif+videotoolbox_support = get_option('videotoolbox') \+ and host_machine.system() == 'darwin'+if videotoolbox_support+ src += [ 'src/interop/interop_videotoolbox.c' ]+endif+usb_support = get_option('usb')if usb_supportsrc += [@@ -219,6 +225,9 @@ conf.set('HAVE_VAAPI', vaapi_support)# enable D3D11 hardware decoding (windows only)conf.set('HAVE_D3D11VA', d3d11va_support)+# enable VideoToolbox hardware decoding (macOS only)+conf.set('HAVE_VIDEOTOOLBOX', videotoolbox_support)+# enable HID over AOA support (linux only)conf.set('HAVE_USB', usb_support)diff --git a/app/scrcpy.1 b/app/scrcpy.1index 9bd3e336..b5dfb183 100644--- a/app/scrcpy.1+++ b/app/scrcpy.1@@ -239,7 +239,7 @@ Print this help..BI "\-\-hwdec " modeConfigure hardware video decoding on the computer.-Possible values are "auto" (the first available hardware decoder, software decoding otherwise), "disabled" (always use software decoding), "vaapi" (Linux only) and "d3d11va" (Windows only).+Possible values are "auto" (the first available hardware decoder, software decoding otherwise), "disabled" (always use software decoding), "vaapi" (Linux only), "d3d11va" (Windows only) and "videotoolbox" (macOS only).Default is "auto".diff --git a/app/src/cli.c b/app/src/cli.cindex eefa7db6..c751ec86 100644--- a/app/src/cli.c+++ b/app/src/cli.c@@ -438,8 +438,9 @@ 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), \"vaapi\" (Linux only) and "- "\"d3d11va\" (Windows only).\n"+ "(always use software decoding), \"vaapi\" (Linux only), "+ "\"d3d11va\" (Windows only) and \"videotoolbox\" (macOS "+ "only).\n""Default is \"auto\".",},{@@ -2531,8 +2532,18 @@ parse_hwdec_mode(const char *optarg, enum sc_hwdec_mode *mode) {#endif}- LOGE("Unsupported hwdec mode: %s (expected auto, disabled, vaapi or "- "d3d11va)", optarg);+ if (!strcmp(optarg, "videotoolbox")) {+#ifdef HAVE_VIDEOTOOLBOX+ *mode = SC_HWDEC_MODE_VIDEOTOOLBOX;+ return true;+#else+ LOGE("VideoToolbox support is disabled on this platform.");+ return false;+#endif+ }++ LOGE("Unsupported hwdec mode: %s (expected auto, disabled, vaapi, d3d11va "+ "or videotoolbox)", optarg);return false;}diff --git a/app/src/hwdec.c b/app/src/hwdec.cindex 68c76cb1..7f85e0ae 100644--- a/app/src/hwdec.c+++ b/app/src/hwdec.c@@ -104,6 +104,20 @@ sc_hwdec_init(struct sc_hwdec *hwdec, enum AVHWDeviceType hw_type,hwdec->name = "d3d11va";break;+#endif+#ifdef HAVE_VIDEOTOOLBOX+ case AV_HWDEVICE_TYPE_VIDEOTOOLBOX: {+ int ret = av_hwdevice_ctx_create(&hwdec->hw_device_ctx, hw_type,+ NULL, NULL, 0);+ if (ret < 0) {+ LOGE("Could not create VideoToolbox device: %s",+ av_err2str(ret));+ return false;+ }++ hwdec->name = "videotoolbox";+ break;+ }#endifdefault:LOGE("No decoder for hardware device type: %s",diff --git a/app/src/interop/interop.c b/app/src/interop/interop.cindex c018d757..33c141fd 100644--- a/app/src/interop/interop.c+++ b/app/src/interop/interop.c@@ -9,6 +9,9 @@#ifdef HAVE_D3D11VA# include "interop_d3d11va.h"#endif+#ifdef HAVE_VIDEOTOOLBOX+# include "interop_videotoolbox.h"+#endif#include "util/log.h"struct sc_interop *@@ -45,6 +48,21 @@ sc_interop_new(enum sc_hwdec_mode hwdec_mode, SDL_Renderer *renderer,}#endif+#ifdef HAVE_VIDEOTOOLBOX+ if (any || hwdec_mode == SC_HWDEC_MODE_VIDEOTOOLBOX) {+ struct sc_interop_videotoolbox *vt =+ sc_interop_videotoolbox_new(renderer);+ if (vt) {+ return &vt->interop;+ }+ if (!any) {+ LOGE("VideoToolbox interop unavailable");+ return NULL;+ }+ LOGI("VideoToolbox 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_videotoolbox.c b/app/src/interop/interop_videotoolbox.cnew file mode 100644index 00000000..74f94c25--- /dev/null+++ b/app/src/interop/interop_videotoolbox.c@@ -0,0 +1,154 @@+#include "interop_videotoolbox.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_videotoolbox */+#define DOWNCAST(INTEROP) \+ container_of(INTEROP, struct sc_interop_videotoolbox, interop)++static void+sc_interop_videotoolbox_reset(struct sc_interop *interop) {+ if (interop->texture) {+ SDL_DestroyTexture(interop->texture);+ interop->texture = NULL;+ }+}++static void+sc_interop_videotoolbox_destroy(struct sc_interop *interop) {+ if (interop->texture) {+ SDL_DestroyTexture(interop->texture);+ }+}++static SDL_Texture *+sc_interop_videotoolbox_create_texture(struct sc_interop_videotoolbox *vt,+ struct sc_size size,+ SDL_Colorspace colorspace,+ void *pixel_buffer) {+ SDL_PropertiesID props = SDL_CreateProperties();+ if (!props) {+ LOG_OOM();+ return NULL;+ }++ 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);+ // The Metal texture is created from the IOSurface of the pixel buffer+ const char *key = SDL_PROP_TEXTURE_CREATE_METAL_PIXELBUFFER_POINTER;+ ok &= SDL_SetPointerProperty(props, key, pixel_buffer);+ if (!ok) {+ LOGE("Could not set texture properties");+ SDL_DestroyProperties(props);+ return NULL;+ }++ SDL_Texture *texture = SDL_CreateTextureWithProperties(vt->renderer, props);+ SDL_DestroyProperties(props);+ if (!texture) {+ LOGD("Could not create texture: %s", SDL_GetError());+ return NULL;+ }++ return texture;+}++static bool+sc_interop_videotoolbox_import(struct sc_interop *interop,+ const AVFrame *frame) {+ struct sc_interop_videotoolbox *vt = DOWNCAST(interop);++ assert(frame->format == AV_PIX_FMT_VIDEOTOOLBOX);+ assert(frame->hw_frames_ctx);++ const AVHWFramesContext *frames =+ (const AVHWFramesContext *) frame->hw_frames_ctx->data;+ if (frames->sw_format != AV_PIX_FMT_NV12) {+ LOGE("VideoToolbox: unsupported pixel format %s",+ av_get_pix_fmt_name(frames->sw_format));+ return false;+ }++ void *pixel_buffer = frame->data[3];+ assert(pixel_buffer);++ struct sc_size size = {frame->width, frame->height};+ assert(size.width && size.height);++ bool size_changed = !interop->texture+ || interop->frame_size.width != size.width+ || interop->frame_size.height != size.height;++ SDL_Colorspace colorspace =+ sc_interop_to_sdl_color_space(frame->colorspace, frame->color_range);+ SDL_Texture *texture =+ sc_interop_videotoolbox_create_texture(vt, size, colorspace,+ pixel_buffer);+ if (!texture) {+ return false;+ }++ // Release the previous frame only once the new one is wrapped+ sc_interop_videotoolbox_reset(interop);+ interop->texture = texture;+ interop->frame_size = size;++ if (size_changed) {+ LOGI("Texture (VideoToolbox): %" PRIu16 "x%" PRIu16, size.width,+ size.height);+ }++ return true;+}++// The reasons why VideoToolbox is unavailable are logged at info level: whether+// it is an error depends on the caller (only if VideoToolbox was explicitly+// requested)+struct sc_interop_videotoolbox *+sc_interop_videotoolbox_new(SDL_Renderer *renderer) {+ const char *renderer_name = SDL_GetRendererName(renderer);+ if (!renderer_name || strcmp(renderer_name, "metal")) {+ LOGI("VideoToolbox not available: the renderer is not Metal");+ return NULL;+ }++ struct sc_interop_videotoolbox *vt = malloc(sizeof(*vt));+ if (!vt) {+ LOG_OOM();+ return NULL;+ }++ static const struct sc_interop_ops ops = {+ .import = sc_interop_videotoolbox_import,+ .reset = sc_interop_videotoolbox_reset,+ .destroy = sc_interop_videotoolbox_destroy,+ };++ vt->interop.name = "videotoolbox";+ vt->interop.hw_type = AV_HWDEVICE_TYPE_VIDEOTOOLBOX;+ vt->interop.pix_fmt = AV_PIX_FMT_VIDEOTOOLBOX;+ vt->interop.texture = NULL;+ vt->interop.ops = &ops;++ vt->renderer = renderer;++ return vt;+}diff --git a/app/src/interop/interop_videotoolbox.h b/app/src/interop/interop_videotoolbox.hnew file mode 100644index 00000000..c2f2a7d5--- /dev/null+++ b/app/src/interop/interop_videotoolbox.h@@ -0,0 +1,22 @@+#ifndef SC_INTEROP_VIDEOTOOLBOX_H+#define SC_INTEROP_VIDEOTOOLBOX_H++#include "common.h"++#include <SDL3/SDL.h>++#include "interop.h"++struct sc_interop_videotoolbox {+ struct sc_interop interop; // interop trait++ SDL_Renderer *renderer; // owned by the screen++ // The texture wraps the CVPixelBuffer of one frame, so a new texture is+ // created for every frame+};++struct sc_interop_videotoolbox *+sc_interop_videotoolbox_new(SDL_Renderer *renderer);++#endifdiff --git a/app/src/options.h b/app/src/options.hindex 9bbda297..1eba9a80 100644--- a/app/src/options.h+++ b/app/src/options.h@@ -76,6 +76,7 @@ enum sc_hwdec_mode {SC_HWDEC_MODE_DISABLED,SC_HWDEC_MODE_VAAPI,SC_HWDEC_MODE_D3D11VA,+ SC_HWDEC_MODE_VIDEOTOOLBOX,};static inline const char *@@ -89,6 +90,8 @@ sc_hwdec_mode_get_name(enum sc_hwdec_mode mode) {return "vaapi";case SC_HWDEC_MODE_D3D11VA:return "d3d11va";+ case SC_HWDEC_MODE_VIDEOTOOLBOX:+ return "videotoolbox";default:return "(unknown)";}diff --git a/doc/video.md b/doc/video.mdindex a2e59519..1dc24510 100644--- a/doc/video.md+++ b/doc/video.md@@ -120,10 +120,11 @@ hardware decoder is used, and software decoding is used otherwise.Hardware decoding can be configured explicitly:```bash-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+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+scrcpy --hwdec=videotoolbox # VideoToolbox, macOS only```Hardware decoding is only supported when video playback is enabled and V4L2 is@@ -138,6 +139,7 @@ The available hardware decoders are:installed on the system. Trilinear filtering (mipmaps) is not available forhardware frames.- `d3d11va` (Windows only).+ - `videotoolbox` (macOS only).With `--hwdec=auto`, if the hardware decoder cannot decode the stream, FFmpegfalls back to software decoding. It fails instead if a specific hardware decoderdiff --git a/meson_options.txt b/meson_options.txtindex dc19943f..a9ee71d2 100644--- a/meson_options.txt+++ b/meson_options.txt@@ -8,3 +8,4 @@ option('v4l2', type: 'boolean', value: true, description: 'Enable V4L2 feature woption('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)')+option('videotoolbox', type: 'boolean', value: true, description: 'Enable VideoToolbox hardware decoding (macOS)')