mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arun Iyengar via B4 Relay <devnull+aiyengar1990.gmail.com@kernel.org>
To: Mauro Carvalho Chehab <mchehab@kernel.org>,
	 Paul Kocialkowski <paulk@sys-base.io>
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Nicolas Dufresne <nicolas.dufresne@collabora.com>,
	 Hans Verkuil <hverkuil@kernel.org>,
	Marco Felsch <m.felsch@pengutronix.de>,
	 Michael Tretter <m.tretter@pengutronix.de>,
	 Arun Iyengar <aiyengar1990@gmail.com>
Subject: [PATCH RFC 11/11] media: h264: Add a VBR mode to the stateless encode rate control
Date: Tue, 10 Nov 2026 10:05:25 +0000	[thread overview]
Message-ID: <20261110-h264-encode-fixes-v1-11-7a64486df499@gmail.com> (raw)
In-Reply-To: <20261110-h264-encode-fixes-v1-0-7a64486df499@gmail.com>

From: Arun Iyengar <aiyengar1990@gmail.com>

The core reads V4L2_CID_MPEG_VIDEO_BITRATE and _BITRATE_PEAK for
V4L2_MPEG_VIDEO_BITRATE_MODE_VBR, but has no mode for it, so a driver
offering VBR fails the first picture with -EINVAL.

Add one with the encode rate control and its peak bitrate: the bitrate is
met on average and every second stays within the peak (a peak below the
bitrate counts as the bitrate). Drivers offer it with the VBR entry of
V4L2_CID_MPEG_VIDEO_BITRATE_MODE and V4L2_CID_MPEG_VIDEO_BITRATE_PEAK.

Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
 drivers/media/v4l2-core/v4l2-h264-enc-rc.c | 24 ++++++++++++++++++++++++
 drivers/media/v4l2-core/v4l2-h264-enc.c    |  3 ++-
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/drivers/media/v4l2-core/v4l2-h264-enc-rc.c b/drivers/media/v4l2-core/v4l2-h264-enc-rc.c
index c4f1718ead0e..691c80c2a950 100644
--- a/drivers/media/v4l2-core/v4l2-h264-enc-rc.c
+++ b/drivers/media/v4l2-core/v4l2-h264-enc-rc.c
@@ -218,10 +218,34 @@ static const struct v4l2_h264_enc_rc_mode cbr_mode = {
 	.complete	= cbr_complete,
 };
 
+/*
+ * Variable bitrate mode: the same rate control with the peak bitrate, which
+ * meets the bitrate on average and keeps every second within the peak.
+ */
+static int vbr_measure(struct v4l2_h264_enc_rc *rc, unsigned long *size)
+{
+	struct v4l2_h264_enc_state *state = rc->state;
+
+	v4l2_enc_cbr_set_peak(&rc->cbr, max(state->bitrate_peak,
+					    state->bitrate));
+
+	return cbr_measure(rc, size);
+}
+
+static const struct v4l2_h264_enc_rc_mode vbr_mode = {
+	.name		= "variable bitrate",
+	.type		= V4L2_H264_ENC_RC_TYPE_VBR,
+	.init		= cbr_init,
+	.measure	= vbr_measure,
+	.estimate	= cbr_estimate,
+	.complete	= cbr_complete,
+};
+
 static const struct v4l2_h264_enc_rc_mode *modes[] = {
 	&direct_mode,
 	&cq_mode,
 	&cbr_mode,
+	&vbr_mode,
 };
 
 static int mode_prepare(struct v4l2_h264_enc_rc *rc)
diff --git a/drivers/media/v4l2-core/v4l2-h264-enc.c b/drivers/media/v4l2-core/v4l2-h264-enc.c
index 9745c0e85ee5..66473231df94 100644
--- a/drivers/media/v4l2-core/v4l2-h264-enc.c
+++ b/drivers/media/v4l2-core/v4l2-h264-enc.c
@@ -1273,7 +1273,8 @@ static int rc_step(struct v4l2_h264_enc *enc)
 	if (rc->mode) {
 		pr_debug("  rc mode: %s", rc->mode->name);
 
-		if (rc->mode->type == V4L2_H264_ENC_RC_TYPE_CBR) {
+		if (rc->mode->type == V4L2_H264_ENC_RC_TYPE_CBR ||
+		    rc->mode->type == V4L2_H264_ENC_RC_TYPE_VBR) {
 			pr_debug("  rc bitrate: %u bits/s", state->bitrate);
 			pr_debug("  rc target: %lu bytes", rc->size);
 		}

-- 
2.55.0



      parent reply	other threads:[~2026-11-10 10:05 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10 10:05 [PATCH RFC 00/11] media: h264: Stateless encode core follow-ups: parameter sets, POC type 2, CBR and VBR Arun Iyengar via B4 Relay
2026-11-10 10:05 ` [PATCH RFC 01/11] media: h264: Only write the SPS and PPS of a stateless encode when they change Arun Iyengar via B4 Relay
2026-11-10 10:05 ` [PATCH RFC 02/11] media: h264: Support picture order count type 2 in stateless encode Arun Iyengar via B4 Relay
2026-11-10 10:05 ` [PATCH RFC 03/11] media: h264: Write the stateless encode SPS and PPS with nal_ref_idc 3 Arun Iyengar via B4 Relay
2026-11-10 10:05 ` [PATCH RFC 04/11] media: v4l2-h264-enc-rc: Rework the constant bitrate mode Arun Iyengar via B4 Relay
2026-11-10 10:05 ` [PATCH RFC 05/11] media: v4l2-core: Move the H.264 encode CBR rate control to a helper Arun Iyengar via B4 Relay
2026-11-10 10:05 ` [PATCH RFC 06/11] media: v4l2-core: Bound the I picture size in the encode CBR rate control Arun Iyengar via B4 Relay
2026-11-10 10:05 ` [PATCH RFC 07/11] media: v4l2-core: Halve the I picture QP increase of the encode CBR Arun Iyengar via B4 Relay
2026-11-10 10:05 ` [PATCH RFC 08/11] media: v4l2-core: Re-encode oversized pictures in " Arun Iyengar via B4 Relay
2026-11-10 10:05 ` [PATCH RFC 09/11] media: v4l2-core: Fix all-intra streams in the encode CBR rate control Arun Iyengar via B4 Relay
2026-11-10 10:05 ` [PATCH RFC 10/11] media: v4l2-core: Add a peak bitrate to the encode " Arun Iyengar via B4 Relay
2026-11-10 10:05 ` Arun Iyengar via B4 Relay [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261110-h264-encode-fixes-v1-11-7a64486df499@gmail.com \
    --to=devnull+aiyengar1990.gmail.com@kernel.org \
    --cc=aiyengar1990@gmail.com \
    --cc=hverkuil@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=m.felsch@pengutronix.de \
    --cc=m.tretter@pengutronix.de \
    --cc=mchehab@kernel.org \
    --cc=nicolas.dufresne@collabora.com \
    --cc=paulk@sys-base.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®