Skip to content

Display and control your Android device

Add option to force size alignment

Romain Vimontae13c77c5eee
diff --git a/app/data/bash-completion/scrcpy b/app/data/bash-completion/scrcpy
index 956926ac..dd8ea424 100644
--- a/app/data/bash-completion/scrcpy
+++ b/app/data/bash-completion/scrcpy
@@ -45,6 +45,7 @@ _scrcpy() {
-m --max-size=
-M
--max-fps=
+ --min-size-alignment=
--mouse=
--mouse-bind=
-n --no-control
@@ -189,6 +190,10 @@ _scrcpy() {
COMPREPLY=($(compgen -W "$("${ADB:-adb}" devices | awk '$2 == "device" {print $1}')" -- ${cur}))
return
;;
+ --min-size-alignment)
+ COMPREPLY=($(compgen -> '1 2 4 8 16' -- "$cur"))
+ return
+ ;;
--audio-bit-rate \
|--audio-buffer \
|-b|--video-bit-rate \
diff --git a/app/data/zsh-completion/_scrcpy b/app/data/zsh-completion/_scrcpy
index 15feb054..b1f33154 100644
--- a/app/data/zsh-completion/_scrcpy
+++ b/app/data/zsh-completion/_scrcpy
@@ -52,6 +52,7 @@ arguments=(
{-m,--max-size=}'[Limit both the width and height of the video to value]'
'-M[Use UHID/AOA mouse \(same as --mouse=uhid or --mouse=aoa, depending on OTG mode\)]'
'--max-fps=[Limit the frame rate of screen capture]'
+ '--min-size-alignment=[Minimum video size alignment (1, 2, 4, 8 or 16)]'
'--mouse=[Set the mouse input mode]:mode:(disabled sdk uhid aoa)'
'--mouse-bind=[Configure bindings of secondary clicks]'
{-n,--no-control}'[Disable device control \(mirror the device in read only\)]'
diff --git a/app/scrcpy.1 b/app/scrcpy.1
index f0848077..5013e2c8 100644
--- a/app/scrcpy.1
+++ b/app/scrcpy.1
@@ -296,6 +296,16 @@ Same as \fB\-\-mouse=uhid\fR, or \fB\-\-mouse=aoa\fR if \fB\-\-otg\fR is set.
.BI "\-\-max\-fps " value
Limit the framerate of screen capture (officially supported since Android 10, but may work on earlier versions).
+.TP
+.BI "\-\-min\-size\-alignment " alignment
+Configure the minimum video size alignment.
+
+This is a power-of-2 value (1, 2, 4, 8 or 16) that the video width and height must be multiples of.
+
+The actual alignment will be the maximum of this value and the video codec's alignment requirement.
+
+Default is 1.
+
.TP
.BI "\-\-mouse " mode
Select how to send mouse inputs to the device.
diff --git a/app/src/cli.c b/app/src/cli.c
index bf7297bb..c0ed71fa 100644
--- a/app/src/cli.c
+++ b/app/src/cli.c
@@ -116,6 +116,7 @@ enum {
OPT_DISPLAY_IME_POLICY,
OPT_CAMERA_TORCH,
OPT_CAMERA_ZOOM,
+ OPT_MIN_SIZE_ALIGNMENT,
};
struct sc_option {
@@ -576,6 +577,17 @@ static const struct sc_option options[] = {
.text = "Limit the frame rate of screen capture (officially supported "
"since Android 10, but may work on earlier versions).",
},
+ {
+ .longopt_id = OPT_MIN_SIZE_ALIGNMENT,
+ .longopt = "min-size-alignment",
+ .argdesc = "alignment",
+ .text = "Configure the minimum video size alignment.\n"
+ "This is a power-of-2 value (1, 2, 4, 8 or 16) that the video "
+ "width and height must be multiples of.\n"
+ "The actual alignment will be the maximum of this value and "
+ "the video codec's alignment requirement.\n"
+ "Default is 1.",
+ },
{
.longopt_id = OPT_MOUSE,
.longopt = "mouse",
@@ -1645,6 +1657,22 @@ parse_max_size(const char *s, uint16_t *max_size) {
return true;
}
+static bool
+parse_min_size_alignment(const char *s, uint8_t *min_size_alignment) {
+ long value;
+ bool ok = parse_integer_arg(s, &value, false, 1, 16, "min size alignment");
+ if (!ok) {
+ return false;
+ }
+ if (value & (value - 1)) {
+ LOGE("The minimum size alignment (%ld) must be a power-of-2", value);
+ return false;
+ }
+
+ *min_size_alignment = (uint8_t) value;
+ return true;
+}
+
static bool
parse_buffering_time(const char *s, sc_tick *tick) {
long value;
@@ -2856,6 +2884,12 @@ parse_args_with_getopt(struct scrcpy_cli_args *args, int argc, char *argv[],
return false;
}
break;
+ case OPT_MIN_SIZE_ALIGNMENT:
+ if (!parse_min_size_alignment(optarg,
+ &opts->min_size_alignment)) {
+ return false;
+ }
+ break;
default:
// getopt prints the error message on stderr
return false;
diff --git a/app/src/options.c b/app/src/options.c
index 2a791841..51d2255f 100644
--- a/app/src/options.c
+++ b/app/src/options.c
@@ -49,6 +49,7 @@ const struct scrcpy_options scrcpy_options_default = {
.tunnel_host = 0,
.tunnel_port = 0,
.shortcut_mods = SC_SHORTCUT_MOD_LALT | SC_SHORTCUT_MOD_LSUPER,
+ .min_size_alignment = 1,
.max_size = 0,
.video_bit_rate = 0,
.audio_bit_rate = 0,
diff --git a/app/src/options.h b/app/src/options.h
index bf876040..b869da6b 100644
--- a/app/src/options.h
+++ b/app/src/options.h
@@ -258,6 +258,7 @@ struct scrcpy_options {
uint32_t tunnel_host;
uint16_t tunnel_port;
uint8_t shortcut_mods; // OR of enum sc_shortcut_mod values
+ uint8_t min_size_alignment;
uint16_t max_size;
uint32_t video_bit_rate;
uint32_t audio_bit_rate;
diff --git a/app/src/scrcpy.c b/app/src/scrcpy.c
index fca52074..230096e9 100644
--- a/app/src/scrcpy.c
+++ b/app/src/scrcpy.c
@@ -439,6 +439,7 @@ scrcpy(struct scrcpy_options *options) {
.port_range = options->port_range,
.tunnel_host = options->tunnel_host,
.tunnel_port = options->tunnel_port,
+ .min_size_alignment = options->min_size_alignment,
.max_size = options->max_size,
.video_bit_rate = options->video_bit_rate,
.audio_bit_rate = options->audio_bit_rate,
diff --git a/app/src/server.c b/app/src/server.c
index c4ee8e3d..df6e2b1f 100644
--- a/app/src/server.c
+++ b/app/src/server.c
@@ -305,6 +305,9 @@ execute_server(struct sc_server *server,
VALIDATE_STRING(params->max_fps);
ADD_PARAM("max_fps=%s", params->max_fps);
}
+ if (params->min_size_alignment != 1) {
+ ADD_PARAM("min_size_alignment=%" PRIu8, params->min_size_alignment);
+ }
if (params->angle) {
VALIDATE_STRING(params->angle);
ADD_PARAM("angle=%s", params->angle);
diff --git a/app/src/server.h b/app/src/server.h
index 8aa462b8..03517233 100644
--- a/app/src/server.h
+++ b/app/src/server.h
@@ -41,6 +41,7 @@ struct sc_server_params {
uint32_t tunnel_host;
uint16_t tunnel_port;
uint16_t max_size;
+ uint8_t min_size_alignment;
uint32_t video_bit_rate;
uint32_t audio_bit_rate;
const char *max_fps; // float to be parsed by the server
diff --git a/server/src/main/java/com/genymobile/scrcpy/Options.java b/server/src/main/java/com/genymobile/scrcpy/Options.java
index fc37dbce..d2c324f6 100644
--- a/server/src/main/java/com/genymobile/scrcpy/Options.java
+++ b/server/src/main/java/com/genymobile/scrcpy/Options.java
@@ -27,6 +27,7 @@ public class Options {
private boolean video = true;
private boolean audio = true;
private int maxSize;
+ private int minSizeAlignment = 1;
private VideoCodec videoCodec = VideoCodec.H264;
private AudioCodec audioCodec = AudioCodec.OPUS;
private VideoSource videoSource = VideoSource.DISPLAY;
@@ -102,6 +103,10 @@ public class Options {
return maxSize;
}
+ public int getMinSizeAlignment() {
+ return minSizeAlignment;
+ }
+
public VideoCodec getVideoCodec() {
return videoCodec;
}
@@ -371,6 +376,13 @@ public class Options {
case "max_size":
options.maxSize = Integer.parseInt(value);
break;
+ case "min_size_alignment":
+ int align = Integer.parseInt(value);
+ if (align < 1 || align > 16 || (align & (align - 1)) != 0) {
+ throw new IllegalArgumentException("min_size_alignment (" + align + ") must be 1, 2, 4, 8 or 16");
+ }
+ options.minSizeAlignment = align;
+ break;
case "video_bit_rate":
options.videoBitRate = Integer.parseInt(value);
break;
diff --git a/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java b/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java
index a05a3613..9f0e6eec 100644
--- a/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java
+++ b/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java
@@ -43,6 +43,7 @@ public class SurfaceEncoder implements AsyncProcessor {
private final int videoBitRate;
private final float maxFps;
private final boolean downsizeOnError;
+ private final int minSizeAlignment;
private boolean firstFrameSent;
private int consecutiveErrors;
@@ -60,6 +61,7 @@ public class SurfaceEncoder implements AsyncProcessor {
this.codecOptions = options.getVideoCodecOptions();
this.encoderName = options.getVideoEncoder();
this.downsizeOnError = options.getDownsizeOnError();
+ this.minSizeAlignment = options.getMinSizeAlignment();
}
private void streamCapture() throws IOException, ConfigurationException {
@@ -70,6 +72,10 @@ public class SurfaceEncoder implements AsyncProcessor {
.getVideoCapabilities();
int alignment = caps != null ? Math.max(caps.getWidthAlignment(), caps.getHeightAlignment()) : 8;
Ln.d("Video codec size alignment requirement: " + alignment + "px");
+ if (alignment < minSizeAlignment) {
+ alignment = minSizeAlignment;
+ Ln.d("Actual video size alignment: " + alignment + "px");
+ }
MediaFormat format = createFormat(codec.getMimeType(), videoBitRate, maxFps, codecOptions);