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
- Variables
- func AV1QIndex(qp int) int
- func NVENCMaxBitRate(w, h int) uint32
- func VTQuality(qp int) float64
- type Codec
- type ColorInfo
- type Config
- type DecodeConfig
- type DecodedPicture
- type Decoder
- type Deinterlacing
- type Encoder
- type Kind
- type Picture
- type PlanarPicture
- type StereoEncoder
- type VideoCodec
Constants ¶
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 ¶
ErrDecodeUnavailable says the decoder's library or device is not present, or it cannot decode this stream.
var ErrDepth = errors.New("gpu: bit depth not supported")
ErrDepth says the encoder or codec cannot encode the bit depth asked.
ErrUnavailable says the encoder's library or device is not present.
Functions ¶
func AV1QIndex ¶
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
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.
Types ¶
type ColorInfo ¶
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.
type Picture ¶
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
Source Files
¶
- call_linux.go
- cstruct.go
- decode.go
- deinterlace.go
- dl_unix.go
- gpu.go
- guid_linux.go
- mem.go
- mf_other.go
- nvdec.go
- nvdec_layout_linux.go
- nvenc.go
- nvenc_layout.go
- sps.go
- vaapi_av1_linux.go
- vaapi_dec_linux.go
- vaapi_h264_linux.go
- vaapi_headers_linux.go
- vaapi_layout.go
- vaapi_linux.go
- vaapi_mpeg2_linux.go
- vaapi_params_linux.go
- vt_other.go