19#include <libavcodec/avcodec.h>
20#include <libswscale/swscale.h>
33 inline constexpr int PYROWAVE_VIDEO_FORMAT = 64;
34 inline constexpr uint32_t PYROWAVE_SERVER_CODEC_MODE = 0x40000000;
59 int chromaSamplingType;
61 int enableIntraRefresh;
63 int encodingFramerate;
67 std::string display_name;
69 bool encoder_probe {
false};
71 std::string kwin_wayland_socket;
72 std::string kwin_pipewire_socket;
78 void free_ctx(AVCodecContext *ctx);
79 void free_frame(AVFrame *frame);
80 void free_buffer(AVBufferRef *ref);
82 using avcodec_ctx_t = util::safe_ptr<AVCodecContext, free_ctx>;
83 using avcodec_frame_t = util::safe_ptr<AVFrame, free_frame>;
84 using avcodec_buffer_t = util::safe_ptr<AVBufferRef, free_buffer>;
85 using sws_t = util::safe_ptr<SwsContext, sws_freeContext>;
86 using img_event_t = std::shared_ptr<safe::event_t<std::shared_ptr<platf::img_t>>>;
98 encoder_platform_formats_avcodec(
99 const AVHWDeviceType &avcodec_base_dev_type,
100 const AVHWDeviceType &avcodec_derived_dev_type,
101 const AVPixelFormat &avcodec_dev_pix_fmt,
102 const AVPixelFormat &avcodec_pix_fmt_8bit,
103 const AVPixelFormat &avcodec_pix_fmt_10bit,
104 const AVPixelFormat &avcodec_pix_fmt_yuv444_8bit,
105 const AVPixelFormat &avcodec_pix_fmt_yuv444_10bit,
106 const init_buffer_function_t &init_avcodec_hardware_input_buffer_function
108 avcodec_base_dev_type {avcodec_base_dev_type},
109 avcodec_derived_dev_type {avcodec_derived_dev_type},
110 avcodec_dev_pix_fmt {avcodec_dev_pix_fmt},
111 avcodec_pix_fmt_8bit {avcodec_pix_fmt_8bit},
112 avcodec_pix_fmt_10bit {avcodec_pix_fmt_10bit},
113 avcodec_pix_fmt_yuv444_8bit {avcodec_pix_fmt_yuv444_8bit},
114 avcodec_pix_fmt_yuv444_10bit {avcodec_pix_fmt_yuv444_10bit},
115 init_avcodec_hardware_input_buffer {init_avcodec_hardware_input_buffer_function} {
116 dev_type = map_base_dev_type(avcodec_base_dev_type);
117 pix_fmt_8bit = map_pix_fmt(avcodec_pix_fmt_8bit);
118 pix_fmt_10bit = map_pix_fmt(avcodec_pix_fmt_10bit);
119 pix_fmt_yuv444_8bit = map_pix_fmt(avcodec_pix_fmt_yuv444_8bit);
120 pix_fmt_yuv444_10bit = map_pix_fmt(avcodec_pix_fmt_yuv444_10bit);
123 AVHWDeviceType avcodec_base_dev_type, avcodec_derived_dev_type;
124 AVPixelFormat avcodec_dev_pix_fmt;
125 AVPixelFormat avcodec_pix_fmt_8bit, avcodec_pix_fmt_10bit;
126 AVPixelFormat avcodec_pix_fmt_yuv444_8bit, avcodec_pix_fmt_yuv444_10bit;
128 init_buffer_function_t init_avcodec_hardware_input_buffer;
132 encoder_platform_formats_nvenc(
139 encoder_platform_formats_t::dev_type = dev_type;
140 encoder_platform_formats_t::pix_fmt_8bit = pix_fmt_8bit;
141 encoder_platform_formats_t::pix_fmt_10bit = pix_fmt_10bit;
142 encoder_platform_formats_t::pix_fmt_yuv444_8bit = pix_fmt_yuv444_8bit;
143 encoder_platform_formats_t::pix_fmt_yuv444_10bit = pix_fmt_yuv444_10bit;
148 std::string_view name;
159 static std::string_view from_flag(
flag_e flag) {
162 return std::string_view(#x)
177 KITTY_DEFAULT_CONSTR_MOVE(option_t)
178 option_t(
const option_t &) =
default;
181 std::variant<int, int *, std::optional<int> *, std::function<int()>, std::string, std::string *, std::function<
const std::string(
const config_t &)>> value;
183 option_t(std::string &&name,
decltype(value) &&value):
184 name {std::move(name)},
185 value {std::move(value)} {
189 const std::unique_ptr<const encoder_platform_formats_t> platform_formats;
192 std::vector<option_t> common_options;
193 std::vector<option_t> sdr_options;
194 std::vector<option_t> hdr_options;
195 std::vector<option_t> sdr444_options;
196 std::vector<option_t> hdr444_options;
197 std::vector<option_t> fallback_options;
200 std::bitset<MAX_FLAGS> capabilities;
202 bool operator[](
flag_e flag)
const {
203 return capabilities[(std::size_t) flag];
206 std::bitset<MAX_FLAGS>::reference operator[](
flag_e flag) {
207 return capabilities[(std::size_t) flag];
212 switch (config.videoFormat) {
214 BOOST_LOG(error) <<
"Unknown video format " << config.videoFormat <<
", falling back to H.264";
222 case PYROWAVE_VIDEO_FORMAT:
237 virtual void request_idr_frame() = 0;
239 virtual void request_normal_frame() = 0;
241 virtual void invalidate_ref_frames(int64_t first_frame, int64_t last_frame) = 0;
249 bool pyrowave_available();
270 virtual bool is_idr() = 0;
272 virtual int64_t frame_index() = 0;
274 virtual uint8_t *data() = 0;
276 virtual size_t data_size() = 0;
279 std::string_view old;
280 std::string_view _new;
282 KITTY_DEFAULT_CONSTR_MOVE(replace_t)
284 replace_t(std::string_view old, std::string_view _new)
noexcept:
285 old {std::move(old)},
286 _new {std::move(_new)} {
290 std::vector<replace_t> *replacements =
nullptr;
291 void *channel_data =
nullptr;
292 bool after_ref_frame_invalidation =
false;
293 std::optional<std::chrono::steady_clock::time_point> frame_timestamp;
297 packet_raw_avcodec() {
298 av_packet = av_packet_alloc();
301 ~packet_raw_avcodec() {
302 av_packet_free(&this->av_packet);
305 bool is_idr()
override {
306 return av_packet->flags & AV_PKT_FLAG_KEY;
309 int64_t frame_index()
override {
310 return av_packet->pts;
313 uint8_t *data()
override {
314 return av_packet->data;
317 size_t data_size()
override {
318 return av_packet->size;
325 packet_raw_generic(std::vector<uint8_t> &&frame_data, int64_t frame_index,
bool idr):
326 frame_data {std::move(frame_data)},
331 bool is_idr()
override {
335 int64_t frame_index()
override {
339 uint8_t *data()
override {
340 return frame_data.data();
343 size_t data_size()
override {
344 return frame_data.size();
347 std::vector<uint8_t> frame_data;
352 using packet_t = std::unique_ptr<packet_raw_t>;
354 struct hdr_info_raw_t {
355 explicit hdr_info_raw_t(
bool enabled):
358 explicit hdr_info_raw_t(
bool enabled,
const SS_HDR_METADATA &metadata):
360 metadata {metadata} {};
363 SS_HDR_METADATA metadata;
366 using hdr_info_t = std::unique_ptr<hdr_info_raw_t>;
368 extern int active_hevc_mode;
369 extern int active_av1_mode;
370 extern bool last_encoder_probe_supported_ref_frames_invalidation;
371 extern std::array<bool, 3> last_encoder_probe_supported_yuv444_for_codec;
379 bool validate_encoder(
encoder_t &encoder,
bool expect_failure);
418 bool client_hdr_requested,
bool display_hdr,
bool hevc_hdr_supported,
bool av1_hdr_supported
Declarations for common platform specific utilities.
Handles process-wide communication.
Standalone NVENC encoder.
Codec and color depth for a capture readiness check.
int dynamic_range
0: SDR 8-bit, 1: HDR 10-bit.
int video_format
0: H.264, 1: HEVC, 2: AV1.
What the last capture-path check found, for a client to show.
std::string client
The paired client whose launch ran the check.
std::int64_t checked_at
Unix time of the check, in seconds.
std::string failure
The failed step and its reason, when not ok.
bool frame_seen
Whether a real frame was captured and encoded.
std::string encoder
The encoder, such as vaapi or nvenc.
bool ok
Whether a frame went all the way through, or the display was only idle.
std::string capture
What was captured, and how.
bool checked
Whether a check has run since Hermes started.
std::string gpu
The GPU it encodes on, where the platform can say.
Result of the last encoder probe, for diagnostics/metrics.
bool selected
True if this was the encoder ultimately chosen.
std::string name
Encoder backend name (e.g. "nvenc", "vaapi").
std::string outcome
Human-readable result, e.g. "selected", "unsupported_on_gpu", "probe_failed".
std::vector< encoder_attempt_t > attempts
std::string encoder
Selected encoder backend (e.g. "nvenc", "vaapi", "software").
bool hardware
True if the selected encoder is hardware-accelerated.
bool probed
A probe has completed at least once.
bool fell_back_to_software
True if a hardware encoder was rejected and we fell back to software.
bool h264
H.264 supported by the selected encoder.
@ DYNAMIC_RANGE
HDR support.
@ VUI_PARAMETERS
AMD encoder with VAAPI doesn't add VUI parameters to SPS.
@ PASSED
Indicates the encoder is supported.
@ REF_FRAMES_RESTRICT
Set maximum reference frames.
@ YUV444
YUV 4:4:4 support.
@ MAX_FLAGS
Maximum number of flags.
Live per-frame pipeline metrics, for diagnostics/metrics.
int width
Active stream width in pixels (0 until a session sets it).
double fps
Measured frames encoded per second.
int height
Active stream height in pixels (0 until a session sets it).
double encode_ms
Avg time spent in the encoder per frame.
uint64_t frames_encoded
Total frames encoded this session.
uint64_t frames_dropped
Total frames dropped (encoder errors/timeouts).
double bitrate_kbps
Measured output bitrate (kilobits/s).
bool valid
At least one frame has been measured.
double capture_to_encode_ms
Avg latency from capture to encode start.
Declarations for thread-safe data structures.
void forget_capture_readiness(const std::string &client)
Forget the last check if it was for client's launch: a later launch of theirs ran without one,...
std::optional< capture_readiness_mode_t > choose_capture_readiness_mode(bool client_hdr_requested, bool display_hdr, bool hevc_hdr_supported, bool av1_hdr_supported)
Choose a readiness mode without ever encoding PQ scanout as SDR.
void metrics_reset()
Reset pipeline metrics at the start of a streaming session.
void metrics_record_frame(double encode_ms, double capture_to_encode_ms, size_t packet_bytes)
Record one encoded frame's timings and output size.
bool display_present()
Whether the host has a display to capture without creating one.
encoder_status_t get_encoder_status()
Snapshot of the last encoder probe result (thread-safe copy).
void metrics_record_drop()
Record one dropped frame (encoder error/timeout).
std::size_t pyrowave_frame_budget(const config_t &config)
The most bytes one PyroWave frame may take.
capture_readiness_t last_capture_readiness()
The result of the last check_capture_path().
int probe_encoders()
Probe encoders and select the preferred encoder. This is called once at startup and each time a strea...
pipeline_metrics_t get_pipeline_metrics()
Snapshot of the live pipeline metrics (thread-safe copy).
int check_capture_path(const config_t &stream, std::string &failure, const std::string &client)
Run one real frame through the path a stream will use.
void metrics_set_resolution(int width, int height)
Record the active stream resolution for diagnostics.
int probe_encoders_without_display()
Probe encoders without opening any screen.
Declarations for colorspace functions.