Sunshine v2026.213.34323
Self-hosted game stream host for Moonlight.
video.h
Go to the documentation of this file.
1
5#pragma once
6
7// local includes
8#include "input.h"
9#include "platform/common.h"
10#include "thread_safe.h"
11#include "video_colorspace.h"
12
13extern "C" {
14#include <libavcodec/avcodec.h>
15#include <libswscale/swscale.h>
16}
17
18struct AVPacket;
19
20namespace video {
21
22 /* Encoding configuration requested by remote client */
23 struct config_t {
24 int width; // Video width in pixels
25 int height; // Video height in pixels
26 int framerate; // Requested framerate, used in individual frame bitrate budget calculation
27 int framerateX100; // Optional field for streaming at NTSC or similar rates e.g. 59.94 = 5994
28 int bitrate; // Video bitrate in kilobits (1000 bits) for requested framerate
29 int slicesPerFrame; // Number of slices per frame
30 int numRefFrames; // Max number of reference frames
31
32 /* Requested color range and SDR encoding colorspace, HDR encoding colorspace is always BT.2020+ST2084
33 Color range (encoderCscMode & 0x1) : 0 - limited, 1 - full
34 SDR encoding colorspace (encoderCscMode >> 1) : 0 - BT.601, 1 - BT.709, 2 - BT.2020 */
35 int encoderCscMode;
36
37 int videoFormat; // 0 - H.264, 1 - HEVC, 2 - AV1
38
39 /* Encoding color depth (bit depth): 0 - 8-bit, 1 - 10-bit
40 HDR encoding activates when color depth is higher than 8-bit and the display which is being captured is operating in HDR mode */
41 int dynamicRange;
42
43 int chromaSamplingType; // 0 - 4:2:0, 1 - 4:4:4
44
45 int enableIntraRefresh; // 0 - disabled, 1 - enabled
46 };
47
48 platf::mem_type_e map_base_dev_type(AVHWDeviceType type);
49 platf::pix_fmt_e map_pix_fmt(AVPixelFormat fmt);
50
51 void free_ctx(AVCodecContext *ctx);
52 void free_frame(AVFrame *frame);
53 void free_buffer(AVBufferRef *ref);
54
59 using img_event_t = std::shared_ptr<safe::event_t<std::shared_ptr<platf::img_t>>>;
60
62 virtual ~encoder_platform_formats_t() = default;
63 platf::mem_type_e dev_type;
64 platf::pix_fmt_e pix_fmt_8bit, pix_fmt_10bit;
65 platf::pix_fmt_e pix_fmt_yuv444_8bit, pix_fmt_yuv444_10bit;
66 };
67
69 using init_buffer_function_t = std::function<util::Either<avcodec_buffer_t, int>(platf::avcodec_encode_device_t *)>;
70
72 const AVHWDeviceType &avcodec_base_dev_type,
73 const AVHWDeviceType &avcodec_derived_dev_type,
74 const AVPixelFormat &avcodec_dev_pix_fmt,
75 const AVPixelFormat &avcodec_pix_fmt_8bit,
76 const AVPixelFormat &avcodec_pix_fmt_10bit,
77 const AVPixelFormat &avcodec_pix_fmt_yuv444_8bit,
78 const AVPixelFormat &avcodec_pix_fmt_yuv444_10bit,
79 const init_buffer_function_t &init_avcodec_hardware_input_buffer_function
80 ):
81 avcodec_base_dev_type {avcodec_base_dev_type},
82 avcodec_derived_dev_type {avcodec_derived_dev_type},
83 avcodec_dev_pix_fmt {avcodec_dev_pix_fmt},
84 avcodec_pix_fmt_8bit {avcodec_pix_fmt_8bit},
85 avcodec_pix_fmt_10bit {avcodec_pix_fmt_10bit},
86 avcodec_pix_fmt_yuv444_8bit {avcodec_pix_fmt_yuv444_8bit},
87 avcodec_pix_fmt_yuv444_10bit {avcodec_pix_fmt_yuv444_10bit},
88 init_avcodec_hardware_input_buffer {init_avcodec_hardware_input_buffer_function} {
89 dev_type = map_base_dev_type(avcodec_base_dev_type);
90 pix_fmt_8bit = map_pix_fmt(avcodec_pix_fmt_8bit);
91 pix_fmt_10bit = map_pix_fmt(avcodec_pix_fmt_10bit);
92 pix_fmt_yuv444_8bit = map_pix_fmt(avcodec_pix_fmt_yuv444_8bit);
93 pix_fmt_yuv444_10bit = map_pix_fmt(avcodec_pix_fmt_yuv444_10bit);
94 }
95
96 AVHWDeviceType avcodec_base_dev_type, avcodec_derived_dev_type;
97 AVPixelFormat avcodec_dev_pix_fmt;
98 AVPixelFormat avcodec_pix_fmt_8bit, avcodec_pix_fmt_10bit;
99 AVPixelFormat avcodec_pix_fmt_yuv444_8bit, avcodec_pix_fmt_yuv444_10bit;
100
101 init_buffer_function_t init_avcodec_hardware_input_buffer;
102 };
103
106 const platf::mem_type_e &dev_type,
107 const platf::pix_fmt_e &pix_fmt_8bit,
108 const platf::pix_fmt_e &pix_fmt_10bit,
109 const platf::pix_fmt_e &pix_fmt_yuv444_8bit,
110 const platf::pix_fmt_e &pix_fmt_yuv444_10bit
111 ) {
112 encoder_platform_formats_t::dev_type = dev_type;
113 encoder_platform_formats_t::pix_fmt_8bit = pix_fmt_8bit;
114 encoder_platform_formats_t::pix_fmt_10bit = pix_fmt_10bit;
115 encoder_platform_formats_t::pix_fmt_yuv444_8bit = pix_fmt_yuv444_8bit;
116 encoder_platform_formats_t::pix_fmt_yuv444_10bit = pix_fmt_yuv444_10bit;
117 }
118 };
119
120 struct encoder_t {
121 std::string_view name;
122
131
132 static std::string_view from_flag(flag_e flag) {
133#define _CONVERT(x) \
134 case flag_e::x: \
135 return std::string_view(#x)
136 switch (flag) {
137 _CONVERT(PASSED);
138 _CONVERT(REF_FRAMES_RESTRICT);
139 _CONVERT(DYNAMIC_RANGE);
140 _CONVERT(YUV444);
141 _CONVERT(VUI_PARAMETERS);
142 _CONVERT(MAX_FLAGS);
143 }
144#undef _CONVERT
145
146 return {"unknown"};
147 }
148
149 struct option_t {
150 KITTY_DEFAULT_CONSTR_MOVE(option_t)
151 option_t(const option_t &) = default;
152
153 std::string name;
154 std::variant<int, int *, std::optional<int> *, std::function<int()>, std::string, std::string *, std::function<const std::string(const config_t &)>> value;
155
156 option_t(std::string &&name, decltype(value) &&value):
157 name {std::move(name)},
158 value {std::move(value)} {
159 }
160 };
161
162 const std::unique_ptr<const encoder_platform_formats_t> platform_formats;
163
164 struct codec_t {
165 std::vector<option_t> common_options;
166 std::vector<option_t> sdr_options;
167 std::vector<option_t> hdr_options;
168 std::vector<option_t> sdr444_options;
169 std::vector<option_t> hdr444_options;
170 std::vector<option_t> fallback_options;
171
172 std::string name;
173 std::bitset<MAX_FLAGS> capabilities;
174
175 bool operator[](flag_e flag) const {
176 return capabilities[(std::size_t) flag];
177 }
178
179 std::bitset<MAX_FLAGS>::reference operator[](flag_e flag) {
180 return capabilities[(std::size_t) flag];
181 }
182 } av1, hevc, h264;
183
184 const codec_t &codec_from_config(const config_t &config) const {
185 switch (config.videoFormat) {
186 default:
187 BOOST_LOG(error) << "Unknown video format " << config.videoFormat << ", falling back to H.264";
188 // fallthrough
189 case 0:
190 return h264;
191 case 1:
192 return hevc;
193 case 2:
194 return av1;
195 }
196 }
197
198 uint32_t flags;
199 };
200
202 virtual ~encode_session_t() = default;
203
204 virtual int convert(platf::img_t &img) = 0;
205
206 virtual void request_idr_frame() = 0;
207
208 virtual void request_normal_frame() = 0;
209
210 virtual void invalidate_ref_frames(int64_t first_frame, int64_t last_frame) = 0;
211 };
212
213 // encoders
214 extern encoder_t software;
215
216#if !defined(__APPLE__)
217 extern encoder_t nvenc; // available for windows and linux
218#endif
219
220#ifdef _WIN32
221 extern encoder_t amdvce;
222 extern encoder_t quicksync;
223 extern encoder_t mediafoundation;
224#endif
225
226#if defined(__linux__) || defined(linux) || defined(__linux) || defined(__FreeBSD__)
227 extern encoder_t vaapi;
228#endif
229
230#ifdef __APPLE__
231 extern encoder_t videotoolbox;
232#endif
233
235 virtual ~packet_raw_t() = default;
236
237 virtual bool is_idr() = 0;
238
239 virtual int64_t frame_index() = 0;
240
241 virtual uint8_t *data() = 0;
242
243 virtual size_t data_size() = 0;
244
245 struct replace_t {
246 std::string_view old;
247 std::string_view _new;
248
249 KITTY_DEFAULT_CONSTR_MOVE(replace_t)
250
251 replace_t(std::string_view old, std::string_view _new) noexcept:
252 old {std::move(old)},
253 _new {std::move(_new)} {
254 }
255 };
256
257 std::vector<replace_t> *replacements = nullptr;
258 void *channel_data = nullptr;
259 bool after_ref_frame_invalidation = false;
260 std::optional<std::chrono::steady_clock::time_point> frame_timestamp;
261 };
262
265 av_packet = av_packet_alloc();
266 }
267
269 av_packet_free(&this->av_packet);
270 }
271
272 bool is_idr() override {
273 return av_packet->flags & AV_PKT_FLAG_KEY;
274 }
275
276 int64_t frame_index() override {
277 return av_packet->pts;
278 }
279
280 uint8_t *data() override {
281 return av_packet->data;
282 }
283
284 size_t data_size() override {
285 return av_packet->size;
286 }
287
288 AVPacket *av_packet;
289 };
290
292 packet_raw_generic(std::vector<uint8_t> &&frame_data, int64_t frame_index, bool idr):
293 frame_data {std::move(frame_data)},
294 index {frame_index},
295 idr {idr} {
296 }
297
298 bool is_idr() override {
299 return idr;
300 }
301
302 int64_t frame_index() override {
303 return index;
304 }
305
306 uint8_t *data() override {
307 return frame_data.data();
308 }
309
310 size_t data_size() override {
311 return frame_data.size();
312 }
313
314 std::vector<uint8_t> frame_data;
315 int64_t index;
316 bool idr;
317 };
318
319 using packet_t = std::unique_ptr<packet_raw_t>;
320
322 explicit hdr_info_raw_t(bool enabled):
323 enabled {enabled},
324 metadata {} {};
325 explicit hdr_info_raw_t(bool enabled, const SS_HDR_METADATA &metadata):
326 enabled {enabled},
327 metadata {metadata} {};
328
329 bool enabled;
330 SS_HDR_METADATA metadata;
331 };
332
333 using hdr_info_t = std::unique_ptr<hdr_info_raw_t>;
334
335 extern int active_hevc_mode;
336 extern int active_av1_mode;
337 extern bool last_encoder_probe_supported_ref_frames_invalidation;
338 extern std::array<bool, 3> last_encoder_probe_supported_yuv444_for_codec; // 0 - H.264, 1 - HEVC, 2 - AV1
339
340 void capture(
341 safe::mail_t mail,
342 config_t config,
343 void *channel_data
344 );
345
346 bool validate_encoder(encoder_t &encoder, bool expect_failure);
347
356 int probe_encoders();
357
358 // Several NTSC standard refresh rates are hardcoded here, because their
359 // true rate requires a denominator of 1001. ffmpeg's av_d2q() would assume it could
360 // reduce 29.97 to 2997/100 but this would be slightly wrong. We also include
361 // support for 23.976 film in case someone wants to stream a film at the perfect
362 // framerate.
363 inline AVRational framerateX100_to_rational(const int framerateX100) {
364 if (framerateX100 % 2997 == 0) {
365 // Multiples of NTSC 29.97 e.g. 59.94, 119.88
366 return AVRational {(framerateX100 / 2997) * 30000, 1001};
367 }
368 switch (framerateX100) {
369 case 2397: // the other weird NTSC framerate, assume these want 23.976 film
370 case 2398:
371 return AVRational {24000, 1001};
372 default:
373 // any other fractional rate can be reduced by ffmpeg. Max is set to 1 << 26 based on docs:
374 // "rational numbers with |num| <= 1<<26 && |den| <= 1<<26 can be recovered exactly from their double representation"
375 return av_d2q((double) framerateX100 / 100.0f, 1 << 26);
376 }
377 }
378} // namespace video
Definition utility.h:530
Declarations for common platform specific utilities.
mem_type_e
Definition common.h:229
pix_fmt_e
Definition common.h:238
Declarations for gamepad, keyboard, and mouse input handling.
Handles process-wide communication.
Definition globals.h:34
Standalone NVENC encoder.
Definition nvenc_base.cpp:90
Definition common.h:409
Definition common.h:366
Definition video.h:23
Definition video.h:201
Definition video.h:164
Definition video.h:149
Definition video.h:120
flag_e
Definition video.h:123
@ DYNAMIC_RANGE
HDR support.
Definition video.h:126
@ VUI_PARAMETERS
AMD encoder with VAAPI doesn't add VUI parameters to SPS.
Definition video.h:128
@ PASSED
Indicates the encoder is supported.
Definition video.h:124
@ REF_FRAMES_RESTRICT
Set maximum reference frames.
Definition video.h:125
@ YUV444
YUV 4:4:4 support.
Definition video.h:127
@ MAX_FLAGS
Maximum number of flags.
Definition video.h:129
Definition video.h:321
Definition video.h:263
Definition video.h:291
Definition video.h:245
Definition video.h:234
Declarations for thread-safe data structures.
flag_e
Definition video.cpp:290
int probe_encoders()
Probe encoders and select the preferred encoder. This is called once at startup and each time a strea...
Definition video.cpp:2720
Declarations for colorspace functions.