17#include <libavcodec/avcodec.h>
18#include <libswscale/swscale.h>
75 void free_buffer(AVBufferRef *ref);
96 using img_event_t = std::shared_ptr<safe::event_t<std::shared_ptr<platf::img_t>>>;
222 #define _CONVERT(x) \
224 return std::string_view(#x)
251 std::variant<
int,
int *, std::optional<
int> *, std::function<
int()>, std::
string, std::
string *, std::function<const std::
string(const
config_t &)>>
value;
315 BOOST_LOG(
error) <<
"Unknown video format " << config.
videoFormat <<
", falling back to H.264";
365#if !defined(__APPLE__)
375#if defined(__linux__) || defined(linux) || defined(__linux) || defined(__FreeBSD__)
462 return av_packet->flags & AV_PKT_FLAG_KEY;
632 if (framerateX100 % 2997 == 0) {
634 return AVRational {(framerateX100 / 2997) * 30000, 1001};
636 switch (framerateX100) {
639 return AVRational {24000, 1001};
643 return av_d2q((
double) framerateX100 / 100.0f, 1 << 26);
671 return std::chrono::nanoseconds {(
static_cast<int64_t
>(fps.den) * 1'000'000'000LL) / fps.num};
Unique pointer wrapper with customizable pointer and deleter types.
Definition utility.h:820
Declarations for common platform specific utilities.
mem_type_e
Enumerates supported mem type options.
Definition common.h:303
pix_fmt_e
Enumerates supported pix fmt options.
Definition common.h:316
video_t video
Default video configuration values used before file and CLI overrides.
Definition config.cpp:623
void free_frame(AVFrame *frame)
Release an FFmpeg frame allocated by the capture or conversion backend.
Definition graphics.cpp:1403
bl::sources::severity_logger< int > error
Recoverable errors.
Definition logging.cpp:44
Handles process-wide communication.
Definition globals.h:34
Standalone NVENC encoder.
Definition nvenc_base.cpp:94
AVCodec-backed encode device and frame state.
Definition common.h:566
Captured frame buffer shared between capture and encode stages.
Definition common.h:502
Encoding configuration requested by a remote client.
Definition video.h:28
int encoderCscMode
Requested color range and SDR colorspace; HDR always uses BT.2020 and ST2084.
Definition video.h:36
int enableIntraRefresh
Intra refresh setting: 0 = disabled, 1 = enabled.
Definition video.h:40
int framerateX100
Optional NTSC-style framerate value, e.g. 59.94 as 5994.
Definition video.h:32
int videoFormat
Video codec format: 0 = H.264, 1 = HEVC, 2 = AV1.
Definition video.h:37
int width
Video width in pixels.
Definition video.h:29
int bitrate
Video bitrate in kilobits for the requested framerate.
Definition video.h:33
int numRefFrames
Maximum number of reference frames.
Definition video.h:35
int height
Video height in pixels.
Definition video.h:30
int slicesPerFrame
Number of slices per frame.
Definition video.h:34
int framerate
Requested framerate used in the per-frame bitrate budget.
Definition video.h:31
int dynamicRange
Encoding color depth: 0 = 8-bit, 1 = 10-bit.
Definition video.h:38
int chromaSamplingType
Chroma sampling type: 0 = 4:2:0, 1 = 4:4:4.
Definition video.h:39
Encoder session state shared by capture and encoding threads.
Definition video.h:332
virtual int convert(platf::img_t &img)=0
Convert a captured frame into the encoder's required input representation.
virtual void request_idr_frame()=0
Mark the frame as a request for an IDR frame.
virtual void request_normal_frame()=0
Mark the frame as a request for a normal inter frame.
virtual void invalidate_ref_frames(int64_t first_frame, int64_t last_frame)=0
Mark the frame range whose references must be invalidated.
Codec capabilities for AV1, HEVC, or H.264.
Definition video.h:270
std::bitset< MAX_FLAGS > capabilities
Capability flags supported by this codec on the encoder.
Definition video.h:279
std::vector< option_t > sdr_options
Options applied to SDR 4:2:0 streams.
Definition video.h:272
std::string name
Codec name passed to the encoder backend.
Definition video.h:278
std::vector< option_t > sdr444_options
Options applied to SDR 4:4:4 streams.
Definition video.h:274
std::vector< option_t > common_options
Options applied to every encode mode for this codec.
Definition video.h:271
std::bitset< MAX_FLAGS >::reference operator[](flag_e flag)
Return a mutable reference to a codec capability flag.
Definition video.h:297
std::vector< option_t > hdr_options
Options applied to HDR 4:2:0 streams.
Definition video.h:273
bool operator[](flag_e flag) const
Test whether a codec capability is enabled.
Definition video.h:287
std::vector< option_t > fallback_options
Options used when the preferred mode cannot be selected.
Definition video.h:276
std::vector< option_t > hdr444_options
Options applied to HDR 4:4:4 streams.
Definition video.h:275
Runtime encoder option exposed to configuration parsing.
Definition video.h:243
std::variant< int, int *, std::optional< int > *, std::function< int()>, std::string, std::string *, std::function< const std::string(const config_t &)> > value
Literal, pointer, or callback that supplies the option value.
Definition video.h:251
std::string name
Encoder command-line option name.
Definition video.h:250
Encoder name and feature flags advertised by Sunshine.
Definition video.h:198
codec_t av1
AV1 codec capability and option set.
Definition video.h:302
codec_t hevc
HEVC codec capability and option set.
Definition video.h:303
static std::string_view from_flag(flag_e flag)
Convert an encoder capability flag to its diagnostic string.
Definition video.h:220
const std::unique_ptr< const encoder_platform_formats_t > platform_formats
Platform-specific memory and pixel formats accepted by the encoder.
Definition video.h:265
const codec_t & codec_from_config(const config_t &config) const
Select the codec descriptor requested by a stream configuration.
Definition video.h:312
std::string_view name
Encoder name used in logs, configuration, and capability probes.
Definition video.h:199
flag_e
Capability flags that describe which stream modes an encoder supports.
Definition video.h:204
@ DYNAMIC_RANGE
HDR support.
Definition video.h:207
@ DYNAMIC_RANGE_YUV444
YUV 4:4:4 HDR support.
Definition video.h:209
@ VUI_PARAMETERS
AMD encoder with VAAPI doesn't add VUI parameters to SPS.
Definition video.h:210
@ PASSED
Indicates the encoder is supported.
Definition video.h:205
@ REF_FRAMES_RESTRICT
Set maximum reference frames.
Definition video.h:206
@ YUV444
YUV 4:4:4 support.
Definition video.h:208
@ MAX_FLAGS
Maximum number of flags.
Definition video.h:211
uint32_t flags
Encoder flags advertised to clients through GameStream capability responses.
Definition video.h:326
codec_t h264
H.264 codec capability and option set.
Definition video.h:304
Raw HDR metadata fields parsed from the display backend.
Definition video.h:561
bool enabled
Whether HDR mode should be enabled.
Definition video.h:580
SS_HDR_METADATA metadata
Display HDR metadata forwarded to the encoder and client.
Definition video.h:581
hdr_info_raw_t(bool enabled, const SS_HDR_METADATA &metadata)
Initialize HDR metadata with display metadata from the backend.
Definition video.h:576
hdr_info_raw_t(bool enabled)
Initialize HDR metadata with only the enabled state.
Definition video.h:567
AVCodec packet wrapper with codec-specific metadata.
Definition video.h:447
uint8_t * data() override
Return writable access to the FFmpeg packet payload.
Definition video.h:479
size_t data_size() override
Return the FFmpeg packet payload length.
Definition video.h:488
AVPacket * av_packet
FFmpeg packet that owns encoded data and frame metadata.
Definition video.h:492
bool is_idr() override
Report whether the FFmpeg packet is marked as a key frame.
Definition video.h:461
int64_t frame_index() override
Return the FFmpeg packet PTS used as Sunshine's frame index.
Definition video.h:470
Generic encoded packet bytes and metadata.
Definition video.h:498
uint8_t * data() override
Return writable access to the generic packet payload.
Definition video.h:535
bool is_idr() override
Report whether the generic packet starts an IDR frame.
Definition video.h:517
std::vector< uint8_t > frame_data
Encoded frame bytes owned by this packet.
Definition video.h:548
int64_t frame_index() override
Return the stored frame index for this generic packet.
Definition video.h:526
int64_t index
Monotonic frame index assigned to this packet.
Definition video.h:549
packet_raw_generic(std::vector< uint8_t > &&frame_data, int64_t frame_index, bool idr)
Wrap generic encoded frame bytes in Sunshine packet metadata.
Definition video.h:506
bool idr
Whether the packet belongs to an IDR frame.
Definition video.h:550
size_t data_size() override
Return the generic packet payload length.
Definition video.h:544
Packet byte-range replacement descriptor.
Definition video.h:420
std::string_view _new
Byte sequence that replaces old before transmission.
Definition video.h:422
std::string_view old
Byte sequence to find in the encoded packet.
Definition video.h:421
Encoded packet wrapper used by the streaming pipeline.
Definition video.h:386
virtual size_t data_size()=0
Return the encoded payload length.
virtual bool is_idr()=0
Report whether this packet starts an IDR frame.
std::vector< replace_t > * replacements
Optional encoded-byte substitutions applied before packetization.
Definition video.h:438
void * channel_data
Platform or protocol state carried with this packet.
Definition video.h:439
virtual int64_t frame_index()=0
Return the frame index associated with this encoded packet.
bool after_ref_frame_invalidation
Whether the frame follows reference-frame invalidation.
Definition video.h:440
virtual uint8_t * data()=0
Return writable access to the encoded packet bytes.
std::optional< std::chrono::steady_clock::time_point > frame_timestamp
Capture timestamp associated with the frame.
Definition video.h:441
Declarations for thread-safe data structures.
std::shared_ptr< mail_raw_t > mail_t
Shared mailbox handle used by event and queue wrappers.
Definition thread_safe.h:737
#define KITTY_DEFAULT_CONSTR_MOVE(x)
Declare defaulted noexcept move construction and assignment for a type.
Definition utility.h:120
encoder_t vaapi
VA-API.
Definition video.cpp:1178
platf::mem_type_e map_base_dev_type(AVHWDeviceType type)
Map base dev type values.
Definition video.cpp:3633
bool validate_encoder(encoder_t &encoder, bool expect_failure)
Validate encoder before it is used.
Definition video.cpp:2997
encoder_t mediafoundation
Mediafoundation.
Definition video.cpp:1043
int active_hevc_mode
HEVC mode selected by the most recent encoder probe.
Definition video.cpp:1404
void free_ctx(AVCodecContext *ctx)
Release context resources.
Definition video.cpp:78
int active_av1_mode
AV1 mode selected by the most recent encoder probe.
Definition video.cpp:1405
std::array< bool, 3 > last_encoder_probe_supported_yuv444_for_codec
YUV444 support discovered for each probed codec.
Definition video.cpp:1407
encoder_t amdvce
Amdvce.
Definition video.cpp:934
int probe_encoders()
Probe encoders and select the preferred encoder. This is called once at startup and each time a strea...
Definition video.cpp:3199
encoder_t quicksync
Quicksync.
Definition video.cpp:823
bool last_encoder_probe_supported_ref_frames_invalidation
Whether the last probe found reference-frame invalidation support.
Definition video.cpp:1406
platf::pix_fmt_e map_pix_fmt(AVPixelFormat fmt)
Map pix fmt values.
Definition video.cpp:3659
encoder_t videotoolbox
Videotoolbox.
Definition video.cpp:1311
encoder_t software
Software.
Definition video.cpp:1104
AVRational framerateX100_to_rational(const int framerateX100)
Convert a framerate stored as hundredths of Hz to an FFmpeg rational.
Definition video.h:631
std::chrono::nanoseconds capture_frame_interval(const config_t &config)
Capture frame interval for the requested framerate. Uses the exact fractional rate when the client pr...
Definition video.h:669
std::unique_ptr< packet_raw_t > packet_t
Owning pointer to an encoded packet abstraction.
Definition video.h:556
std::shared_ptr< safe::event_t< std::shared_ptr< platf::img_t > > > img_event_t
Shared event that transports captured images between capture and encode threads.
Definition video.h:96
AVRational framerate_to_rational(const config_t &config)
Requested framerate as an exact rational. Uses the exact fractional rate when the client provided an ...
Definition video.h:655
std::unique_ptr< hdr_info_raw_t > hdr_info_t
Owning pointer to optional HDR metadata for a captured display.
Definition video.h:587
Declarations for colorspace functions.