nvidia_video_codec_sdk/safe/
builders.rs

1//! Builders for large params struct from autogenerated bindings.
2//!
3//! The bindings contain many massive structs which are used as parameters to
4//! functions in the API. This module provides builders to simplify creating
5//! these struct. They each have a `new()` method which has the minimum required
6//! parameters for the struct. Other options
7
8use std::ffi::c_void;
9
10use crate::sys::nvEncodeAPI::{
11    GUID,
12    NV_ENC_BUFFER_FORMAT,
13    NV_ENC_CONFIG,
14    NV_ENC_INITIALIZE_PARAMS,
15    NV_ENC_INITIALIZE_PARAMS_VER,
16    NV_ENC_INPUT_RESOURCE_TYPE,
17    NV_ENC_PIC_FLAGS,
18    NV_ENC_PIC_PARAMS,
19    NV_ENC_PIC_PARAMS_VER,
20    NV_ENC_REGISTER_RESOURCE,
21    NV_ENC_REGISTER_RESOURCE_VER,
22};
23
24#[deprecated(note = "use the safe wrapper `EncoderInitParams`")]
25impl NV_ENC_INITIALIZE_PARAMS {
26    /// Builder for [`NV_ENC_INITIALIZE_PARAMS`].
27    #[must_use]
28    pub fn new(encode_guid: GUID, width: u32, height: u32) -> Self {
29        NV_ENC_INITIALIZE_PARAMS {
30            version: NV_ENC_INITIALIZE_PARAMS_VER,
31            encodeGUID: encode_guid,
32            encodeWidth: width,
33            encodeHeight: height,
34            ..Default::default()
35        }
36    }
37
38    /// Specifies the preset for encoding. If the preset GUID is set then
39    /// the preset configuration will be applied before any other parameter.
40    pub fn preset_guid(&mut self, preset_guid: GUID) -> &mut Self {
41        self.presetGUID = preset_guid;
42        self
43    }
44
45    /// Specifies the advanced codec specific structure. If client has sent a
46    /// valid codec config structure, it will override parameters set by the
47    /// [`NV_ENC_INITIALIZE_PARAMS::preset_guid`].
48    ///
49    /// The client can query the interface for codec-specific parameters
50    /// using [`Encoder::get_preset_config`](super::encoder::Encoder::get_preset_config).
51    /// It can then modify (if required) some of the codec config parameters and
52    /// send down a custom config structure using this method. Even in this
53    /// case the client is recommended to pass the same preset GUID it has
54    /// used to get the config.
55    pub fn encode_config(&mut self, encode_config: &mut NV_ENC_CONFIG) -> &mut Self {
56        self.encodeConfig = encode_config;
57        self
58    }
59
60    /// Specifies the display aspect ratio (H264/HEVC) or the render
61    /// width/height (AV1).
62    pub fn display_aspect_ratio(&mut self, width: u32, height: u32) -> &mut Self {
63        self.darWidth = width;
64        self.darHeight = height;
65        self
66    }
67
68    /// Specifies the framerate in frames per second as a fraction
69    /// `numerator / denominator`.
70    pub fn framerate(&mut self, numerator: u32, denominator: u32) -> &mut Self {
71        self.frameRateNum = numerator;
72        self.frameRateDen = denominator;
73        self
74    }
75
76    /// Enable the Picture Type Decision to be taken by the
77    /// `NvEncodeAPI` interface.
78    pub fn enable_picture_type_decision(&mut self) -> &mut Self {
79        self.enablePTD = 1;
80        self
81    }
82
83    // TODO: Add other options
84}
85
86impl NV_ENC_PIC_PARAMS {
87    /// Create an EOS empty frame that is used at the
88    /// end of encoding to flush the encoder.
89    #[must_use]
90    pub fn end_of_stream() -> Self {
91        NV_ENC_PIC_PARAMS {
92            version: NV_ENC_PIC_PARAMS_VER,
93            encodePicFlags: NV_ENC_PIC_FLAGS::NV_ENC_PIC_FLAG_EOS as u32,
94            ..Default::default()
95        }
96    }
97}
98
99impl NV_ENC_REGISTER_RESOURCE {
100    /// Builder for [`NV_ENC_REGISTER_RESOURCE`].
101    ///
102    /// # Arguments
103    ///
104    /// * `resource_type` - Specifies the type of resource to be registered.
105    ///   Supported values are:
106    ///   - [`NV_ENC_INPUT_RESOURCE_TYPE::NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX`],
107    ///   - [`NV_ENC_INPUT_RESOURCE_TYPE::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR`],
108    ///   - [`NV_ENC_INPUT_RESOURCE_TYPE::NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX`]
109    /// * `width` - Input frame width.
110    /// * `height` - Input frame height.
111    /// * `resource_to_register` - Handle to the resource that is being
112    ///   registered. In the case of
113    ///   [`NV_ENC_INPUT_RESOURCE_TYPE::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR`],
114    ///   this should be a `CUdeviceptr` which you can get from
115    ///   `cuExternalMemoryGetMappedBuffer`.
116    /// * `buffer_format` - Buffer format of resource to be registered.
117    #[must_use]
118    pub fn new(
119        resource_type: NV_ENC_INPUT_RESOURCE_TYPE,
120        width: u32,
121        height: u32,
122        resource_to_register: *mut c_void,
123        buffer_format: NV_ENC_BUFFER_FORMAT,
124    ) -> Self {
125        NV_ENC_REGISTER_RESOURCE {
126            version: NV_ENC_REGISTER_RESOURCE_VER,
127            resourceType: resource_type,
128            width,
129            height,
130            pitch: width,
131            resourceToRegister: resource_to_register,
132            registeredResource: std::ptr::null_mut(),
133            bufferFormat: buffer_format,
134            ..Default::default()
135        }
136    }
137
138    /// Set the input buffer pitch.
139    ///
140    /// - For [`NV_ENC_INPUT_RESOURCE_TYPE::NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX`]
141    ///   resources, set this to 0.
142    /// - For [`NV_ENC_INPUT_RESOURCE_TYPE::NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR`]
143    ///   resources, set this to the pitch as obtained from `cuMemAllocPitch()`,
144    ///   or to the width in **bytes** (if this resource was created by using
145    ///   `cuMemAlloc()`). This value must be a multiple of 4.
146    /// - For [`NV_ENC_INPUT_RESOURCE_TYPE::NV_ENC_INPUT_RESOURCE_TYPE_OPENGL_TEX`]
147    ///   resources, set this to the texture width multiplied by the number of
148    ///   components in the texture format.
149    #[must_use]
150    pub fn pitch(mut self, pitch: u32) -> Self {
151        self.pitch = pitch;
152        self
153    }
154
155    // TODO: Add other options
156}