Struct nvidia_video_codec_sdk::safe::Encoder
source · pub struct Encoder { /* private fields */ }
Expand description
Entrypoint for the Encoder API.
The general usage follows these steps:
- Initialize the encoder.
- Set up the desired encoding parameters.
- Allocate or register input and output buffers.
- Copy frames to input buffers, encode, and read out of output bitstream.
- 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
impl Encoder
sourcepub fn initialize_with_cuda(
cuda_device: Arc<CudaDevice>
) -> Result<Self, EncodeError>
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();
sourcepub fn get_encode_guids(&self) -> Result<Vec<GUID>, EncodeError>
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));
sourcepub fn get_preset_guids(
&self,
encode_guid: GUID
) -> Result<Vec<GUID>, EncodeError>
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));
sourcepub fn get_profile_guids(
&self,
encode_guid: GUID
) -> Result<Vec<GUID>, EncodeError>
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));
sourcepub fn get_supported_input_formats(
&self,
encode_guid: GUID
) -> Result<Vec<NV_ENC_BUFFER_FORMAT>, EncodeError>
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));
sourcepub fn get_preset_config(
&self,
encode_guid: GUID,
preset_guid: GUID,
tuning_info: NV_ENC_TUNING_INFO
) -> Result<NV_ENC_PRESET_CONFIG, EncodeError>
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();
sourcepub fn start_session(
self,
buffer_format: NV_ENC_BUFFER_FORMAT,
initialize_params: NV_ENC_INITIALIZE_PARAMS
) -> Result<Session, EncodeError>
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 Drop for Encoder
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.