codec

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 8 Imported by: ? 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 codec provides low-latency video encoding, decoding and hardware capability probing on top of NSGo Media's bundled FFmpeg libraries.

Encoders are configured for interactive streaming: no B-frames, low-delay settings, on-demand keyframes with inline parameter sets (Annex B), and dynamic bit-rate updates. Decoders prefer platform hardware acceleration and fall back to software automatically.

Index

Constants

View Source
const (
	H264 = "h264"
)

Codec names match FFmpeg's short codec names, for use in capability negotiation handshakes.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccessUnit

type AccessUnit struct {
	Data     []byte
	Keyframe bool
	PTS      int64
}

AccessUnit is one encoded video frame in Annex B format. Keyframes carry inline SPS/PPS so a decoder can recover immediately after dropped frames.

type Capability

type Capability struct {
	// Codec is the standard codec name, such as H264 ("h264").
	Codec string
	// Name is the underlying FFmpeg codec or hardware backend name
	// (e.g. "h264_videotoolbox", "h264_nvenc", "h264_vaapi", "libopenh264").
	Name string
	// Hardware reports whether the implementation uses hardware acceleration.
	Hardware bool
}

Capability describes an encoder or decoder available on this machine.

func Decoders

func Decoders() ([]Capability, error)

Decoders probes the machine's available video decoders, ordered with hardware acceleration first and software fallback after.

func Encoders

func Encoders() ([]Capability, error)

Encoders probes the machine's available video encoders, ordered with preferred hardware encoders first and software fallbacks after. Each reported encoder has been verified to open on the current system.

type Decoder

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

Decoder decodes Annex B video access units into RGBA images ready for an NSGo view (core.ImageSource{Pixels: img}). It prefers hardware decoding and falls back to software automatically on failure.

func NewDecoder

func NewDecoder(cfg DecoderConfig) (*Decoder, error)

NewDecoder opens a low-latency video decoder, preferring the platform's hardware decoder and falling back to software when unavailable.

func (*Decoder) Close

func (d *Decoder) Close() error

Close releases the decoder and its FFmpeg resources.

func (*Decoder) Decode

func (d *Decoder) Decode(unit []byte) (*image.RGBA, error)

Decode feeds one Annex B access unit and returns the decoded picture as an *image.RGBA, or (nil, nil) if no picture is ready yet.

func (*Decoder) Flush

func (d *Decoder) Flush()

Flush drops buffered frames inside the decoder (e.g. after packet loss before feeding a fresh keyframe).

func (*Decoder) Hardware

func (d *Decoder) Hardware() bool

Hardware reports whether the decoder is currently using hardware acceleration.

type DecoderConfig

type DecoderConfig struct {
	// Codec is the stream codec name; defaults to H264 when empty.
	Codec string
	// SoftwareOnly skips hardware acceleration and decodes in software.
	SoftwareOnly bool
}

DecoderConfig configures a low-latency video decoder.

type Encoder

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

Encoder encodes uncompressed BGRA or NV12 frames into low-latency video access units without B-frames.

func NewEncoder

func NewEncoder(cfg EncoderConfig) (*Encoder, error)

NewEncoder opens a low-latency video encoder for cfg, preferring hardware encoders and falling back to available alternatives unless cfg.Name or cfg.SoftwareOnly restricts the choice.

func (*Encoder) Close

func (e *Encoder) Close() error

Close releases the encoder and its FFmpeg resources.

func (*Encoder) Encode

func (e *Encoder) Encode(frame Frame, keyframe bool) (AccessUnit, error)

Encode encodes frame into a single AccessUnit. Setting keyframe forces an IDR keyframe with inline SPS/PPS so a remote decoder can recover after packet or frame loss.

func (*Encoder) Hardware

func (e *Encoder) Hardware() bool

Hardware reports whether the opened encoder uses hardware acceleration.

func (*Encoder) Name

func (e *Encoder) Name() string

Name returns the FFmpeg encoder backend name (e.g. "h264_videotoolbox").

func (*Encoder) SetBitRate

func (e *Encoder) SetBitRate(bps int) error

SetBitRate changes the target bit rate (in bits per second) while running.

type EncoderConfig

type EncoderConfig struct {
	// Codec is the target codec name; defaults to H264 when empty.
	Codec string
	// Width and Height are the picture dimensions in pixels (must be positive
	// and even for 4:2:0 encoding).
	Width  int
	Height int
	// FPS is the nominal frame rate used for rate control; defaults to 60.
	FPS int
	// BitRate is the target bit rate in bits per second; defaults to 8 Mbps.
	BitRate int
	// SoftwareOnly skips hardware encoders and opens a software encoder.
	SoftwareOnly bool
	// Name, when non-empty, forces a specific FFmpeg encoder backend
	// (e.g. "h264_nvenc", "h264_mf", "libopenh264").
	Name string
}

EncoderConfig configures a low-latency video encoder.

type Frame

type Frame struct {
	Format PixelFormat
	Width  int
	Height int
	// Data holds BGRA pixels or, for NV12, the Y plane (and optionally the
	// interleaved UV plane immediately after Stride*Height when UV is nil).
	Data []byte
	// Stride is the byte stride of Data rows. When zero, it defaults to
	// Width*4 for BGRA and Width for NV12.
	Stride int
	// UV optionally holds the NV12 interleaved chroma plane separately from
	// Data. Ignored for BGRA.
	UV []byte
	// UVStride is the byte stride of UV rows. When zero, it defaults to
	// Stride (or Width).
	UVStride int
}

Frame is an uncompressed video frame passed to Encoder.Encode.

type PixelFormat

type PixelFormat int

PixelFormat identifies the layout of an uncompressed input Frame.

const (
	// BGRA is packed 32-bit BGRA (4 bytes per pixel: B, G, R, A).
	BGRA PixelFormat = iota
	// NV12 is semi-planar 4:2:0 YUV: a full-resolution Y plane followed by
	// a half-resolution interleaved UV plane.
	NV12
)

func (PixelFormat) String

func (f PixelFormat) String() string

String returns the pixel format's name.

Jump to

Keyboard shortcuts

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