Skip to main content

Encoder

Struct Encoder 

Source
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

Source

pub fn new(config: Config) -> Result<Self>

Opens Config::device, or the first render node that encodes Config::codec, and sets up an encode session for config.

§Errors

Fails when the frame size is zero or odd, 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.

Source

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.

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 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.

Source

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.

Source

pub fn rate_control(&self) -> RateControl

Returns how the bitrate is spent, picked at open from what the driver offers.

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§

§

impl !Freeze for Encoder

§

impl !RefUnwindSafe for Encoder

§

impl !Send for Encoder

§

impl !Sync for Encoder

§

impl Forget for Encoder

§

impl Move for Encoder

§

impl Unpin for Encoder
where NonZero<u32>: Unpin, *const u8 is !null: Unpin, Arc<Sps>: Unpin, Arc<Pps>: Unpin, Arc<Display>: Unpin, InternalBitFlags: Unpin, Arc<FreeList>: Unpin, Rc<Context>: Unpin,

§

impl UnsafeUnpin for Encoder
where NonZeroU32Inner: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, *const ArcInner<Sps> is !null: UnsafeUnpin, PhantomData<ArcInner<Sps>>: UnsafeUnpin, *const ArcInner<Pps> is !null: UnsafeUnpin, PhantomData<ArcInner<Pps>>: UnsafeUnpin, *const ArcInner<Display> is !null: UnsafeUnpin, PhantomData<ArcInner<Display>>: UnsafeUnpin, PhantomData<Surface<()>>: UnsafeUnpin, PhantomData<((u32, u32), SurfacePool)>: UnsafeUnpin, PhantomData<((u32, u32), Rc<Context>)>: UnsafeUnpin, *const RcInner<Context> is !null: UnsafeUnpin, PhantomData<RcInner<Context>>: UnsafeUnpin,

§

impl UnwindSafe for Encoder
where NonZero<u32>: UnwindSafe, RawVecInner: UnwindSafe, Arc<Sps>: UnwindSafe, Arc<Pps>: UnwindSafe, Arc<Display>: UnwindSafe, HashMap<(u32, u32), SurfacePool>: UnwindSafe, HashMap<(u32, u32), Rc<Context>>: UnwindSafe, Rc<Context>: UnwindSafe,

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more