pub struct Encoder { /* private fields */ }Expand description
A VA-API H.264 or H.265 encoder. Built once, fed NV12 frames, emits an Annex-B stream.
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 Config::codec at the frame size, and sets up an encode session for config.
§Errors
Fails when the frame size is zero or odd or outside the driver’s Limits, when libva cannot be loaded, when the named node cannot be opened
or does not encode the codec (a named node is never swapped for
another), when no node encodes it, and when the driver refuses the
session or, for H.265, requires a coding tool this encoder does not write
or takes no packed sequence header. H.264 also fails on a frame larger than
the highest level (6.2) allows.
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 (with the
parameter sets inline on each IDR). keyframe forces an IDR.
§Errors
Fails when nv12 is shorter than a frame at the encoder’s size, when the
driver has no NV12 image format, 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 like
encode_nv12.
An NV12 buffer at the encoder’s size, which the encoder reads without
padding (whole macroblocks for H.264; for H.265, whole minimum coding
blocks at the driver’s surface alignment), 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 Config::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.
Sourcepub fn rate_control(&self) -> RateControl
pub fn rate_control(&self) -> RateControl
Returns how the bitrate is spent, picked at open from what the driver offers.