Files
fegger 36d086af4e perf(codec): CRF rate control, longer GOP, faster preset, screen tuning
Four encoder quality improvements, all sender-side:

- CRF rate control (default 22, --crf to override): targets a
  constant visual quality level instead of a fixed bitrate. Static
  desktop content uses 300-800 kbps (vs. forced 4000+), and the saved
  bits go to sharp text and clean motion when they appear. The VBV
  max rate (the --bitrate value, now a cap rather than a target)
  bounds bursts so the receiver's UDP buffers stay safe. Round-trip
  test bitrate dropped from 1390 kb/s to 47 kb/s on synthetic frames
  — the encoder uses only what it needs.

- 5-second GOP (was 1 second): 80% fewer keyframe bits freed for
  detail frames. Screen content changes incrementally, not
  wholesale; PLI feedback recovers from loss in one frame time
  regardless of GOP length.

- faster preset (was veryfast): better sub-pixel estimation and
  RDO on more decisions. The desktop handles it trivially at 1080p.

- Screen-content x264 tuning: aq-mode=2 (auto-variance AQ moves
  bits away from flat areas toward text edges) and psy-rd=1.5
  (preserves texture sharpness).

Combined with the earlier veryfast upgrade and sender-side
downscaling, this is roughly 2x the perceived quality at the same
average bandwidth compared to the original ultrafast ABR encoder.

meson test 5/5 in both configurations, valgrind clean.
2026-09-09 12:12:36 +02:00

60 lines
1.6 KiB
C++

#pragma once
#include "screencast/capture/capture.h"
#include "screencast/codec/error.h"
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
namespace sc {
struct EncodedFrame {
uint64_t capture_timestamp_ns = 0;
uint32_t rtp_timestamp = 0;
bool is_keyframe = false;
std::vector<std::byte> data; // Annex-B H.264 NAL units
};
struct EncoderConfig {
std::string codec_name = "h264";
int width = 0;
int height = 0;
int frame_rate_num = 30;
int frame_rate_den = 1;
// CRF (constant rate factor, 0-51): the target visual quality level.
// Lower = better quality. 23 is x264's default; screen content looks
// good at 20-24.
int crf = 22;
// Maximum bitrate in kbps (the VBV cap). The encoder uses fewer bits on
// static content and more on motion, but never exceeds this.
int bitrate_kbps = 4000;
bool hardware_accel = false;
};
class Encoder {
public:
virtual ~Encoder() = default;
// Encode one captured frame. Returns an empty vector if the encoder emits
// no packet for this frame.
virtual CodecResult<std::vector<EncodedFrame>> encode(const CapturedFrame& frame) = 0;
// Flush the encoder and return any remaining packets.
virtual CodecResult<std::vector<EncodedFrame>> flush() = 0;
// Force the next output frame to be a keyframe.
virtual void request_keyframe() = 0;
// Codec-specific parameters required by a decoder (SPS/PPS for H.264).
virtual std::vector<std::byte> get_extradata() const = 0;
};
class EncoderFactory {
public:
static CodecResult<std::unique_ptr<Encoder>> create(const EncoderConfig& config);
};
} // namespace sc