From nobody Sun Nov 16 00:58:31 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1745827499489198.23418065304986; Mon, 28 Apr 2025 01:04:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u9JSz-0004qL-KF; Mon, 28 Apr 2025 04:03:53 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u9JSt-0004jV-Sd for qemu-devel@nongnu.org; Mon, 28 Apr 2025 04:03:49 -0400 Received: from [94.136.29.99] (helo=zilli.proxmox.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u9JSs-00052S-2A for qemu-devel@nongnu.org; Mon, 28 Apr 2025 04:03:47 -0400 Received: by zilli.proxmox.com (Postfix, from userid 1000) id DF1221C0ACB; Mon, 28 Apr 2025 10:03:38 +0200 (CEST) From: Dietmar Maurer To: marcandre.lureau@redhat.com, qemu-devel@nongnu.org Cc: Dietmar Maurer Subject: [PATCH v4 5/8] h264: search for available h264 encoder Date: Mon, 28 Apr 2025 10:03:33 +0200 Message-Id: <20250428080336.2574852-6-dietmar@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250428080336.2574852-1-dietmar@proxmox.com> References: <20250428080336.2574852-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 94.136.29.99 (failed) Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=94.136.29.99; envelope-from=dietmar@zilli.proxmox.com; helo=zilli.proxmox.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, NO_DNS_FOR_FROM=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1745827500978019100 Content-Type: text/plain; charset="utf-8" The search list is currently hardcoded to: ["x264enc", "openh264enc"] x264enc: is probably the best available software encoder openh264enc: lower quality, but available on more systems. We restrict encoders to a known list because each encoder requires fine tuning to get reasonable/usable results. Signed-off-by: Dietmar Maurer --- ui/vnc-enc-h264.c | 82 ++++++++++++++++++++++++++++++++++++++--------- ui/vnc.h | 1 + 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/ui/vnc-enc-h264.c b/ui/vnc-enc-h264.c index 26e8c19270..191e3aeb39 100644 --- a/ui/vnc-enc-h264.c +++ b/ui/vnc-enc-h264.c @@ -9,6 +9,61 @@ =20 #include =20 +const char *encoder_list[] =3D { "x264enc", "openh264enc" }; + +static const char *get_available_encoder(void) +{ + for (int i =3D 0; i < G_N_ELEMENTS(encoder_list); i++) { + GstElement *element =3D gst_element_factory_make( + encoder_list[i], "video-encoder"); + if (element !=3D NULL) { + gst_object_unref(element); + return encoder_list[i]; + } + } + return NULL; +} + +static GstElement *create_encoder(const char *encoder_name) +{ + GstElement *encoder =3D gst_element_factory_make( + encoder_name, "video-encoder"); + if (!encoder) { + VNC_DEBUG("Could not create gst '%s' video encoder\n", encoder_nam= e); + return NULL; + } + + if (!strcmp(encoder_name, "x264enc")) { + g_object_set( + encoder, + "tune", 4, /* zerolatency */ + /* + * fix for zerolatency with novnc (without, + * noVNC displays green stripes) + */ + "threads", 1, + "pass", 5, /* Constant Quality */ + "quantizer", 26, + /* avoid access unit delimiters (Nal Unit Type 9) - not requir= ed */ + "aud", false, + NULL); + } else if (!strcmp(encoder_name, "openh264enc")) { + g_object_set( + encoder, + "usage-type", 1, /* screen content */ + "complexity", 0, /* low, high speed */ + "rate-control", 0, /* quality mode */ + "qp-min", 20, + "qp-max", 27, + NULL); + } else { + VNC_DEBUG("Unknown H264 encoder name '%s' - not setting any proper= ties", + encoder_name); + } + + return encoder; +} + static void destroy_encoder_context(VncState *vs) { gst_clear_object(&vs->h264->source); @@ -46,26 +101,12 @@ static bool create_encoder_context(VncState *vs, int w= , int h) goto error; } =20 - vs->h264->gst_encoder =3D gst_element_factory_make("x264enc", "gst-enc= oder"); + vs->h264->gst_encoder =3D create_encoder(vs->h264->encoder_name); if (!vs->h264->gst_encoder) { VNC_DEBUG("Could not create gst x264 encoder\n"); goto error; } =20 - g_object_set( - vs->h264->gst_encoder, - "tune", 4, /* zerolatency */ - /* - * fix for zerolatency with novnc (without, noVNC displays - * green stripes) - */ - "threads", 1, - "pass", 5, /* Constant Quality */ - "quantizer", 26, - /* avoid access unit delimiters (Nal Unit Type 9) - not required */ - "aud", false, - NULL); - vs->h264->sink =3D gst_element_factory_make("appsink", "sink"); if (!vs->h264->sink) { VNC_DEBUG("Could not create gst sink\n"); @@ -150,9 +191,20 @@ static bool create_encoder_context(VncState *vs, int w= , int h) =20 bool vnc_h264_encoder_init(VncState *vs) { + const char *encoder_name; + g_assert(vs->h264 =3D=3D NULL); =20 + encoder_name =3D get_available_encoder(); + if (encoder_name =3D=3D NULL) { + VNC_DEBUG("No H264 encoder available.\n"); + return -1; + } + vs->h264 =3D g_new0(VncH264, 1); + vs->h264->encoder_name =3D encoder_name; + + VNC_DEBUG("Allow H264 using encoder '%s`\n", encoder_name); =20 return true; } diff --git a/ui/vnc.h b/ui/vnc.h index 29012b75c7..4afc68d6ec 100644 --- a/ui/vnc.h +++ b/ui/vnc.h @@ -239,6 +239,7 @@ typedef struct VncZywrle { /* Number of frames we send after the display is clean. */ #define VNC_H264_KEEP_DIRTY 10 typedef struct VncH264 { + const char *encoder_name; GstElement *pipeline, *source, *gst_encoder, *sink, *convert; size_t width; size_t height; --=20 2.39.5