Skip to main content

Encoder

Struct Encoder 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn config(&self) -> &Config

Returns the configuration in effect, including a bitrate changed since opening and the render node picked when none was named.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.