From nobody Fri Dec 19 08:06:17 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 1744978836688201.63265824038956; Fri, 18 Apr 2025 05:20:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5jvT-0005Vg-Iq; Fri, 18 Apr 2025 07:30:31 -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 1u5jvM-0005SB-12 for qemu-devel@nongnu.org; Fri, 18 Apr 2025 07:30:28 -0400 Received: from [94.136.29.99] (helo=zilli.proxmox.com) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5jv5-0005cv-7C for qemu-devel@nongnu.org; Fri, 18 Apr 2025 07:30:23 -0400 Received: by zilli.proxmox.com (Postfix, from userid 1000) id 34DAF1C167F; Fri, 18 Apr 2025 13:29:58 +0200 (CEST) From: Dietmar Maurer To: marcandre.lureau@redhat.com, qemu-devel@nongnu.org Cc: Dietmar Maurer Subject: [PATCH v3 4/9] h264: search for available h264 encoder Date: Fri, 18 Apr 2025 13:29:48 +0200 Message-Id: <20250418112953.1744442-5-dietmar@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250418112953.1744442-1-dietmar@proxmox.com> References: <20250418112953.1744442-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: 1744978839567019100 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 | 89 +++++++++++++++++++++++++++++++++++++++-------- ui/vnc.h | 1 + 2 files changed, 75 insertions(+), 15 deletions(-) diff --git a/ui/vnc-enc-h264.c b/ui/vnc-enc-h264.c index 3abe6a1528..047f4a3128 100644 --- a/ui/vnc-enc-h264.c +++ b/ui/vnc-enc-h264.c @@ -27,6 +27,68 @@ =20 #include =20 +const char *encoder_list[] =3D { "x264enc", "openh264enc", NULL }; + +static const char *get_available_encoder(void) +{ + int i =3D 0; + do { + const char *encoder_name =3D encoder_list[i]; + if (encoder_name =3D=3D NULL) { + break; + } + GstElement *element =3D gst_element_factory_make( + encoder_name, "video-encoder"); + if (element !=3D NULL) { + gst_object_unref(element); + return encoder_name; + } + i =3D i + 1; + } while (true); + + 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); @@ -68,26 +130,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"); @@ -172,9 +220,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 a5ea134de8..e97276349e 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