Devicetree
 help / color / mirror / Atom feed
From: Daniel Golle <daniel@makrotopia.org>
To: Nicolas Dufresne <nicolas.dufresne@collabora.com>,
	Paul Kocialkowski <paulk@sys-base.io>
Cc: devicetree@vger.kernel.org, imx@lists.linux.dev,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, linux-media@vger.kernel.org,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Shawn Guo <shawnguo@kernel.org>,
	Sascha Hauer <s.hauer@pengutronix.de>,
	Pengutronix Kernel Team <kernel@pengutronix.de>,
	Benjamin Gaignard <benjamin.gaignard@collabora.com>,
	Philipp Zabel <p.zabel@pengutronix.de>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Hans Verkuil <hverkuil@kernel.org>,
	Marco Felsch <m.felsch@pengutronix.de>,
	Michael Tretter <m.tretter@pengutronix.de>,
	Detlev Casanova <detlev.casanova@collabora.com>
Subject: Re: [PATCH 02/14] media: uapi: Add H.264 stateless encode support
Date: Tue, 1 Dec 2026 14:25:40 +0000	[thread overview]
Message-ID: <aw7Y5HKZhk6F7iKy@makrotopia.org> (raw)
In-Reply-To: <1a1feaf854e6e1c34af468b7501988f2cf68ebca.camel@collabora.com>

Hi Nicolas, Paul,

Some input from a second encoder of a different vendor, as Paul asked
for in the follow-ups thread.

Two weeks ago I posted an RFC for the Rockchip RKVENC v1
encoder of the RV1126 ("vepu541") and RK3566/RK3568 ("vepu540") [1].
It is stateful for now, but its hardware back end takes a per-picture
description without V4L2 types, so I went through this uAPI and the
review with that hardware in mind. It is Rockchip's own IP, not a
Hantro/Verisilicon derivative.

What matters about the block here:

- The driver writes SPS and PPS (and VPS for HEVC). The hardware
  writes the slice header and the slice data from registers. The
  H.264 slice header registers take nal_ref_idc, nal_unit_type,
  slice_type, pic_parameter_set_id, frame_num, idr_pic_id,
  pic_order_cnt_lsb, cabac_init_idc, disable_deblocking_filter_idc
  and the alpha/beta offsets, plus copies of the SPS/PPS fields the
  slice header syntax depends on (log2_max_frame_num_minus4,
  log2_max_pic_order_cnt_lsb_minus4, entropy_coding_mode_flag,
  transform_8x8_mode_flag, pic_init_qp, the chroma QP offsets,
  deblocking_filter_control_present_flag). For this class of hardware
  the slice header fields are register inputs to the driver, never
  bitstream the kernel writes itself.
- It writes pic_order_cnt_type 0 slice headers (pic_order_cnt_lsb is a
  register field), so unlike the H1/VEPU1/VEPU2 case Arun described it
  does not need POC type 2.
- One reconstructed picture is written and one reference is read per
  picture. My driver alternates two slots with sliding window marking,
  which is what this uAPI assumes today. I have not investigated
  long-term references on this block.
- My driver codes one slice per picture. The block has slice split
  registers that I have not characterised. If the hardware splits
  slices by itself, first_mb_in_slice and the header fields of every
  slice after the first come from the hardware, so a per-slice array
  in the uAPI would have to allow "one entry, hardware decides".
- The bitstream buffer has a start offset register, so the CPU can
  write the parameter sets at the head of the CAPTURE buffer and the
  hardware appends behind them. On 32-bit SoCs that matters: only the
  CAPTURE buffer needs a kernel mapping, OUTPUT frames are imported
  with DMA_ATTR_NO_KERNEL_MAPPING, and 4K streams fit the vmalloc
  space.
- A bitstream buffer overflow raises its own interrupt and the picture
  can be encoded again.

On your review points:

> > +	__u8 nal_ref_idc;
>
> This is fixed in this implementation, and will be derivable with
> support for other reference model. I'd remove.

Fine for RKVENC v1, as long as the core hands the derived value to the
driver: it is a register field here.

> > +	__u8 slice_type;
>
> I think we should do like Vulkan here though. They split the frame
> invariant (StdVideoEncodeH264PictureInfo) from the slice parameters
> (StdVideoEncodeH264SliceHeader).

The split works for this hardware too. It has a single set of slice
header registers, so its driver would constrain the slice array to
one entry through the state constrain callback of the core, or to
"hardware split" once the slice split registers are understood.

> > +	__u8 colour_plane_id;
>
> I'm not aware of any HW capable of != 0 here, so I'd be keen on
> dropping.

RKVENC v1 has no separate colour plane coding either.

One constraint for slice_qp_delta: for HEVC the slice QP in the header
register has to equal the QP the picture is quantised with, so the
core must not report a slice QP that differs from the QP it hands to
the driver. Arun already noted that the encode params are published
before rate control sets slice_qp_delta; with this hardware that
mismatch would be visible in the bitstream.

Resolution-dependent restrictions, which the state constrain callback
or format negotiation has to be able to express:

- H.264 P pictures are coded wrongly unless the picture is at least 3
  macroblocks wide and 2 high (1x1 works); the driver codes those
  sizes as IDR only.
- HEVC P pictures of a single 64-pixel CTU row decode wrongly.
- H.264 pictures at most 2 macroblocks wide and more than 1 high hang
  the encoder and have to be refused.

Rate control. Nicolas wrote in the VEPU510 thread that the in-kernel
rate control is the part to share between interfaces. The RKVENC v1
driver has frame-level CBR in the kernel with the hardware row rate
control off. It has run in RV1126 IP cameras streaming RTSP since
October, and these are the lessons from that:

1. I and P pictures need one QP state. With separate I and P QP
   histories a static camera scene showed a once-per-second pulse: the
   P QP settled at 24-26 while the I QP stayed at 33-37. Deriving the
   I QP from the mean P QP of the previous GOP minus a fixed delta (4
   for HEVC, 2 for H.264, the defaults of Rockchip's MPP library)
   removed it. Arun named the separate I and P QP histories as one
   cause of his VEPU2 results too.
2. A per-picture P QP step of 4 turned into a 32/36 limit cycle on
   skip-heavy pictures. A step of 1 with an exponentially smoothed P
   complexity removed it.
3. The I/P complexity ratio of static scenes is large: 37 measured at
   720p. A clamp of 16 starved the I pictures; 64 works.
4. A bitstream overflow is a normal event, not a fault. CAPTURE
   buffers are sized well below the worst case (half the pixel count,
   for the vmalloc reason above), and a camera starting in the dark
   drives the QP down until the first bright picture overflows. The
   rate control needs the buffer size and has to re-encode the picture
   coarser.
5. Error reporting belongs in the spec. When an encode fails, the
   CAPTURE buffer comes back in the ERROR state with a payload of 0.
   GStreamer 1.28's V4L2 buffer pool treats an empty CAPTURE buffer
   from an M2M device as the legacy drain marker, ends the stream and
   posts no error, so the pipeline stops silently. The stateless
   encoder spec should state how a failed encode is reported and that
   only V4L2_BUF_FLAG_LAST marks the end of a drain.

If any of this would be easier to discuss against code, the back end
of [1] is in rkvenc-vepu541*.c and the rate control in rkvenc-rc.c.


Cheers,


Daniel


[1] https://lore.kernel.org/all/cover.1794666330.git.daniel@makrotopia.org/


  reply	other threads:[~2026-12-01 14:27 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-22 10:16 [PATCH 00/14] media: Add V4L2 H.264 stateless encode and VC8000E support Paul Kocialkowski
2026-05-22 10:16 ` [PATCH 01/14] media: h264: Add a more generic reflist builder init Paul Kocialkowski
2026-11-17 21:06   ` Nicolas Dufresne
2026-05-22 10:16 ` [PATCH 02/14] media: uapi: Add H.264 stateless encode support Paul Kocialkowski
2026-11-17 21:30   ` Nicolas Dufresne
2026-12-01 14:25     ` Daniel Golle [this message]
2026-05-22 10:16 ` [PATCH 03/14] media: h264: Add SPS video definitions Paul Kocialkowski
2026-11-17 21:31   ` Nicolas Dufresne
2026-11-17 21:34   ` Nicolas Dufresne
2026-05-22 10:16 ` [PATCH 04/14] media: h264: Add stateless encode core Paul Kocialkowski
2026-11-17 21:37   ` Nicolas Dufresne
2026-05-22 10:16 ` [PATCH 05/14] media: h264: Add stateless encode rbsp Paul Kocialkowski
2026-05-22 10:16 ` [PATCH 06/14] media: h264: Add stateless encode reference management Paul Kocialkowski
2026-05-22 10:16 ` [PATCH 07/14] media: h264: Add stateless encode rate control Paul Kocialkowski
2026-05-22 10:16 ` [PATCH 08/14] media: verisilicon: Report default pixel coding for non-JPEG and fix JPEG case Paul Kocialkowski
2026-05-22 10:16 ` [PATCH 09/14] media: verisilicon: Cancel job with runtime pm put/clk disable on failure Paul Kocialkowski
2026-11-17 21:42   ` Nicolas Dufresne
2026-05-22 10:16 ` [PATCH 10/14] media: hantro: use hantro_decoded_buffer only for dst_vq Paul Kocialkowski
2026-11-17 21:48   ` Nicolas Dufresne
2026-05-22 10:16 ` [PATCH 11/14] media: verisilicon: Add common encoder parm and frameintervals ioctls Paul Kocialkowski
2026-11-17 21:50   ` Nicolas Dufresne
2026-05-22 10:16 ` [PATCH 12/14] media: verisilicon: Add support for the VC8000E H.264 encoder Paul Kocialkowski
2026-11-17 22:00   ` Nicolas Dufresne
2026-05-22 10:16 ` [PATCH 13/14] media: verilisicon: imx8m: Add support for the VC8000E on i.MX8MP Paul Kocialkowski
2026-05-22 10:16 ` [PATCH 14/14] arm64: dts: imx8mp: add VC8000E encoder node Paul Kocialkowski
2026-05-22 11:28   ` Fabio Estevam
2026-11-17 22:02   ` Nicolas Dufresne
2026-05-22 14:47 ` [PATCH 00/14] media: Add V4L2 H.264 stateless encode and VC8000E support Nicolas Dufresne
2026-09-23 12:10   ` Michael Nazzareno Trimarchi
2026-09-23 20:05     ` Paul Kocialkowski
2026-09-24  5:30       ` Michael Nazzareno Trimarchi
2026-09-24 18:09         ` Paul Kocialkowski
2026-09-24 19:38           ` Michael Nazzareno Trimarchi
2026-09-24 19:44             ` Nicolas Dufresne
2026-09-24 20:30               ` Paul Kocialkowski
2026-09-24 20:46                 ` Nicolas Dufresne
2026-09-30  4:14                   ` Chen-Yu Tsai
2026-12-01 14:33                 ` Daniel Golle

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=aw7Y5HKZhk6F7iKy@makrotopia.org \
    --to=daniel@makrotopia.org \
    --cc=benjamin.gaignard@collabora.com \
    --cc=conor+dt@kernel.org \
    --cc=detlev.casanova@collabora.com \
    --cc=devicetree@vger.kernel.org \
    --cc=hverkuil@kernel.org \
    --cc=imx@lists.linux.dev \
    --cc=kernel@pengutronix.de \
    --cc=krzk+dt@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.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=p.zabel@pengutronix.de \
    --cc=paulk@sys-base.io \
    --cc=robh@kernel.org \
    --cc=s.hauer@pengutronix.de \
    --cc=shawnguo@kernel.org \
    /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