Skip to main content

VideoEncoder

Struct VideoEncoder 

Source
pub struct VideoEncoder { /* private fields */ }
Expand description

Stateful hardware encoder session for H.264 High or H.265 Main.

One encoder owns a private Vulkan Video encode session, per-slot NV12 input pictures, and the RGBA→NV12 conversion on the selected Engine’s queues. Frames enter as packed RGBA8 bytes sized exactly visible_extent().width * visible_extent().height * 4; conversion, GOP cadence, rate control, DPB management, and Annex-B framing stay private. Every emitted EncodedVideoPacket is one access unit; keyframe packets carry the session’s cached SPS/PPS (and VPS for H.265) parameter sets so each random-access point is self-describing.

encode waits for the submitted frame and returns its packet. flush waits for every outstanding encode job and drains the remainder — required before close when asynchronous submissions left frames in flight.

Implementations§

Source§

impl VideoEncoder

Source

pub fn new(engine: &Engine, settings: VideoEncodeConfig) -> Result<Self>

Create a session admitted by exact capability preflight.

Settings are validated against the engine’s queried encode capabilities; session creation then performs the Vulkan-level admission a second time. A device without an encode queue family for the requested codec profile fails here, not on the first encode call.

§Errors

Returns an error when settings fail preflight or the engine’s device does not admit a hardware encode session for the profile.

Source

pub fn encode( &mut self, rgba: &[u8], presentation_timestamp_micros: u64, ) -> Result<EncodedVideoPacket>

Encode one packed RGBA8 frame and return its access unit.

rgba must contain exactly visible_extent().width * visible_extent().height * 4 bytes in row-major RGBA8 order. The frame is converted to the session’s coded NV12 extent, queued on the hardware encode queue, and awaited before the packet is returned; presentation timestamps are in microseconds.

§Errors

Returns an error after close, for a mismatched input size, or when conversion, encode submission, feedback, or bitstream readback fails.

Source

pub fn encode_rgba16( &mut self, rgba: &[u16], presentation_timestamp_micros: u64, ) -> Result<EncodedVideoPacket>

Encode one packed RGBA64 frame and return its access unit.

rgba must contain exactly visible_extent().width * visible_extent().height * 4 little-endian 16-bit samples in row-major RGBA order; the session must carry a wide-component profile such as H.265 Main10. An 8-bit session rejects the input through its length validation.

§Errors

Returns an error after close, for a mismatched input size, or when conversion, encode submission, feedback, or bitstream readback fails.

Source

pub fn flush(&mut self) -> Result<Vec<EncodedVideoPacket>>

Wait for every outstanding encode job and drain all remaining packets.

Synchronous encode leaves nothing pending; flush is the tail drain for asynchronous submissions and returns an empty list when the session is already drained.

§Errors

Returns an error after close or when an outstanding job fails feedback or bitstream readback.

Source

pub fn close(&mut self)

Close the encoder and release its session resources.

Unharvested asynchronous jobs are released without being drained; call flush first to keep every submitted frame.

Source

pub const fn is_open(&self) -> bool

Return whether the encoder still accepts frames.

Source

pub fn coded_extent(&self) -> Option<VideoExtent>

Return the aligned NV12 coded extent the driver encodes at.

Source

pub fn visible_extent(&self) -> Option<VideoExtent>

Return the visible input extent expected by encode.

Source

pub fn codec_headers(&self) -> Option<&[u8]>

Borrow the session’s cached codec parameter sets in Annex-B form.

H.264 sessions carry SPS+PPS and H.265 sessions carry VPS+SPS+PPS; the bytes are already prepended to every keyframe packet, so muxers that need out-of-band codec configuration should derive it from a keyframe or use this reference directly.

Source

pub fn record_frames(&mut self, frames: &[(&[u8], u64)]) -> Result<()>

Record RGBA8 frame encodes into an active execution capture.

Inside Engine::capture this opens a retained session, records each frame’s RGBA→native conversion on the compute family and its vkCmdEncodeVideoKHR picture on the encode family as execution-graph nodes, and restores this encoder’s live session afterwards. The resulting plan replays the same frames without host work; packets materialize from the plan’s completion Event through Event::encoded_packets.

Returns an error outside capture, after close, for an empty frame list, or when a frame’s length does not match visible_extent().

Source

pub fn record_planes(&mut self, frames: &[(&[Matrix], u64)]) -> Result<()>

Record one frame whose input planes are already engine matrices.

Each entry supplies the session’s native-format planes in order (Y then UV for NV12/P010) plus a presentation timestamp in microseconds. Inside Engine::capture, each frame’s buffer→image staging records on the compute family and its vkCmdEncodeVideoKHR picture on the encode family, so a decoded-and-transformed oa::Matrix plane can be re-encoded without a host round trip.

Returns an error outside capture, after close, for an empty frame list, or when a frame’s plane count or extent mismatches the session.

Trait Implementations§

Source§

impl Drop for VideoEncoder

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

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.