hevc

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: 11 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 hevc decodes H.265/HEVC Main and Main 10 video in Go (4:2:0, 8 and 10 bits, every tool of those profiles): a fallback where no GPU decodes an Ultra HD Blu-ray's HEVC.

Index

Constants

View Source
const (
	RPSStCurrBefore = 1 + iota
	RPSStCurrAfter
	RPSLtCurr
)

The reference picture set lists.

Variables

This section is empty.

Functions

func IsUnsupported

func IsUnsupported(err error) bool

IsUnsupported reports whether err is the decoder declining a stream it does not decode (the format range extensions, for one).

Types

type Accel

type Accel interface {
	// NewSurface gives a surface for pictures of the size and depth (the
	// coded size, in luma samples). A change of size or depth comes at an
	// IRAP picture that empties the buffer: the surfaces of the old one
	// have all been released.
	NewSurface(width, height, bitDepth int) (int, error)
	// Release gives a surface back.
	Release(surface int)
	// DecodePicture decodes a picture from its slice segments, in order.
	DecodePicture(p *AccelPicture, slices []AccelSlice) error
	// Output gives out the picture in surface, its information in p (no
	// planes), in display order.
	Output(surface int, p *Picture) error
}

Accel decodes pictures for the decoder.

type AccelPicture

type AccelPicture struct {
	Surface int
	POC     int
	// Refs are the references the buffer holds; the slices' lists index
	// them.
	Refs      []AccelRef
	IRAP, IDR bool

	// From the SPS.
	Width, Height                    int // in luma samples
	ChromaFormat                     int
	SeparateColourPlane              bool
	BitDepth, BitDepthC              int
	Log2MaxPOCLsb                    int
	MaxDecPicBuffering               int
	Log2MinCb, Log2Ctb               int
	Log2MinTb, Log2MaxTb             int
	MaxTrDepthInter, MaxTrDepthIntra int
	PCM                              bool
	PCMBits, PCMBitsC                int
	Log2MinPCM, Log2MaxPCM           int
	PCMLoopFilterDisabled            bool
	AMP, SAO                         bool
	TemporalMVP                      bool
	StrongIntraSmoothing             bool
	LongTermRefsPresent              bool
	NumShortTermRPS                  int
	NumLongTermRefsSPS               int
	// Scaling is nil without scaling lists.
	Scaling *ScalingLists

	// From the PPS.
	DependentSlices         bool
	OutputFlagPresent       bool
	NumExtraSliceHeaderBits int
	SignDataHiding          bool
	CabacInitPresent        bool
	NumRefIdxDefault        [2]int
	InitQP                  int
	ConstrainedIntraPred    bool
	TransformSkip           bool
	CuQPDelta               bool
	DiffCuQPDeltaDepth      int
	CbQPOffset, CrQPOffset  int
	SliceChromaQPOffsets    bool
	WeightedPred            bool
	WeightedBipred          bool
	TransquantBypass        bool
	Tiles                   bool
	EntropySync             bool
	ColumnWidths            []int // in CTBs, with tiles
	RowHeights              []int
	LoopFilterAcrossTiles   bool
	LoopFilterAcrossSlices  bool
	DeblockingOverride      bool
	DeblockingDisabled      bool
	BetaOffsetDiv2          int
	TcOffsetDiv2            int
	ListsModification       bool
	Log2ParMrgLevel         int
	SliceHeaderExtension    bool

	// StRPSBits is the size of the first slice header's own
	// short_term_ref_pic_set(), 0 when it takes one of the SPS's.
	StRPSBits int
}

AccelPicture is a picture's parameters, the standard's syntax elements and their derived values, named as there.

type AccelRef

type AccelRef struct {
	Surface  int
	POC      int
	LongTerm bool
	// RPS says which list of the picture's reference picture set it is
	// in: RPSStCurrBefore, RPSStCurrAfter, RPSLtCurr, or 0 (kept only for
	// later pictures).
	RPS int
}

AccelRef is a reference picture of the one being decoded.

type AccelSlice

type AccelSlice struct {
	// Data is the NAL unit, header and emulation prevention bytes
	// included; slice_data() starts at DataOffset.
	Data       []byte
	DataOffset int

	SegmentAddr        int
	Dependent          bool
	Type               int // 0 B, 1 P, 2 I
	ColourPlane        int
	SAOLuma, SAOChroma bool
	MvdL1Zero          bool
	CabacInit          bool
	TemporalMVP        bool
	DeblockingDisabled bool
	ColFromL0          bool
	LoopFilterAcross   bool
	ColRefIdx          int
	NumRefIdx          [2]int
	// RefList are the lists, as indexes into the picture's Refs.
	RefList                      [2][]int
	QPDelta                      int
	CbQPOffset, CrQPOffset       int
	BetaOffsetDiv2, TcOffsetDiv2 int
	MaxMergeCand                 int
	// With explicit weighted prediction.
	Log2WeightDenom, Log2WeightDenomC int
	Weights                           [2][]AccelWeight
}

AccelSlice is a slice segment: its NAL unit and its header.

type AccelWeight

type AccelWeight struct {
	LumaWeight, LumaOffset     int
	ChromaWeight, ChromaOffset [2]int
}

AccelWeight is a reference's explicit weights, offsets at 8 bits.

type Decoder

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

Decoder decodes an H.265/HEVC elementary stream, access unit by access unit (or any run of whole NAL units).

func New

func New() *Decoder

New makes a decoder.

func (*Decoder) Decode

func (d *Decoder) Decode(au []byte, pts int64, out func(*Picture) error) error

Decode decodes the NAL units of an access unit given at pts, giving out the pictures that are due, in output order.

func (*Decoder) Errors

func (d *Decoder) Errors() int

Errors is how many slices could not be decoded.

func (*Decoder) Flush

func (d *Decoder) Flush(out func(*Picture) error) error

Flush finishes the last picture and gives out every picture still due.

func (*Decoder) SetAccel

func (d *Decoder) SetAccel(a Accel)

SetAccel makes the decoder decode with a, before the first picture.

func (*Decoder) SetLayers added in v0.12.0

func (d *Decoder) SetLayers(n int)

SetLayers sets how many layers are decoded: 1, the base layer (the default; the others are skipped), or 2, MV-HEVC's two views, whose pictures come out with their Layer. Before decoding.

func (*Decoder) SetThreads

func (d *Decoder) SetThreads(n int)

SetThreads sets how many goroutines decode: the slices of a picture, and pictures at once (half as many, up to 8; each holds a picture and some state). Not while decoding.

type Encoder added in v0.11.0

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

Encoder codes pictures as an HEVC stream: an IDR picture every Keyint pictures, then groups of BFrames + 1 pictures each coded as an anchor P picture followed by its B pictures in a hierarchy, each predicted from the pictures around it.

Pictures are coded in a pipeline: a picture starts as soon as the pictures it predicts from are decoded (by the decoder in the loop, which gets the access units in decoding order), several at once, each on a wavefront of CTB rows. In decoding order each anchor precedes the B pictures of the group before it, so the anchors, which only need each other, run ahead while the B pictures fill in.

func NewEncoder added in v0.11.0

func NewEncoder(o EncoderOptions) (*Encoder, error)

NewEncoder makes an encoder for the options.

func (*Encoder) Encode added in v0.11.0

func (e *Encoder) Encode(src *Source) ([][]byte, error)

Encode takes a picture, returning the access units ready: a group's once it is coded, in decoding order, the parameter sets before an IDR picture. It blocks while the pipeline is full.

func (*Encoder) EncodeStereo added in v0.12.0

func (e *Encoder) EncodeStereo(left, right *Source) ([][]byte, error)

EncodeStereo takes a picture of two views (an encoder of Views 2): each access unit holds the left view's picture and the right's.

func (*Encoder) Flush added in v0.11.0

func (e *Encoder) Flush() ([][]byte, error)

Flush codes the pictures waiting and ends the stream, returning the access units left; with the verification, it checks that the stream decoded to the encoder's pictures, all of them, in display order.

func (*Encoder) Stats added in v0.11.0

func (e *Encoder) Stats() EncoderStats

Stats reports what the encoder has done.

type EncoderOptions added in v0.11.0

type EncoderOptions struct {
	Width, Height int
	BitDepth      int // 8 or 10
	QP            int // 0..51, the I pictures'; the others code higher
	FPSNum        int
	FPSDen        int
	// Keyint is the IDR picture interval; 0 picks 250, 1 codes intra only.
	Keyint int
	// BFrames is how many B pictures come between anchors: 0, 1, 3 or 7
	// (a hierarchy); -1 picks 7, smaller and with more pictures to code
	// at once than 3, whose 6-picture buffer at 1080p keeps to level 4.1
	// (7 needs 10, level 5).
	BFrames int
	// Refs is how many pictures before it a P picture may predict from;
	// 0 picks 2.
	Refs int
	// Threads is how many CTB rows of a picture are coded at once; 0
	// picks the CPUs.
	Threads int
	// Pipeline is how many pictures are coded at once; 0 picks by the
	// threads.
	Pipeline int
	// NoVerify leaves out the decoding of every access unit written, which
	// checks that the stream decodes to the encoder's pictures.
	NoVerify bool
	// Rect tries the rectangular partitions (2NxN, Nx2N) of inter CUs:
	// 1.7% smaller output for 1.8 times the work, off unless asked.
	Rect bool
	// QPOffsetP and QPOffsetB are how much above QP the anchor P pictures
	// and the first level of B pictures code (each deeper level one more);
	// 0 picks 4 and 8, the cascade x265 uses, which measured best.
	QPOffsetP, QPOffsetB int
	// Views is 2 for MV-HEVC: EncodeStereo takes the left view, coded as
	// the base layer, and the right, a second layer predicted from it.
	Views int
	// Primaries, Transfer and Matrix are the colour description the
	// stream states (H.273); 0 or 2 for unspecified.
	Primaries, Transfer, Matrix int
	FullRange                   bool
}

EncoderOptions configures an Encoder.

type EncoderStats added in v0.11.0

type EncoderStats struct {
	Pictures int
	Bytes    int64
	SSE      [3]float64
	Samples  [3]float64
}

EncoderStats is what an encoder has done: pictures and bytes written, and the squared error of its reconstructions per plane, for a PSNR.

func (*EncoderStats) PSNR added in v0.11.0

func (s *EncoderStats) PSNR(ci, bitDepth int) float64

PSNR of plane ci (0 luma, 1 Cb, 2 Cr) of the pictures coded, in dB.

type Picture

type Picture struct {
	Width, Height    int
	BitDepth         int
	Y, Cb, Cr        []uint16 // from the window's top left
	StrideY, StrideC int
	PTS              int64
	// FrameRateNum and FrameRateDen are the VUI's timing (0 if absent).
	FrameRateNum, FrameRateDen int
	// Primaries, Transfer and Matrix are the VUI's colour description
	// (H.273 code points; 2, unspecified, when there is none).
	Primaries, Transfer, Matrix int
	FullRange                   bool
	// Left and Top are the window's offset in the coded picture.
	Left, Top int
	// Layer is 0 for the base layer, 1 for MV-HEVC's second view (when
	// the decoder is told to decode it, SetLayers).
	Layer int
}

Picture is a decoded picture in output order: 4:2:0 planes of samples (uint16 whatever the bit depth), its conformance window applied by Width, Height and the planes' offsets.

func (*Picture) Pack

func (p *Picture) Pack(y, uv []byte, pitch, r0, r1 int)

Pack writes rows [r0, r1) of the picture (r0 even) as an encoder takes it: NV12 at 8 bits, else P010 (two bytes a sample, little-endian, the value in the top bits), a luma plane y and an interleaved Cb/Cr plane uv, each row pitch bytes apart. Rows may be packed in bands concurrently.

type ScalingLists

type ScalingLists struct {
	L4      [6][16]uint8
	L8, L16 [6][64]uint8
	L32     [2][64]uint8
	DC16    [6]uint8
	DC32    [2]uint8
}

ScalingLists are the scaling factors in raster order: ScalingList of the standard by size; 32x32's matrices 0 and 1 are its matrixId 0 and 3.

type Source added in v0.11.0

type Source struct {
	Y, Cb, Cr        []uint16
	StrideY, StrideC int
}

Source is a picture to encode: 4:2:0 planes of samples (uint16 whatever the bit depth) of the encoder's picture size.

Jump to

Keyboard shortcuts

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