perf(codec): upgrade x264 preset and downscale to the receiver display

Two quality improvements:

Preset: ultrafast -> veryfast. Unlocks Main profile with CABAC
entropy coding, hexagonal motion search, 3 reference frames, and
adaptive quantization — typically 30-40% better quality at the same
bitrate. The desktop handles the extra encoding cost trivially
(150+ fps at 1080p).

Downscaling: the receiver now advertises its display resolution in
the signaling answer (display_width/display_height, 0 = unknown).
When the capture exceeds the display (e.g. 2256x1504 source on a
1920x1080 receiver), the sender scales down preserving aspect ratio
before encoding — the same sws_scale pass that already converts the
pixel format also handles the resolution change, so there is no
extra step. This concentrates the entire bitrate into pixels the
display actually shows (~2.7x more bits per visible pixel at
4000 kbps when going from 2256x1504 to 1620x1080).

The renderer caches the display size during window creation
(native monitor resolution in fullscreen/KMSDRM; window size
otherwise). The receiver includes it in every signaling answer; the
sender pipeline computes aspect-preserving, even-rounded scaled
dimensions when the display is smaller than the capture.

meson test 5/5 in both configurations, valgrind clean.
This commit is contained in:
2026-09-09 11:02:42 +02:00
parent 6516b45b02
commit 30538fba73
8 changed files with 85 additions and 5 deletions
+19 -5
View File
@@ -286,8 +286,12 @@ class FfmpegEncoder final : public Encoder {
return CodecError{"failed to allocate AVFrame"};
}
output->width = frame.width;
output->height = frame.height;
// The output frame is at the encoder's configured dimensions (which
// may be smaller than the capture when downscaling to the receiver's
// display); sws_scale handles both the format conversion and the
// resolution change in one pass.
output->width = config_.width;
output->height = config_.height;
output->format = AV_PIX_FMT_YUV420P;
output->time_base = ctx_->time_base;
output->pts =
@@ -338,8 +342,18 @@ class FfmpegEncoder final : public Encoder {
return true;
}
scaler_.reset(sws_getContext(
width, height, input_format, width, height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, nullptr, nullptr, nullptr));
// Scale to the encoder's configured output (which may be smaller
// than the input when downscaling to the receiver's display).
scaler_.reset(sws_getContext(width,
height,
input_format,
config_.width,
config_.height,
AV_PIX_FMT_YUV420P,
SWS_BILINEAR,
nullptr,
nullptr,
nullptr));
if (scaler_ == nullptr) {
return false;
}
@@ -411,7 +425,7 @@ CodecResult<std::unique_ptr<Encoder>> EncoderFactory::create(const EncoderConfig
// without out-of-band parameter negotiation.
ctx->flags |= AV_CODEC_FLAG_LOW_DELAY;
if (av_opt_set(ctx->priv_data, "preset", "ultrafast", 0) < 0) {
if (av_opt_set(ctx->priv_data, "preset", "veryfast", 0) < 0) {
return CodecError{"failed to set libx264 preset"};
}
if (av_opt_set(ctx->priv_data, "tune", "zerolatency", 0) < 0) {