decode

package
v0.0.0-...-3e1e54c Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 20 Imported by: 1 module, 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

Index

Constants

View Source
const DefaultMaxFrameMacroblocks = 36864
View Source
const DefaultMaxNALBytes = 8 << 20

Variables

View Source
var ErrWaitingForIDR = errors.New("waiting for an IDR picture")

ErrWaitingForIDR means prediction cannot resume until a complete IDR picture arrives. A receiver can use this error to request a new keyframe.

Functions

This section is empty.

Types

type DecodedFrame

type DecodedFrame = frame.Frame

DecodedFrame is an alias for frame.Frame for CLI convenience.

type Decoder

type Decoder struct {
	SPS    map[uint32]*nal.SPS
	PPS    map[uint32]*nal.PPS
	DPB    *frame.DPB
	Frames []*frame.Frame
	// MaxFrames optionally stops Decode after this many complete pictures.
	// Zero means decode all frames.
	MaxFrames int
	// MaxFrameMacroblocks bounds each coded picture before pixel, motion, or
	// macroblock allocation. Zero uses DefaultMaxFrameMacroblocks. This is a
	// resource budget, not a claim of conformance to any H.264 level.
	MaxFrameMacroblocks int

	// TraceMB is optional diagnostic output for first-divergence tooling. Leave
	// nil in normal decode paths to avoid overhead and preserve API behaviour.
	TraceMB func(MBTraceEvent)
	// contains filtered or unexported fields
}

Decoder is an H.264 Annex B bitstream decoder.

func NewDecoder

func NewDecoder() *Decoder

NewDecoder creates a new H.264 decoder.

func (*Decoder) Decode

func (d *Decoder) Decode(data []byte) (frames []*frame.Frame, resultErr error)

Decode accepts a complete Annex B buffer. A picture may contain multiple slices, but an incomplete final picture is an error, not a streaming buffer.

type Frame

type Frame interface {
	// Dimensions
	GetWidth() int
	GetHeight() int

	// Pixel access (x, y in luma coordinates; chroma = luma/2)
	PixelY(x, y int) uint8
	SafePixelY(x, y int) uint8
	PixelU(x, y int) uint8
	PixelV(x, y int) uint8

	// Metadata
	GetPOC() int
	GetFrameNum() int
	IsIDRFrame() bool
	IsRefFrame() bool
}

Frame is a thin read-only interface over a decoded YUV 4:2:0 picture. Callers (cmd/, conformance tests, external consumers) should prefer this interface over *frame.Frame so the internal plane representation can change without breaking callers.

*frame.Frame satisfies Frame via the DecodedFrame alias; callers that already hold *frame.Frame can continue using it or switch to this interface.

type MBTraceEvent

type MBTraceEvent struct {
	NALType           uint8
	FrameNum          int
	SliceType         uint32
	MBAddr, MBX       int
	MBY               int
	EntropyCABAC      bool
	Kind              string
	MBType            uint32
	SubMBType         [4]uint32
	CBP               uint32
	QPDelta           int32
	QP                int
	Skipped           bool
	Use8x8            bool
	ChromaPred        int8
	Intra4x4Mode      [16]int8
	Intra4x4PredMode  [16]int8
	Intra4x4FinalMode [16]int8
	Intra8x8Mode      [4]int8
	Intra8x8PredMode  [4]int8
	Intra8x8LeftEdge  [2]int8
	Intra8x8TopEdge   [2]int8
	RefIdx            [4]int8
	MV                [4]syntax.MotionVector
	SubMV             [16]syntax.MotionVector
	TotalCoeff        [16]int
	ChromaCoeff       [2][4]int
}

MBTraceEvent is a compact macroblock syntax snapshot emitted after a macroblock is decoded but before the optional CABAC termination bit is read. It deliberately records decoded syntax, not reconstructed pixels, so trace consumers can compare CABAC decisions against FFmpeg before chasing prediction or transform drift.

type StreamConfig

type StreamConfig struct {
	// MaxNALBytes bounds one encoded NAL, including its header but excluding
	// Annex B framing. Zero uses DefaultMaxNALBytes.
	MaxNALBytes int
	// MaxFrameMacroblocks has the same meaning as Decoder.MaxFrameMacroblocks.
	MaxFrameMacroblocks int
	// OutputOrder enables presentation-order delivery for progressive frames.
	// Pictures are released when pending output exceeds the SPS reorder bound
	// or DPB storage fills. A zero bound delivers each completed picture
	// immediately; an absent bound is inferred and can retain up to the level's
	// DPB capacity (at most 16). Reference pictures remain available after
	// delivery. Drain ends the sequence and requires a new IDR; false preserves
	// the original decoding-order/segment-drain API.
	OutputOrder bool
}

type StreamDecoder

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

StreamDecoder accepts incremental Annex B input without retaining an output history. Outputs are delivered synchronously in decoding order unless StreamConfig.OutputOrder is enabled. I/P pictures can also need reordering. Each output owns its pixels and metadata and may be modified or retained by the consumer.

Retained storage is bounded by MaxNALBytes, the coded-picture budget and the SPS reference count (at most 16). In output-order mode, the reference/output union is bounded by the level's DPB capacity, plus one coded picture for reconstruction or reuse. Consumer-retained outputs are not included. Methods and the output callback must not call this decoder concurrently or reentrantly.

func NewStreamDecoder

func NewStreamDecoder(config StreamConfig, output func(*frame.Frame) error) (*StreamDecoder, error)

func (*StreamDecoder) DecodeAccessUnit

func (s *StreamDecoder) DecodeAccessUnit(data []byte, tag uint64) (err error)

DecodeAccessUnit accepts one complete Annex B picture, possibly in multiple slices. Parameter-set or filler-only input is also allowed and emits nothing. Each output carries its picture's tag, even when later input releases it; tags on input without a picture are ignored. Zero is a valid tag. MaxNALBytes bounds each NAL, not the whole input. Callers must bound data; temporary parsing storage scales with the number of NALs in the buffer.

Completing an access unit preserves prediction state and any delayed output. Explicit end markers still end the sequence. Input is not retained after return. Decode errors discard prediction state as with Push; previously delivered pictures remain valid. Pending incremental input must first be finished with Drain or discarded with Discontinuity. Calling this method while it is pending returns an error without discarding that input.

func (*StreamDecoder) Discontinuity

func (s *StreamDecoder) Discontinuity()

Discontinuity reports transport loss or an application-level seek. It drops partial input and references but retains parsed SPS/PPS, then waits for IDR. Input errors and callback errors invoke this automatically. The remainder of a failed Push call is discarded; the next Push must begin at a start code. Complete pictures delivered before an error remain valid.

func (*StreamDecoder) Drain

func (s *StreamDecoder) Drain() (err error)

Drain finishes the last NAL and picture, or reports a truncated picture. It retains SPS/PPS and, in decoding-order mode, references for the next segment. In output-order mode it flushes pending pictures in presentation order and ends the sequence: SPS/PPS survive, but a new IDR is required. Do not drain between arbitrary chunks of an output-order sequence; use Push instead. Reference-gap placeholders are never output. Repeated Drain is a no-op.

func (*StreamDecoder) Push

func (s *StreamDecoder) Push(data []byte) (err error)

Push accepts arbitrarily split Annex B bytes, including split start codes. A NAL is consumed when the next start code arrives; Drain consumes the last NAL. Push does not retain the supplied slice after it returns. Pictures decoded through Push have tag zero.

func (*StreamDecoder) Reset

func (s *StreamDecoder) Reset()

Reset drops all input, parameter sets and reference state. Configuration and the output callback are preserved. The next sequence must provide SPS/PPS/IDR.

func (*StreamDecoder) WaitingForIDR

func (s *StreamDecoder) WaitingForIDR() bool

Jump to

Keyboard shortcuts

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