Skip to main content

Module encode

Module encode 

Source
Expand description

Thin VA-API H.264 and H.265 encoder built on the vendored libva binding.

For H.264 it writes the SPS, PPS, and slice headers with kodecs-h264, and drives libva directly for surface upload + slice submission, rather than vendoring cros-codecs’s generic multi-backend encoder framework. The per-frame VA buffer population is ported from cros-codecs’s encoder/stateless/h264/vaapi.rs. H.265 writes its own parameter sets and slice headers (see h265.rs).

Low-latency only: IPPP (no B-frames), one reference frame, matching the VideoToolbox / Media Foundation / NVENC backends in moq-video.

Three ways in, cheapest first:

  • Encoder::encode_dmabuf with an NV12 DMA-BUF at the encoder’s size encodes the producer’s memory in place.
  • Encoder::encode_dmabuf with any other importable DMA-BUF (packed RGB from a screen capture, YUYV from a camera, NV12 at another size) converts and scales it into the encoder’s own surface with the video processor, on the GPU.
  • Encoder::encode_nv12 uploads CPU pixels.

Checked on Intel Meteor Lake and Arrow Lake with the iHD driver, and on an AMD RX 9070 with radeonsi, by decoding with ffmpeg’s software decoder: CPU and DMA-BUF input come back above 35 dB luma PSNR, and BGRX converts to BT.601 and BT.709 with every channel within 6 code values of the reference. The low-power entrypoint is untested, since those devices expose the full one.

Structs§

Config
Encoder configuration. Dimensions must be even (4:2:0 chroma).
Encoder
A VA-API H.264 or H.265 encoder. Built once, fed NV12 frames, emits an Annex-B stream.
Limits
The frame sizes a driver encodes a codec at, as probe reports them.

Enums§

RateControl
The rate control mode that holds a stream to Config::bitrate, picked at open for the driver.

Constants§

DEFAULT_MIN_QP
The default quantizer floor, the start of Config::qp.
HRD_FRAMES
The HRD buffer, in frames of the bitrate, sent with every picture. The driver’s own default is a second of the rate, which let a second of high motion run half again over the bitrate on radeonsi. It is also what a still scene banks: the first busy frames after one spend all of it, so that second runs this many frames over the bitrate.
MAX_QP
The coarsest quantizer H.264 and H.265 define, the default ceiling and the end of Config::qp.
RATE_CONTROL_WINDOW_MS
Window over which libva’s rate controller targets the configured bitrate.

Functions§

probe
Returns the frame sizes display’s driver encodes codec at, which is what Encoder::new checks of a render node.