pub struct Encoder { /* private fields */ }Expand description
A VA-API H.264 encoder. Built once, fed NV12 frames, emits Annex-B H.264.
Implementations§
Source§impl Encoder
impl Encoder
Sourcepub fn new(config: Config) -> Result<Self>
pub fn new(config: Config) -> Result<Self>
Opens Config::device, or the first render node that encodes H.264, and sets up an encode session for config.
§Errors
Fails when libva cannot be loaded, when the named node cannot be opened or does not encode H.264 (a named node is never swapped for another), when no node encodes H.264, and when the driver refuses the session.
Sourcepub fn encode_nv12(&mut self, nv12: &[u8], keyframe: bool) -> Result<Vec<u8>>
pub fn encode_nv12(&mut self, nv12: &[u8], keyframe: bool) -> Result<Vec<u8>>
Encodes one tightly-packed NV12 frame, returning Annex-B H.264 (with inline
SPS/PPS on each IDR). keyframe forces an IDR.
§Errors
Fails when nv12 is shorter than a frame at the encoder’s size, and when
the upload or the encode fails.
Sourcepub fn encode_dmabuf(
&mut self,
buffer: DmaBuf,
keyframe: bool,
) -> Result<Vec<u8>>
pub fn encode_dmabuf( &mut self, buffer: DmaBuf, keyframe: bool, ) -> Result<Vec<u8>>
Encodes one frame held in a DMA-BUF, returning Annex-B H.264 like
encode_nv12.
An NV12 buffer at the encoder’s size, whose size is a whole number of
macroblocks and whose color is the stream’s or
unknown, is imported and encoded in place. Any other mapped format or
size goes through the video processor into the encoder’s own surface
first: scaled to the encoder’s size and converted to Config::color.
A YUV buffer with no color of its own is taken to be in that space
already. Nothing reaches the CPU on either path.
The macroblock condition is not a formality: the encoder reads whole macroblocks, so it reads 1088 rows of a 1080-row picture, and a producer that places the chroma plane right after the last visible luma row (PipeWire does) has nothing there to read.
The producer must have finished writing the buffer; this does not wait on its fence.
§Errors
Fails when the driver refuses to import the buffer (an unsupported
modifier or layout), when the device has no video processor and the
buffer needs one, and when the encode itself fails. The encoder stays
usable after a refused import, so a caller can fall back to
encode_nv12 for the same frame.
Sourcepub fn set_bitrate(&mut self, bitrate: u32) -> Result<()>
pub fn set_bitrate(&mut self, bitrate: u32) -> Result<()>
Changes the target bitrate, in bits per second, from the next frame on.
The rate control parameters go out with every frame, so this neither
forces an IDR nor rebuilds the session; the next frame also asks the
driver to reset its rate controller. It then converges over its window,
RATE_CONTROL_WINDOW_MS.
The level in the SPS is not revisited, so a rate far above the one the stream opened with can exceed what that level allows.
§Errors
Fails for a bitrate of zero, and when a larger coded buffer for the new rate cannot be allocated, in which case the old rate stays.