Files
screen_cast/include/screencast/codec/encoder.h
T
fegger 71218b1b1f feat(codec): implement software H.264 encode/decode round-trip
Add FFmpeg-based encoder/decoder with SPS/PPS extradata, Annex-B output

normalization, low-latency libx264 settings, and a round-trip unit test.

Includes review hardening: cached SwsContext, bitrate-only rate control,

std::byte/uin8_t cast helpers, and richer test assertions.
2026-09-07 10:07:50 +02:00

54 lines
1.3 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;
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