From eee6c588bdf4cd501d7173b7eb6265ae819b8520 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Tue, 24 Mar 2026 00:07:44 +0800 Subject: [PATCH] [fix] fix X11 odd-size captures by aligning dimensions to even values --- .../linux/screen_capturer_x11.cpp | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/screen_capturer/linux/screen_capturer_x11.cpp b/src/screen_capturer/linux/screen_capturer_x11.cpp index c660e45..607267f 100644 --- a/src/screen_capturer/linux/screen_capturer_x11.cpp +++ b/src/screen_capturer/linux/screen_capturer_x11.cpp @@ -121,8 +121,16 @@ int ScreenCapturerX11::Init(const int fps, cb_desktop_data cb) { width_ = attr.width; height_ = attr.height; - if (width_ % 2 != 0 || height_ % 2 != 0) { - LOG_ERROR("Width and height must be even numbers"); + if ((width_ & 1) != 0 || (height_ & 1) != 0) { + LOG_WARN( + "X11 root size {}x{} is not even, aligning down to {}x{} for NV12", + width_, height_, width_ & ~1, height_ & ~1); + width_ &= ~1; + height_ &= ~1; + } + + if (width_ <= 1 || height_ <= 1) { + LOG_ERROR("Invalid capture size after alignment: {}x{}", width_, height_); return -2; } @@ -289,6 +297,16 @@ void ScreenCapturerX11::OnFrame() { src_argb = reinterpret_cast(image->data); } + const size_t y_size = + static_cast(width_) * static_cast(height_); + const size_t uv_size = y_size / 2; + if (y_plane_.size() != y_size) { + y_plane_.resize(y_size); + } + if (uv_plane_.size() != uv_size) { + uv_plane_.resize(uv_size); + } + libyuv::ARGBToNV12(src_argb, width_ * 4, y_plane_.data(), width_, uv_plane_.data(), width_, width_, height_);