18#include <libavcodec/avcodec.h>
19#include <libswscale/swscale.h>
65 int select_h264_profile(std::string_view encoder_name,
const config_t &config,
int amd_coder);
87 void free_ctx(AVCodecContext *ctx);
99 void free_buffer(AVBufferRef *ref);
120 using img_event_t = std::shared_ptr<safe::event_t<std::shared_ptr<platf::img_t>>>;
143 int set_frame(AVFrame *in_frame, AVBufferRef *hw_frames_ctx)
override;
160 int init(
int in_width,
int in_height, AVFrame *in_frame, AVPixelFormat format,
bool hardware);
174 int reinit_sws(AVPixelFormat src_format);
183 AVPixelFormat sws_src_format {AV_PIX_FMT_BGR0};
314 #define _CONVERT(x) \
316 return std::string_view(#x)
343 std::variant<
int,
int *, std::optional<
int> *, std::function<
int()>, std::
string, std::
string *, std::function<const std::
string(const
config_t &)>>
value;
405 switch (config.videoFormat) {
407 BOOST_LOG(
error) <<
"Unknown video format " << config.videoFormat <<
", falling back to H.264";
457#if !defined(__APPLE__)
467#if defined(__linux__) || defined(linux) || defined(__linux) || defined(__FreeBSD__)
540 packet_raw_avcodec() {
544 ~packet_raw_avcodec() {
554 return av_packet->flags & AV_PKT_FLAG_KEY;
740 if (framerateX100 % 2997 == 0) {
742 return AVRational {(framerateX100 / 2997) * 30000, 1001};
744 switch (framerateX100) {
747 return AVRational {24000, 1001};
751 return av_d2q((
double) framerateX100 / 100.0f, 1 << 26);
764 if (config.framerateX100 > 0) {
767 return AVRational {config.framerate, 1};
779 return std::chrono::nanoseconds {(
static_cast<int64_t
>(fps.den) * 1'000'000'000LL) / fps.num};
FFmpeg software encode device used when no hardware frames are required.
Definition video.h:125
int init(int in_width, int in_height, AVFrame *in_frame, AVPixelFormat format, bool hardware)
Initialize FFmpeg software encoding for the requested codec.
Definition video.cpp:330
int convert(platf::img_t &img) override
Convert a captured image into the encoder input representation.
Definition video.cpp:224
void apply_colorspace() override
Apply the configured colorspace metadata to the active frame.
Definition video.cpp:317
int set_frame(AVFrame *in_frame, AVBufferRef *hw_frames_ctx) override
Attach frame resources used by the next conversion or encode operation.
Definition video.cpp:300
Declarations for common platform specific utilities.
mem_type_e
Enumerates supported mem type options.
Definition common.h:325
pix_fmt_e
Enumerates supported pix fmt options.
Definition common.h:338
void free_frame(AVFrame *frame)
Release an FFmpeg frame allocated by the capture or conversion backend.
Definition graphics.cpp:1537
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:185
AVCodec-backed encode device and frame state.
Definition common.h:588
Captured frame buffer shared between capture and encode stages.
Definition common.h:524
Encoding configuration requested by a remote client.
Definition video.h:29
int encoderCscMode
Requested color range and SDR colorspace; HDR always uses BT.2020 and ST2084.
Definition video.h:37
int enableIntraRefresh
Intra refresh setting: 0 = disabled, 1 = enabled.
Definition video.h:41
int framerateX100
Optional NTSC-style framerate value, e.g. 59.94 as 5994.
Definition video.h:33
int videoFormat
Video codec format: 0 = H.264, 1 = HEVC, 2 = AV1.
Definition video.h:38
int width
Video width in pixels.
Definition video.h:30
int bitrate
Video bitrate in kilobits for the requested framerate.
Definition video.h:34
int numRefFrames
Maximum number of reference frames.
Definition video.h:36
int height
Video height in pixels.
Definition video.h:31
int slicesPerFrame
Number of slices per frame.
Definition video.h:35
int framerate
Requested framerate used in the per-frame bitrate budget.
Definition video.h:32
int dynamicRange
Encoding color depth: 0 = 8-bit, 1 = 10-bit.
Definition video.h:39
int chromaSamplingType
Chroma sampling type: 0 = 4:2:0, 1 = 4:4:4.
Definition video.h:40
Encoder session state shared by capture and encoding threads.
Definition video.h:424
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:362
std::bitset< MAX_FLAGS > capabilities
Capability flags supported by this codec on the encoder.
Definition video.h:371
std::vector< option_t > sdr_options
Options applied to SDR 4:2:0 streams.
Definition video.h:364
std::string name
Codec name passed to the encoder backend.
Definition video.h:370
std::vector< option_t > sdr444_options
Options applied to SDR 4:4:4 streams.
Definition video.h:366
std::vector< option_t > common_options
Options applied to every encode mode for this codec.
Definition video.h:363
std::bitset< MAX_FLAGS >::reference operator[](flag_e flag)
Return a mutable reference to a codec capability flag.
Definition video.h:389
std::vector< option_t > hdr_options
Options applied to HDR 4:2:0 streams.
Definition video.h:365
bool operator[](flag_e flag) const
Test whether a codec capability is enabled.
Definition video.h:379
std::vector< option_t > fallback_options
Options used when the preferred mode cannot be selected.
Definition video.h:368
std::vector< option_t > hdr444_options
Options applied to HDR 4:4:4 streams.
Definition video.h:367
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:343
option_t(option_t &&) noexcept=default
Defaulted move constructor for the generated type.
std::string name
Encoder command-line option name.
Definition video.h:342
Encoder name and feature flags advertised by Sunshine.
Definition video.h:290
codec_t av1
AV1 codec capability and option set.
Definition video.h:394
codec_t hevc
HEVC codec capability and option set.
Definition video.h:395
static std::string_view from_flag(flag_e flag)
Convert an encoder capability flag to its diagnostic string.
Definition video.h:312
const std::unique_ptr< const encoder_platform_formats_t > platform_formats
Platform-specific memory and pixel formats accepted by the encoder.
Definition video.h:357
const codec_t & codec_from_config(const config_t &config) const
Select the codec descriptor requested by a stream configuration.
Definition video.h:404
std::string_view name
Encoder name used in logs, configuration, and capability probes.
Definition video.h:291
flag_e
Capability flags that describe which stream modes an encoder supports.
Definition video.h:296
@ DYNAMIC_RANGE
HDR support.
Definition video.h:299
@ DYNAMIC_RANGE_YUV444
YUV 4:4:4 HDR support.
Definition video.h:301
@ VUI_PARAMETERS
AMD encoder with VAAPI doesn't add VUI parameters to SPS.
Definition video.h:302
@ PASSED
Indicates the encoder is supported.
Definition video.h:297
@ REF_FRAMES_RESTRICT
Set maximum reference frames.
Definition video.h:298
@ YUV444
YUV 4:4:4 support.
Definition video.h:300
@ MAX_FLAGS
Maximum number of flags.
Definition video.h:303
uint32_t flags
Encoder flags advertised to clients through GameStream capability responses.
Definition video.h:418
codec_t h264
H.264 codec capability and option set.
Definition video.h:396
bool enabled
Whether HDR mode should be enabled.
Definition video.h:672
SS_HDR_METADATA metadata
Display HDR metadata forwarded to the encoder and client.
Definition video.h:673
hdr_info_raw_t(bool enabled, const SS_HDR_METADATA &metadata)
Initialize HDR metadata with display metadata from the backend.
Definition video.h:668
hdr_info_raw_t(bool enabled)
Initialize HDR metadata with only the enabled state.
Definition video.h:659
uint8_t * data() override
Return writable access to the FFmpeg packet payload.
Definition video.h:571
size_t data_size() override
Return the FFmpeg packet payload length.
Definition video.h:580
AVPacket * av_packet
FFmpeg packet that owns encoded data and frame metadata.
Definition video.h:584
bool is_idr() override
Report whether the FFmpeg packet is marked as a key frame.
Definition video.h:553
int64_t frame_index() override
Return the FFmpeg packet PTS used as Sunshine's frame index.
Definition video.h:562
uint8_t * data() override
Return writable access to the generic packet payload.
Definition video.h:627
bool is_idr() override
Report whether the generic packet starts an IDR frame.
Definition video.h:609
std::vector< uint8_t > frame_data
Encoded frame bytes owned by this packet.
Definition video.h:640
int64_t frame_index() override
Return the stored frame index for this generic packet.
Definition video.h:618
int64_t index
Monotonic frame index assigned to this packet.
Definition video.h:641
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:598
bool idr
Whether the packet belongs to an IDR frame.
Definition video.h:642
size_t data_size() override
Return the generic packet payload length.
Definition video.h:636
replace_t(replace_t &&) noexcept=default
Defaulted move constructor for the generated type.
std::string_view _new
Byte sequence that replaces old before transmission.
Definition video.h:514
std::string_view old
Byte sequence to find in the encoded packet.
Definition video.h:513
Encoded packet wrapper used by the streaming pipeline.
Definition video.h:478
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:530
void * channel_data
Platform or protocol state carried with this packet.
Definition video.h:531
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:532
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:533
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:753
uniq_ptr< T, Destroy< T *, void, function > > safe_ptr
Unique pointer using a compile-time C-style destroy function.
Definition utility.h:1591
#define KITTY_DEFAULT_CONSTR_MOVE(x)
Declare defaulted noexcept move construction and assignment for a type.
Definition utility.h:121
encoder_t vaapi
VA-API.
Definition video.cpp:1233
config_t resolve_dynamic_range(const encoder_t &encoder, config_t config)
Resolve a client-requested dynamic range against probed encoder capabilities.
Definition video.cpp:170
void capture(safe::mail_t mail, config_t config, void *channel_data)
Capture and encode video for a streaming session.
Definition video.cpp:2966
platf::mem_type_e map_base_dev_type(AVHWDeviceType type)
Map base dev type values.
Definition video.cpp:3696
bool validate_encoder(encoder_t &encoder, bool expect_failure)
Validate encoder before it is used.
Definition video.cpp:3068
encoder_t mediafoundation
Mediafoundation.
Definition video.cpp:1092
int active_hevc_mode
HEVC mode selected by the most recent encoder probe.
Definition video.cpp:1471
int active_av1_mode
AV1 mode selected by the most recent encoder probe.
Definition video.cpp:1472
std::array< bool, 3 > last_encoder_probe_supported_yuv444_for_codec
YUV444 support discovered for each probed codec.
Definition video.cpp:1474
encoder_t amdvce
Amdvce.
Definition video.cpp:977
int probe_encoders()
Probe encoders and select the preferred encoder. This is called once at startup and each time a strea...
Definition video.cpp:3266
encoder_t quicksync
Quicksync.
Definition video.cpp:863
bool last_encoder_probe_supported_ref_frames_invalidation
Whether the last probe found reference-frame invalidation support.
Definition video.cpp:1473
platf::pix_fmt_e map_pix_fmt(AVPixelFormat fmt)
Map pix fmt values.
Definition video.cpp:3722
encoder_t videotoolbox
Videotoolbox.
Definition video.cpp:1375
encoder_t software
Software.
Definition video.cpp:1156
util::safe_ptr< AVCodecContext, free_ctx > avcodec_ctx_t
Owning pointer for an FFmpeg codec context.
Definition video.h:104
AVRational framerateX100_to_rational(const int framerateX100)
Convert a framerate stored as hundredths of Hz to an FFmpeg rational.
Definition video.h:739
coder_e
Enumerates supported coder options for the AMF encoder.
Definition video.h:49
@ auto_
Select the coder based on the H.264 profile.
Definition video.h:50
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:777
std::unique_ptr< packet_raw_t > packet_t
Owning pointer to an encoded packet abstraction.
Definition video.h:648
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:120
util::safe_ptr< SwsContext, sws_freeContext > sws_t
Owning pointer for an FFmpeg software-scaling context.
Definition video.h:116
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:763
util::safe_ptr< AVBufferRef, free_buffer > avcodec_buffer_t
Owning pointer for an FFmpeg buffer reference.
Definition video.h:112
util::safe_ptr< AVFrame, free_frame > avcodec_frame_t
Owning pointer for an FFmpeg frame.
Definition video.h:108
std::unique_ptr< hdr_info_raw_t > hdr_info_t
Owning pointer to optional HDR metadata for a captured display.
Definition video.h:679
Declarations for colorspace functions.