Documentation
¶
Overview ¶
Package encode defines the hardware video encoder contract shared by the vendor backends: NVENC, AMF, oneVPL (Quick Sync), and VideoToolbox.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrDropped = errors.New("encode: frame dropped by the encoder")
ErrDropped means the encoder discarded the frame to stay real time. The session is still usable.
var ErrUnsupported = errors.New("encode: unsupported by this encoder")
var HDR10Color = ColorDescription{Primaries: 9, Transfer: 16, Matrix: 9}
HDR10Color is BT.2020 primaries, SMPTE ST 2084 (PQ) transfer and the BT.2020 non-constant luminance matrix.
Functions ¶
func RegisterCodec ¶
func RegisterCodec(b CodecBackend)
RegisterCodec makes b the encoder for its codec; Open prefers it to the vendor's backend.
Types ¶
type Backend ¶
type Backend struct {
Name string
Vendor gpu.Vendor
Open func(dev gpu.Device, cfg Config) (Encoder, error)
}
Backend opens one vendor's hardware encoder on a device of that vendor. Backends register themselves when their package is imported; import pkg/encode/all for every backend of the platform.
type Caps ¶
type Caps struct {
Adapter gpu.Adapter
Codecs []Codec
Chroma map[Codec][]Chroma
MaxWidth int
MaxHeight int
IntraRefresh bool
SubFrame bool
// RefInvalidation means the encoder implements RefInvalidator.
RefInvalidation bool
Engines int
}
Caps are what the opened device reports, not what the API documents.
type Chroma ¶
type Chroma uint8
Chroma is the coded sample layout of the bitstream, independent of what the capture surface holds.
type CodecBackend ¶
type CodecBackend struct {
Name string
Codec Codec
Open func(dev gpu.Device, cfg Config) (Encoder, error)
}
CodecBackend opens an encoder for one codec on any vendor's GPU, such as PyroWave's compute shaders.
type ColorDescription ¶
type ColorDescription struct {
Primaries, Transfer, Matrix uint8
}
ColorDescription is a bitstream's colour signalling, as ITU-T H.273 code points: HEVC VUI and the AV1 sequence header carry the same values.
type Config ¶
type Config struct {
Codec Codec
Chroma Chroma
// Source is the surface format the encoder will be fed. Hardware fixes
// the input bit depth and color conversion when the session opens.
Source capture.Format
Width int
Height int
FPS int
BitrateKbps int
// IntraRefreshPeriod > 0 replaces periodic IDR frames with a rolling
// intra band that sweeps the picture every N frames.
IntraRefreshPeriod int
// Slices > 1 splits each picture so the packetizer can send the top of a
// frame while the bottom is still being encoded.
Slices int
// SubFrame asks the backend to expose slices as they finish instead of
// after the whole picture (NVENC subframe readback, VT not supported).
SubFrame bool
// Adapter selects the encoding GPU. Zero means the adapter that owns the
// captured surface (zero copy); any other LUID means cross-adapter offload.
Adapter gpu.LUID
// Split chooses whether a GPU with two or more encode engines splits
// each picture between them; see SplitFrame.
Split Split
// ChunkLen is the transport shard size PyroWave packets are framed in
// (Session.ShardSize); PyroWave needs at least 256.
ChunkLen int
// HDR10 says the Source holds BT.2020 PQ (an HDR desktop's
// RGB10A2): the bitstream signals HDR10Color, and the conversion to
// YUV uses the BT.2020 matrix. It needs a 10-bit Chroma.
HDR10 bool
}
Config is what the caller asks for. Backends refuse what the hardware cannot do with ErrUnsupported rather than silently degrading.
func (Config) SplitFrame ¶
SplitFrame reports whether the encoder should split each picture between its engines. SplitAuto splits at 1440 lines and up, and at 1080 lines from 200 fps, where one engine takes a large share of the frame interval; never with codec slices, which the engines do not split.
type Encoder ¶
type Encoder interface {
Caps() Caps
// Encode consumes frame synchronously and appends the bitstream to dst.
Encode(frame capture.Frame, forceKey bool, dst []byte) (Packet, error)
SetBitrate(kbps int) error
Close() error
}
Encoder turns captured GPU surfaces into packets.
func Open ¶
Open opens the encoder of the GPU that holds the frames, for zero copy: devices[0] is the capture device (NVIDIA -> NVENC, AMD -> AMF, Intel -> oneVPL, Apple -> VideoToolbox). A non-zero cfg.Adapter picks the device in devices with that LUID instead; the caller then moves each frame to that device before Encode. A codec with its own backend (RegisterCodec) opens that backend on the chosen device whatever its vendor.
type Packet ¶
type Packet struct {
Data []byte
Keyframe bool
// SliceEnds holds the end offset of each slice in Data when the backend
// reports slice boundaries.
SliceEnds []int
PTS int64
// Critical is the number of leading slices whose loss costs more than
// local blur (PyroWave's coarse levels), which deserve more parity.
Critical int
}
Packet is one encoded picture. Data aliases the dst buffer given to Encode.
type RefInvalidator ¶
type RefInvalidator interface {
// InvalidateRefs marks the picture with this Packet.PTS, and every later
// one, as unusable references.
InvalidateRefs(pts int64) error
}
RefInvalidator is an Encoder that can stop predicting from pictures the receiver lost, so the stream recovers without a keyframe.
type SliceEncoder ¶
type SliceEncoder interface {
// EncodeSlices is Encode that also calls emit with each slice, in
// order, as it becomes readable: its index, its bytes (a part of the
// Packet's Data, valid until Encode or EncodeSlices runs again) and
// whether the picture is a keyframe. An error from emit ends the
// encode with that error.
EncodeSlices(frame capture.Frame, forceKey bool, dst []byte, emit func(index int, slice []byte, keyframe bool) error) (Packet, error)
}
SliceEncoder is an Encoder with sub-frame output (Config.SubFrame): it hands each codec slice on as soon as the hardware finished it, so the first slices travel while the rest of the picture is still encoding.
type Split ¶
type Split uint8
Split is a choice of split-frame encode: each picture divided between a GPU's encode engines finishes sooner, at about a third more bits for the same quality. Measured on an RTX 4080: 1440p120 encode p50 2.17 to 1.49 ms, 1080p240 1.35 to 1.03 ms, frames 32 to 33% larger.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package all registers every hardware encoder backend of the platform with pkg/encode, for encode.Open.
|
Package all registers every hardware encoder backend of the platform with pkg/encode, for encode.Open. |
|
Package encodetest runs hardware encoders on synthetic GPU frames for the encode, decode and player tests.
|
Package encodetest runs hardware encoders on synthetic GPU frames for the encode, decode and player tests. |
|
Package nvenc drives NVIDIA's hardware encoder through nvEncodeAPI64.dll's function table, with no cgo and no NVIDIA SDK at build time.
|
Package nvenc drives NVIDIA's hardware encoder through nvEncodeAPI64.dll's function table, with no cgo and no NVIDIA SDK at build time. |
|
Package vaapi encodes HEVC on Linux with VA-API (Intel's iHD and AMD's radeonsi drivers), through libva.so.2 and libva-drm.so.2 loaded with purego (no cgo).
|
Package vaapi encodes HEVC on Linux with VA-API (Intel's iHD and AMD's radeonsi drivers), through libva.so.2 and libva-drm.so.2 loaded with purego (no cgo). |
|
Package vt drives Apple VideoToolbox hardware HEVC encode and decode through purego, with no cgo.
|
Package vt drives Apple VideoToolbox hardware HEVC encode and decode through purego, with no cgo. |