Sunshine v2025.118.151840
Self-hosted game stream host for Moonlight.
nvenc_base.h
Go to the documentation of this file.
1
5#pragma once
6
7#include "nvenc_colorspace.h"
8#include "nvenc_config.h"
10
11#include "src/logging.h"
12#include "src/video.h"
13
14#include <ffnvcodec/nvEncodeAPI.h>
15
19namespace nvenc {
20
25 class nvenc_base {
26 public:
30 explicit nvenc_base(NV_ENC_DEVICE_TYPE device_type);
31 virtual ~nvenc_base();
32
33 nvenc_base(const nvenc_base &) = delete;
35 operator=(const nvenc_base &) = delete;
36
45 bool
46 create_encoder(const nvenc_config &config, const video::config_t &client_config, const nvenc_colorspace_t &colorspace, NV_ENC_BUFFER_FORMAT buffer_format);
47
52 void
54
64 encode_frame(uint64_t frame_index, bool force_idr);
65
73 bool
74 invalidate_ref_frames(uint64_t first_frame, uint64_t last_frame);
75
76 protected:
82 virtual bool
84
91 virtual bool
93
99 virtual bool
100 synchronize_input_buffer() { return true; }
101
108 virtual bool
109 wait_for_async_event(uint32_t timeout_ms) { return false; }
110
111 bool
112 nvenc_failed(NVENCSTATUS status);
113
122 uint32_t
123 min_struct_version(uint32_t version, uint32_t v11_struct_version = 0, uint32_t v12_struct_version = 0);
124
125 const NV_ENC_DEVICE_TYPE device_type;
126
127 void *encoder = nullptr;
128
129 struct {
130 uint32_t width = 0;
131 uint32_t height = 0;
132 NV_ENC_BUFFER_FORMAT buffer_format = NV_ENC_BUFFER_FORMAT_UNDEFINED;
133 uint32_t ref_frames_in_dpb = 0;
134 bool rfi = false;
135 } encoder_params;
136
137 std::string last_nvenc_error_string;
138
139 // Derived classes set these variables
140 void *device = nullptr;
142 std::shared_ptr<NV_ENCODE_API_FUNCTION_LIST> nvenc;
144 NV_ENC_REGISTERED_PTR registered_input_buffer = nullptr;
146 void *async_event_handle = nullptr;
148
149 private:
150 NV_ENC_OUTPUT_PTR output_bitstream = nullptr;
151 uint32_t minimum_api_version = 0;
152
153 struct {
154 uint64_t last_encoded_frame_index = 0;
155 bool rfi_needs_confirmation = false;
156 std::pair<uint64_t, uint64_t> last_rfi_range;
157 logging::min_max_avg_periodic_logger<double> frame_size_logger = { debug, "NvEnc: encoded frame sizes in kB", "" };
158 } encoder_state;
159 };
160
161} // namespace nvenc
A helper class for tracking and logging numerical values across a period of time.
Definition logging.h:109
Abstract platform-agnostic base of standalone NVENC encoder. Derived classes perform platform-specifi...
Definition nvenc_base.h:25
bool invalidate_ref_frames(uint64_t first_frame, uint64_t last_frame)
Perform reference frame invalidation (RFI) procedure.
Definition nvenc_base.cpp:549
nvenc_encoded_frame encode_frame(uint64_t frame_index, bool force_idr)
Encode the next frame using platform-specific input surface.
Definition nvenc_base.cpp:464
NV_ENC_REGISTERED_PTR registered_input_buffer
Definition nvenc_base.h:144
bool create_encoder(const nvenc_config &config, const video::config_t &client_config, const nvenc_colorspace_t &colorspace, NV_ENC_BUFFER_FORMAT buffer_format)
Create the encoder.
Definition nvenc_base.cpp:97
virtual bool create_and_register_input_buffer()=0
Required. Used for creating outside-facing input surface, registering this surface with nvenc->nvEncR...
std::shared_ptr< NV_ENCODE_API_FUNCTION_LIST > nvenc
Definition nvenc_base.h:142
uint32_t min_struct_version(uint32_t version, uint32_t v11_struct_version=0, uint32_t v12_struct_version=0)
This function returns the corresponding struct version for the minimum API required by the codec.
Definition nvenc_base.cpp:641
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:109
void * async_event_handle
Definition nvenc_base.h:146
void destroy_encoder()
Destroy the encoder. Derived classes classes call it in the destructor.
Definition nvenc_base.cpp:432
void * device
Definition nvenc_base.h:140
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:88
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:100
Declarations for logging related functions.
Standalone NVENC encoder.
Definition nvenc_base.cpp:86
Declarations for NVENC YUV colorspace.
Declarations for NVENC encoder configuration.
Declarations for NVENC encoded frame.
YUV colorspace and color range.
Definition nvenc_colorspace.h:14
NVENC encoder configuration.
Definition nvenc_config.h:18
Encoded frame.
Definition nvenc_encoded_frame.h:15
Definition video.h:21
Declarations for video.