Sunshine0.0.5917
Sunshine 0.0.5917
Self-hosted game stream host for Moonlight.
Loading...
Searching...
No Matches
nvenc_base.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <variant>
9
10// local includes
11#include "nvenc_colorspace.h"
12#include "nvenc_config.h"
13#include "nvenc_encoded_frame.h"
14#include "nvenc_encoder.h"
15#include "nvenc_sdk.h"
16#include "src/logging.h"
17#include "src/video.h"
19
20struct ID3D11Device;
21
25namespace NVENC_NAMESPACE {
26
30 struct encoder_session_t;
34 struct completion_event_t;
35
40 // Virtual inheritance is required because platform implementations also inherit their SDK-neutral interface.
41 class nvenc_base: public virtual ::nvenc::nvenc_encoder { // NOSONAR(cpp:S1011)
42 public:
46 explicit nvenc_base(NV_ENC_DEVICE_TYPE device_type);
50 ~nvenc_base() override;
51
52 nvenc_base(const nvenc_base &) = delete;
53 nvenc_base &operator=(const nvenc_base &) = delete;
54
63 bool create_encoder(
64 const ::nvenc::nvenc_config &config,
65 const video::config_t &client_config,
66 const video::sunshine_colorspace_t &colorspace,
67 platf::pix_fmt_e buffer_format
68 ) override;
69
74 void destroy_encoder() override;
75
84 ::nvenc::nvenc_encoded_frame encode_frame(uint64_t frame_index, bool force_idr) override;
85
93 bool invalidate_ref_frames(uint64_t first_frame, uint64_t last_frame) override;
94
95 protected:
101 virtual bool init_library() = 0;
102
110
117 return true;
118 }
119
126 virtual bool wait_for_async_event(uint32_t timeout_ms) {
127 return false;
128 }
129
136 bool nvenc_failed(NVENCSTATUS status);
137
138 const NV_ENC_DEVICE_TYPE device_type;
139
140 encoder_session_t *encoder = nullptr;
141
142 struct {
143 uint32_t width = 0;
144 uint32_t height = 0;
145 NV_ENC_BUFFER_FORMAT buffer_format = NV_ENC_BUFFER_FORMAT_UNDEFINED;
146 uint32_t ref_frames_in_dpb = 0;
147 bool rfi = false;
148 } encoder_params;
149
151
152 // Derived classes set these variables
153 std::variant<ID3D11Device *, CUctx_st *> device {static_cast<ID3D11Device *>(nullptr)};
154 std::shared_ptr<NV_ENCODE_API_FUNCTION_LIST> nvenc;
156 NV_ENC_REGISTERED_PTR registered_input_buffer = nullptr;
158 completion_event_t *async_event_handle = nullptr;
160
161 private:
169 int get_encoder_cap(const GUID &encode_guid, NV_ENC_CAPS cap) const;
170
178 bool validate_encoder_capabilities(const GUID &encode_guid, NV_ENC_BUFFER_FORMAT buffer_format);
179
187 void configure_split_frame(
188 NV_ENC_INITIALIZE_PARAMS &init_params,
189 const ::nvenc::nvenc_config &config,
190 const video::config_t &client_config
191 ) const;
192
201 void configure_rate_control(
202 NV_ENC_CONFIG &enc_config,
203 const ::nvenc::nvenc_config &config,
204 const video::config_t &client_config,
205 const GUID &encode_guid
206 );
207
217 void configure_reference_frames(
218 std::uint32_t &ref_frames_option,
219 NV_ENC_NUM_REF_FRAMES &list0_option,
220 std::uint32_t default_count,
221 int requested_count,
222 const GUID &encode_guid
223 );
224
235 template<typename FormatConfig>
236 void configure_h264_hevc_metadata(
237 FormatConfig &format_config,
238 const video::config_t &client_config,
239 const nvenc_colorspace_t &colorspace,
240 NV_ENC_BUFFER_FORMAT buffer_format,
241 const GUID &encode_guid
242 );
243
254 void configure_h264(
255 NV_ENC_CONFIG &enc_config,
256 const ::nvenc::nvenc_config &config,
257 const video::config_t &client_config,
258 const nvenc_colorspace_t &colorspace,
259 NV_ENC_BUFFER_FORMAT buffer_format,
260 const GUID &encode_guid
261 );
262
273 void configure_hevc(
274 NV_ENC_CONFIG &enc_config,
275 const ::nvenc::nvenc_config &config,
276 const video::config_t &client_config,
277 const nvenc_colorspace_t &colorspace,
278 NV_ENC_BUFFER_FORMAT buffer_format,
279 const GUID &encode_guid
280 );
281
282#if NVENC_SDK_VERSION >= 1200
293 void configure_av1(
294 NV_ENC_CONFIG &enc_config,
295 const ::nvenc::nvenc_config &config,
296 const video::config_t &client_config,
297 const nvenc_colorspace_t &colorspace,
298 NV_ENC_BUFFER_FORMAT buffer_format,
299 const GUID &encode_guid
300 );
301#endif
302
313 void configure_codec(
314 NV_ENC_CONFIG &enc_config,
315 const ::nvenc::nvenc_config &config,
316 const video::config_t &client_config,
317 const nvenc_colorspace_t &colorspace,
318 NV_ENC_BUFFER_FORMAT buffer_format,
319 const GUID &encode_guid
320 );
321
328 bool initialize_encoder_resources(NV_ENC_INITIALIZE_PARAMS &init_params);
329
339 void log_created_encoder(
340 const NV_ENC_INITIALIZE_PARAMS &init_params,
341 const NV_ENC_CONFIG &enc_config,
342 const ::nvenc::nvenc_config &config,
343 const video::config_t &client_config,
344 NV_ENC_BUFFER_FORMAT buffer_format
345 ) const;
346
347 NV_ENC_OUTPUT_PTR output_bitstream = nullptr;
348
349 struct {
350 uint64_t last_encoded_frame_index = 0;
351 bool rfi_needs_confirmation = false;
352 std::pair<uint64_t, uint64_t> last_rfi_range;
353 logging::min_max_avg_periodic_logger<double> frame_size_logger = {debug, "NvEnc: encoded frame sizes in kB", ""};
354 } encoder_state;
355 };
356
357} // namespace NVENC_NAMESPACE
A helper class for tracking and logging numerical values across a period of time.
Definition logging.h:163
std::string last_nvenc_error_string
Last NVENC error string.
Definition nvenc_base.h:150
bool invalidate_ref_frames(uint64_t first_frame, uint64_t last_frame) override
Perform reference frame invalidation (RFI) procedure.
Definition nvenc_base.cpp:799
bool create_encoder(const ::nvenc::nvenc_config &config, const video::config_t &client_config, const video::sunshine_colorspace_t &colorspace, platf::pix_fmt_e buffer_format) override
Create the encoder.
Definition nvenc_base.cpp:563
const NV_ENC_DEVICE_TYPE device_type
NVENC device backend used by this encoder instance.
Definition nvenc_base.h:138
NV_ENC_REGISTERED_PTR registered_input_buffer
Definition nvenc_base.h:156
encoder_session_t * encoder
Opaque NVENC encoder session handle returned by the driver.
Definition nvenc_base.h:140
completion_event_t * async_event_handle
Definition nvenc_base.h:158
virtual bool create_and_register_input_buffer()=0
Required. Used for creating outside-facing input surface, registering this surface with nvenc->nvEncR...
void destroy_encoder() override
Destroy the encoder. Derived classes classes call it in the destructor.
Definition nvenc_base.cpp:684
std::variant< ID3D11Device *, CUctx_st * > device
Native D3D11 device or CUDA context, set by the derived backend.
Definition nvenc_base.h:153
::nvenc::nvenc_encoded_frame encode_frame(uint64_t frame_index, bool force_idr) override
Encode the next frame using platform-specific input surface.
Definition nvenc_base.cpp:715
virtual bool wait_for_async_event(uint32_t timeout_ms)
Optional. Override if you want to create encoder in async mode. In this case must also set async_even...
Definition nvenc_base.h:126
~nvenc_base() override
Destroy the encoder wrapper; callers release the encoder session with destroy_encoder().
virtual bool init_library()=0
Required. Used for loading NvEnc library and setting nvenc variable with NvEncodeAPICreateInstance()....
nvenc_base(NV_ENC_DEVICE_TYPE device_type)
Definition nvenc_base.cpp:187
virtual bool synchronize_input_buffer()
Optional. Override if you must perform additional operations on the registered input surface in the b...
Definition nvenc_base.h:116
SDK-neutral standalone NVENC encoder interface.
Definition nvenc_encoder.h:28
pix_fmt_e
Enumerates supported pix fmt options.
Definition common.h:342
bl::sources::severity_logger< int > debug
Follow what is happening.
Definition logging.cpp:43
Declarations for logging related functions.
Standalone NVENC encoder.
Definition nvenc_base.cpp:185
Declarations for NVENC YUV colorspace.
Declarations for NVENC encoder configuration.
Declarations for NVENC encoded frame.
Declarations for the SDK-neutral NVENC encoder interface.
Imports one NVENC SDK into its version-specific implementation namespace.
#define NVENC_NAMESPACE
Namespace used for the documented NVENC implementation.
Definition nvenc_sdk.h:12
YUV colorspace and color range.
Definition nvenc_colorspace.h:15
Encoded NVENC output frame and metadata needed by the packetizer.
Definition nvenc_encoded_frame.h:16
Encoding configuration requested by a remote client.
Definition video.h:33
Sunshine colorimetry values derived from stream configuration.
Definition video_colorspace.h:36
Declarations for video.
Declarations for colorspace functions.