Struct nvidia_video_codec_sdk::safe::Session
source · pub struct Session { /* private fields */ }
Expand description
An encoding session to create input/output buffers and encode frames.
You need to call Encoder::start_session
before you can
encode frames using the session. On drop, the session will automatically
send an empty EOS frame to flush the encoder.
Implementations§
source§impl Session
impl Session
Functions for creating input and output buffers.
sourcepub fn create_input_buffer(&self) -> Result<Buffer<'_>, EncodeError>
pub fn create_input_buffer(&self) -> Result<Buffer<'_>, EncodeError>
Create a Buffer
.
See NVIDIA docs.
Errors
Could error if the width
, height
, or buffer_format
is invalid,
or if we run out of memory.
Examples
//* Create encoder. *//
//* Set `encode_guid` and `buffer_format`, and check that H.264 encoding and the ARGB format are supported. *//
//* Begin encoder session. *//
// Create an input buffer.
let _input_buffer = session
.create_input_buffer()
.unwrap();
sourcepub fn create_output_bitstream(&self) -> Result<Bitstream<'_>, EncodeError>
pub fn create_output_bitstream(&self) -> Result<Bitstream<'_>, EncodeError>
Create a Bitstream
.
See NVIDIA docs.
Errors
Could error is we run out of memory.
Examples
//* Create encoder. *//
//* Set `encode_guid` and `buffer_format`, and check that H.264 encoding and the ARGB format are supported. *//
//* Begin encoder session. *//
// Create an output bitstream buffer.
let _output_bitstream = session
.create_output_bitstream()
.unwrap();
sourcepub fn register_cuda_resource(
&self,
pitch: u32,
mapped_buffer: MappedBuffer
) -> Result<RegisteredResource<'_, MappedBuffer>, EncodeError>
pub fn register_cuda_resource( &self, pitch: u32, mapped_buffer: MappedBuffer ) -> Result<RegisteredResource<'_, MappedBuffer>, EncodeError>
Create a RegisteredResource
from a [MappedBuffer
].
See Session::register_generic_resource
.
pitch
should be set to the value obtained from cuMemAllocPitch()
,
or to the width in bytes (if this resource was created by using
cuMemAlloc()
). This value must be a multiple of 4.
Errors
Could error if registration or mapping fails, if the resource is invalid, or if we run out of memory.
sourcepub fn register_generic_resource<T>(
&self,
marker: T,
resource_type: NV_ENC_INPUT_RESOURCE_TYPE,
resource_to_register: *mut c_void,
pitch: u32
) -> Result<RegisteredResource<'_, T>, EncodeError>
pub fn register_generic_resource<T>( &self, marker: T, resource_type: NV_ENC_INPUT_RESOURCE_TYPE, resource_to_register: *mut c_void, pitch: u32 ) -> Result<RegisteredResource<'_, T>, EncodeError>
Create a RegisteredResource
.
This function is generic in the marker. This is so that you can
optionally put a value on the RegisteredResource
to make sure that
value does not get dropped while the resource is registered. You should
prefer using specific functions for the resource you are registering,
such as Session::register_cuda_resource
, when they are available.
See NVIDIA docs.
Errors
Could error if registration or mapping fails, if the resource is invalid, or if we run out of memory.
source§impl Session
impl Session
sourcepub fn get_encoder(&self) -> &Encoder
pub fn get_encoder(&self) -> &Encoder
Get the encoder used for this session.
This might be useful if you want to use some of
the functions on Encoder
.
Examples
//* Create encoder. *//
//* Set `encode_guid` and check that H.264 encoding is supported. *//
let session = encoder
.start_session(
NV_ENC_BUFFER_FORMAT_ARGB,
NV_ENC_INITIALIZE_PARAMS::new(encode_guid, 1920, 1080),
)
.unwrap();
// We can still use the encoder like this:
let _input_formats = session
.get_encoder()
.get_supported_input_formats(encode_guid);
sourcepub fn encode_picture<I: EncoderInput, O: EncoderOutput>(
&self,
input_buffer: &mut I,
output_bitstream: &mut O,
params: EncodePictureParams
) -> Result<(), EncodeError>
pub fn encode_picture<I: EncoderInput, O: EncoderOutput>( &self, input_buffer: &mut I, output_bitstream: &mut O, params: EncodePictureParams ) -> Result<(), EncodeError>
Encode a frame.
See NVIDIA docs.
Errors
Could error if the encode picture parameters were invalid or otherwise incorrect, or if we run out memory.
There are two recoverable errors:
- If this returns an error with
ErrorKind::EncoderBusy
then you should retry after a few milliseconds. - If this returns an error with
ErrorKind::NeedMoreInput
, the client should not lock the output bitstream yet. They should continue encoding until this function returnsOk
, and then lock the bitstreams in the order in which they were originally used.
Panics
Panics if codec specific parameters are provided for a different codec than the one used in the session.
Examples
//* Create encoder. *//
//* Set `encode_guid` and `buffer_format`, and check that H.264 encoding and the ARGB format are supported. *//
// Begin encoder session.
let mut initialize_params = NV_ENC_INITIALIZE_PARAMS::new(encode_guid, WIDTH, HEIGHT);
initialize_params.display_aspect_ratio(16, 9)
.framerate(30, 1)
.enable_picture_type_decision();
let session = encoder.start_session(
buffer_format,
initialize_params,
).unwrap();
//* Create input and output buffers. *//
// Encode frame.
unsafe { input_buffer.lock().unwrap().write(&[0; DATA_LEN]) };
session
.encode_picture(
&mut input_buffer,
&mut output_bitstream,
// Optional picture parameters
EncodePictureParams {
input_timestamp: 42,
..Default::default()
}
)
.unwrap();
let _data = output_bitstream.lock().unwrap().data();
sourcepub fn end_of_stream(&self) -> Result<(), EncodeError>
pub fn end_of_stream(&self) -> Result<(), EncodeError>
Send an EOS notifications to flush the encoder.
This function is called automatically on drop, but if you wish to get the data after flushing, you should call this function yourself.
Errors
Could error if we run out of memory.
If this returns an error with
ErrorKind::EncoderBusy
then you
should retry after a few milliseconds.