[PATCH 02/14] media: uapi: Add H.264 stateless encode support
Daniel Golle
daniel at makrotopia.org
Tue Dec 1 06:25:40 PST 2026
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/
More information about the linux-arm-kernel
mailing list