pub struct Encoder { /* private fields */ }
Expand description

Entrypoint for the Encoder API.

The general usage follows these steps:

  1. Initialize the encoder.
  2. Set up the desired encoding parameters.
  3. Allocate or register input and output buffers.
  4. Copy frames to input buffers, encode, and read out of output bitstream.
  5. Close the encoding session and clean up.

With this wrapper cleanup is performed automatically. To do the other steps this struct provides associated functions such as Encoder::get_encode_guids or Encoder::get_supported_input_formats.

Once the configuration is completed, a session should be initialized with Encoder::start_session to get a Session. This type has further function to create input and output buffers and encode pictures.

See NVIDIA Video Codec SDK - Video Encoder API Programming Guide.

Implementations§

source§

impl Encoder

source

pub fn initialize_with_cuda( cuda_device: Arc<CudaDevice> ) -> Result<Self, EncodeError>

Create an Encoder with CUDA as the encode device.

See NVIDIA docs.

Errors

Could error if there was no encode capable device detected or if the encode device was invalid.

Examples
let cuda_device = CudaDevice::new(0).unwrap();
let encoder = Encoder::initialize_with_cuda(cuda_device).unwrap();
source

pub fn get_encode_guids(&self) -> Result<Vec<GUID>, EncodeError>

Get the encode GUIDs which the encoder supports.

You should use this function to check whether your machine supports the video compression standard that you with to use.

See NVIDIA docs.

Errors

Could error if we run out of memory.

Examples
let encoder = Encoder::initialize_with_cuda(cuda_device).unwrap();
let encode_guids = encoder.get_encode_guids().unwrap();
// Confirm that this machine support encoding to H.264.
assert!(encode_guids.contains(&NV_ENC_CODEC_H264_GUID));
source

pub fn get_preset_guids( &self, encode_guid: GUID ) -> Result<Vec<GUID>, EncodeError>

Get the encode preset GUIDs which the encoder supports for the given codec GUID.

You should use this function to check whether your machine supports the encode preset that you wish to use.

See NVIDIA docs.

Errors

Could error if the encode GUID is invalid or we run out of memory.

Examples
let encoder = Encoder::initialize_with_cuda(cuda_device).unwrap();

//* Check if H.264 encoding is supported. *//

let preset_guids = encoder.get_preset_guids(NV_ENC_CODEC_H264_GUID).unwrap();
// Confirm that H.264 supports the P1 preset (high performance, low quality) on this machine.
assert!(preset_guids.contains(&NV_ENC_PRESET_P1_GUID));
source

pub fn get_profile_guids( &self, encode_guid: GUID ) -> Result<Vec<GUID>, EncodeError>

Get the encode profile GUIDs which the encoder supports for the given codec GUID.

You should use this function to check whether your machine supports the encode profile that you wish to use.

See NVIDIA docs.

Errors

Could error if the encode GUID is invalid or we run out of memory.

Examples
let encoder = Encoder::initialize_with_cuda(cuda_device).unwrap();

//* Check if H.264 encoding is supported. *//

let profile_guids = encoder.get_profile_guids(NV_ENC_CODEC_H264_GUID).unwrap();
// Confirm that H.264 supports the HIGH profile on this machine.
assert!(profile_guids.contains(&NV_ENC_H264_PROFILE_HIGH_GUID));
source

pub fn get_supported_input_formats( &self, encode_guid: GUID ) -> Result<Vec<NV_ENC_BUFFER_FORMAT>, EncodeError>

Get the buffer formats which the encoder supports for the given codec GUID.

You should use this function to check whether your machine supports the buffer format that you wish to use.

See NVIDIA docs.

Errors

Could error if the encode GUID is invalid or we run out of memory.

Examples
let encoder = Encoder::initialize_with_cuda(cuda_device).unwrap();

//* Check if H.264 encoding is supported. *//

let input_guids = encoder
    .get_supported_input_formats(NV_ENC_CODEC_H264_GUID)
    .unwrap();
// Confirm that H.264 supports the `ARGB10` format on this machine.
assert!(input_guids.contains(&NV_ENC_BUFFER_FORMAT::NV_ENC_BUFFER_FORMAT_ARGB10));
source

pub fn get_preset_config( &self, encode_guid: GUID, preset_guid: GUID, tuning_info: NV_ENC_TUNING_INFO ) -> Result<NV_ENC_PRESET_CONFIG, EncodeError>

Get the preset config struct from the given codec GUID, preset GUID, and tuning info.

You should use this function to generate a preset config for the encoder session if you want to modify the preset further.

See NVIDIA docs

Errors

Could error if encode_guid or preset_guid is invalid, if tuning_info is set to NV_ENC_TUNING_INFO::NV_ENC_TUNING_INFO_UNDEFINED or NV_ENC_TUNING_INFO::NV_ENC_TUNING_INFO_COUNT, or if we run out of memory.

Examples
let encoder = Encoder::initialize_with_cuda(cuda_device).unwrap();

//* Check if H.264 encoding and the P1 preset (highest performance) are supported. *//

// Create the preset config.
let _preset_config = encoder
    .get_preset_config(
        NV_ENC_CODEC_H264_GUID,
        NV_ENC_PRESET_P1_GUID,
        NV_ENC_TUNING_INFO::NV_ENC_TUNING_INFO_ULTRA_LOW_LATENCY,
    )
    .unwrap();
source

pub fn start_session( self, buffer_format: NV_ENC_BUFFER_FORMAT, initialize_params: NV_ENC_INITIALIZE_PARAMS ) -> Result<Session, EncodeError>

Initialize an encoder session with the given configuration.

You must do this before you can encode a picture. You should use the NV_ENC_INITIALIZE_PARAMS builder via NV_ENC_INITIALIZE_PARAMS::new.

See NVIDIA docs.

Errors

Could error if the initialize_params are invalid or if we run out of memory.

Examples
let encoder = Encoder::initialize_with_cuda(cuda_device).unwrap();

//* Check if `NV_ENC_CODEC_H264_GUID` is supported. *//

// Initialize the encoder session.
let _session = encoder
    .start_session(
        NV_ENC_BUFFER_FORMAT_ARGB,
        NV_ENC_INITIALIZE_PARAMS::new(NV_ENC_CODEC_H264_GUID, 1920, 1080),
    )
    .unwrap();

Trait Implementations§

source§

impl Debug for Encoder

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Encoder

The client must flush the encoder before freeing any resources. Do this by sending an EOS encode frame. (This is also done automatically when Session is dropped.).

Sending an EOS frame might still generate data, so if you care about this you should send an EOS frame yourself.

The client must free all the input and output resources before destroying the encoder. If using events, they must also be unregistered.

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.