codec

package
v0.0.0-...-495624c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package codec encodes swampcy's screen for the web viewer: AV1 through libaom and H.264 through OpenH264, both loaded at run time with purego, so swampcy needs no cgo and runs (without the viewer) where neither library is installed.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnavailable = errors.New("codec library not available")

ErrUnavailable means the codec's library could not be loaded.

Functions

func ConvertXRGB

func ConvertXRGB(f *Frame, src []byte, stride, width, height int, rects []image.Rectangle, yInvert bool)

ConvertXRGB converts the given rectangles of an XRGB8888 picture (little-endian, so bytes B, G, R, X: wl_shm's format 1, and ARGB8888's layout too) into f, which has the same size. Rectangles are widened to even coordinates so every 2x2 chroma block they touch is recomputed whole. yInvert reads the source bottom up.

Types

type Config

type Config struct {
	Width, Height int
	// FPS is the frame rate the rate control plans for. Frames arrive only when
	// the screen changes, at most this often, each with its own timestamp.
	FPS int
	// BitrateKbps caps the stream; the network's estimate moves it at run time.
	BitrateKbps int
	// Quality is a constant-quality target where the codec has one (AV1's cq-level,
	// 0 best to 63 worst). The bitrate cap still applies. 0 means the default (24).
	Quality int
	// Threads is how many threads the encoder may use; 0 means its default.
	Threads int
	// H264Profile is "high" (CABAC) or "baseline" (CAVLC, constrained baseline).
	// Ignored by AV1.
	H264Profile string
}

Config is what an encoder is opened with. Sizes are fixed for its life; the bitrate can change.

type Encoder

type Encoder interface {
	// Encode encodes f, as a keyframe when forceKey is set. The encoder does not
	// keep f after returning.
	Encode(f *Frame, forceKey bool) (Packet, error)
	// SetBitrate changes the cap, in kbit/s, from the next frame on.
	SetBitrate(kbps int) error
	// Name is the codec's name as WebRTC knows it: "AV1" or "H264".
	Name() string
	Close() error
}

Encoder turns frames into packets, one per frame, with no lookahead.

func NewAV1

func NewAV1(cfg Config) (Encoder, error)

NewAV1 opens libaom's AV1 encoder in realtime mode with its screen-content tools, tuned as swampcy's codec benchmark found best for a desktop: speed 10, palette and intra block copy on, no lookahead, constrained quality at cfg.Quality with cfg.BitrateKbps as the cap, keyframes only on request, BT.709 full-range colour.

func NewH264

func NewH264(cfg Config) (Encoder, error)

NewH264 opens an OpenH264 encoder in its screen-content real-time mode.

type Frame

type Frame struct {
	Y, U, V          []byte
	YStride, UStride int // UStride is also V's
	PTS              time.Duration
}

Frame is one picture in I420: full-range BT.709 luma and 2x2-subsampled chroma.

func NewI420

func NewI420(width, height int) *Frame

NewI420 allocates a frame of the given size.

type Packet

type Packet struct {
	Data     []byte
	Keyframe bool
	PTS      time.Duration
}

Packet is one encoded frame: for AV1 the temporal unit's OBUs (low-overhead format, what pion's AV1 payloader takes); for H.264 the access unit's NAL units in Annex B (start codes), what pion's H.264 payloader takes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL