gpu

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 17 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 gpu drives GPU video encoders and decoders through their system libraries — NVENC and NVDEC (NVIDIA's driver), VAAPI (Mesa, Intel's media driver), VideoToolbox (macOS) and Media Foundation (Windows) — loaded at run time, so bdtools needs neither cgo nor ffmpeg for hardware video.

Each encoder takes NV12 pictures (P010 for 10-bit) and writes an Annex B H.264 or HEVC stream, or an AV1 stream of OBUs in the low-overhead format (each with its size), temporal unit after temporal unit. Each decoder takes a compressed stream and gives back pictures in the same layouts.

Index

Constants

View Source
const DefaultGOP = 250

DefaultGOP is the keyframe interval when none is given: 250 frames, as x264 and x265 default to, about 10 s at film rates. Against a 2 s interval it saves 7% at the same quality, and players seek within it fine.

Variables

View Source
var ErrDecodeUnavailable = errors.New("gpu: decoder not available")

ErrDecodeUnavailable says the decoder's library or device is not present, or it cannot decode this stream.

View Source
var ErrDepth = errors.New("gpu: bit depth not supported")

ErrDepth says the encoder or codec cannot encode the bit depth asked.

View Source
var ErrUnavailable = errors.New("gpu: not available")

ErrUnavailable says the encoder's library or device is not present.

Functions

func AV1QIndex

func AV1QIndex(qp int) int

AV1QIndex maps a quantiser on the H.264/HEVC 0-51 scale to an AV1 quantizer index (1-255), so that a value means about the same picture quality in AV1 as in HEVC. Index 0 would be AV1's lossless mode, which no --crf means, so the bottom of the scale stops at 1.

The line was measured on NVENC (same preset, tuning and B-frames for both codecs) over 300 frames of a Blu-ray 3D: for HEVC QP 14, 18, 24 and 30, the AV1 index with the same luma PSNR against the source was 26, 49, 92 and 139, and the line fits those within 3 (about 0.2 dB). At equal PSNR the AV1 encodes were 11-15% smaller.

func NVENCMaxBitRate added in v0.8.3

func NVENCMaxBitRate(w, h int) uint32

NVENCMaxBitRate is NVENC's ceiling at a constant quality, in bits a second, for a w x h picture: a Blu-ray's most and then some for HD (50 Mbit/s, which NVENC declares level 4.1, as HD HEVC is), an Ultra HD Blu-ray's for larger pictures (100 Mbit/s, level 5.0). Neither holds back an encode at any --crf a film is converted at; higher, NVENC declares levels players refuse (6.1 at 300 Mbit/s). A size of 0 (not known yet) takes the larger.

func VTQuality

func VTQuality(qp int) float64

VTQuality maps a constant quantiser, lower is better, onto VideoToolbox's quality, 0 to 1 with higher better: QP 0 is 1, QP 51 is 0.

Types

type Codec

type Codec int

Codec is the output codec.

const (
	H264 Codec = iota
	HEVC
	AV1
)

type ColorInfo

type ColorInfo struct {
	Primaries, Transfer, Matrix int
	FullRange                   bool
}

ColorInfo is a stream's colour signalling, as its sequence header states it (H.273 code points; 2 is "unspecified").

type Config

type Config struct {
	Codec          Codec
	Width, Height  int
	FPSNum, FPSDen int
	// QP is the constant quantiser for P pictures; I and B pictures get
	// ffmpeg's default factors for the encoder from it, so a value means
	// what it did when these encoders were driven through ffmpeg's -qp.
	// For AV1 it is on the same 0-51 scale and mapped by AV1QIndex.
	QP int
	// CQ, when set, has NVENC encode H.264 and HEVC at a constant quality
	// (variable rate, its targetQuality) instead of constant QP: the
	// encoder moves each picture's quantiser where it gains most, which at
	// the size of a constant-QP encode keeps its worst pictures nearer the
	// source. It is what ffmpeg's -rc vbr -cq CQ -b:v 0 sets. Other
	// encoders, and AV1, keep to QP.
	CQ int
	// GOP is the keyframe interval in frames; 0 picks DefaultGOP.
	GOP int
	// Device is the VAAPI render node (VAAPI only).
	Device string
	// BitDepth is 8 (or 0) or 10. Ten encodes HEVC Main 10 or 10-bit AV1
	// from P010 pictures: from an 8-bit source the picture is the same, but
	// the encoder works at finer precision, which shows as less banding and
	// a few percent fewer bits for the same quality. H.264 is 8-bit only.
	BitDepth int
	// Color, when set, is the colour signalling the stream states (its
	// VUI, or AV1's colour config): an HDR source's BT.2020 and PQ, say.
	Color *ColorInfo
	// Views is 2 for stereo MV-HEVC (HEVC on NVENC): the left eye as the
	// base layer, the right as layer 1 predicted from it, each picture
	// given through StereoEncoder.EncodeStereo. 0 or 1 is one view.
	Views int
	// LowLatency encodes each picture as it comes, for a stream a player
	// reads live: no B-frames (so no reordering), no lookahead, and each
	// picture's bitstream written before the next is taken (NVENC; the
	// other encoders keep their own pace).
	LowLatency bool
}

Config describes the stream to encode.

type DecodeConfig

type DecodeConfig struct {
	Codec VideoCodec
	// LowDelay asks for pictures soon after their access units, at some
	// cost in speed, where a decoder holds them back (ffmpeg's frame
	// threads): for a stream decoded beside another and paired with it.
	LowDelay bool
}

DecodeConfig describes the stream to decode.

type DecodedPicture

type DecodedPicture struct {
	Width, Height int
	Depth         int
	Y, UV         []byte
	Pitch         int
	PTS           int64 // as given to Decode
	Color         ColorInfo
	// FrameRateNum and FrameRateDen are the stream's, when it states one.
	FrameRateNum, FrameRateDen int
}

DecodedPicture is one picture in display order, in the layout the encoders take: a luma plane and a half-resolution plane of interleaved Cb/Cr, each row Pitch bytes apart. At Depth 8 it is NV12; above it, P010 (two bytes a sample, little-endian, the value in the top bits). Its planes are valid only during the call that delivers it.

type Decoder

type Decoder interface {
	// Decode feeds one access unit (Annex B; for VC-1 and MPEG-2 the
	// elementary stream's own framing) presented at pts. Pictures come out
	// in display order through the function given to OpenDecoder, from
	// within Decode or Flush.
	Decode(au []byte, pts int64) error
	// Flush ends the stream: the pictures held back for reordering come
	// out.
	Flush() error
	// Close releases the device.
	Close() error
}

Decoder decodes a compressed video stream.

func OpenDecoder

func OpenDecoder(k Kind, cfg DecodeConfig, picture func(*DecodedPicture) error) (Decoder, error)

OpenDecoder starts a decoder of kind k, calling picture with each decoded picture in display order.

type Deinterlacing added in v0.8.0

type Deinterlacing struct {
	// contains filtered or unexported fields
}

Deinterlacing deinterlaces a decoder's pictures and hands them on as NV12.

func NewDeinterlacing added in v0.8.0

func NewDeinterlacing(picture func(*DecodedPicture) error) *Deinterlacing

NewDeinterlacing gives the pictures taken to picture.

func (*Deinterlacing) Flush added in v0.8.0

func (g *Deinterlacing) Flush() error

Flush gives out the frames the deinterlacer holds.

func (*Deinterlacing) Take added in v0.8.0

func (g *Deinterlacing) Take(p *PlanarPicture) error

Take copies a decoded picture (the decoders reuse their buffers) for the deinterlacer, which holds the one after the frame it gives out.

type Encoder

type Encoder interface {
	// Encode asks fill to draw the next picture, then encodes it.
	Encode(fill func(*Picture)) error
	// Close finishes the stream (the encoder may hold pictures back for
	// B-frames) and releases the device.
	Close() error
}

Encoder encodes pictures to an Annex B stream.

func Open

func Open(k Kind, cfg Config, w io.Writer) (Encoder, error)

Open starts an encoder writing to w.

type Kind

type Kind string

Kind names a hardware encoder.

const (
	NVENC        Kind = "nvenc"
	VAAPI        Kind = "vaapi"
	VideoToolbox Kind = "videotoolbox"
	// MediaFoundation is the encoder MFT a Windows GPU driver installs:
	// Intel Quick Sync, AMD AMF, NVIDIA's too.
	MediaFoundation Kind = "mediafoundation"
)

type Picture

type Picture struct {
	Y, UV []byte
	Pitch int
	Depth int
}

Picture is the frame an encoder wants filled: a full-resolution luma plane and a half-resolution plane of interleaved Cb/Cr, each row Pitch bytes apart. At Depth 8 it is NV12, a byte a sample; at 10 it is P010, two bytes a sample, little-endian, the value in the top ten bits.

type PlanarPicture added in v0.8.0

type PlanarPicture struct {
	Width, Height              int
	Y, Cb, Cr                  []byte
	StrideY, StrideC           int
	PTS                        int64
	Interlaced, TopFieldFirst  bool
	FrameRateNum, FrameRateDen int
	Color                      ColorInfo
}

PlanarPicture is a decoded picture in 8-bit 4:2:0 planes.

func MPEG2Picture added in v0.8.0

func MPEG2Picture(p *mpeg2.Picture) *PlanarPicture

MPEG2Picture is an MPEG-2 decoder's picture as a PlanarPicture.

type StereoEncoder added in v0.10.0

type StereoEncoder interface {
	Encoder
	// EncodeStereo asks fill to draw the left and right eyes of the next
	// picture, then encodes them as the base layer and layer 1.
	EncodeStereo(fill func(left, right *Picture)) error
}

StereoEncoder is an Encoder opened with Config.Views 2: it takes both eyes of each picture.

type VideoCodec

type VideoCodec int

VideoCodec is a compressed video format a decoder takes.

const (
	DecodeH264 VideoCodec = iota
	DecodeHEVC
	DecodeVC1
	DecodeMPEG2
	DecodeAV1
)

func (VideoCodec) String

func (c VideoCodec) String() string

Jump to

Keyboard shortcuts

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