Idiomatic Go for Video for Linux 2

Capture frames, control hardware cameras, inspect Media Controller pipelines, and drive V4L2 codecs directly from Go with CGO_ENABLED=0, zero-copy iter.Seq2 streaming, and first-class multi-arch Linux support.

$ go get github.com/vladimirvivien/go4vl/v4l2
// Zero-copy MMAP capture via Go iter.Seq2
dev, err := device.Open("/dev/video0",
    device.WithBufferSize(4),
    device.WithPixFormat(v4l2.PixFormat{
        Width: 1280, Height: 720,
        PixelFormat: v4l2.PixelFmtMJPEG,
    }),
)
defer dev.Close()
dev.Start(ctx)
defer dev.Stop()

for frame, err := range dev.Frames(ctx) {
    process(frame.Data) // slices MMAP memory directly
} // automatically re-queued via VIDIOC_QBUF
// Discontiguous multi-planar (MPLANE) capture
dev, err := device.Open("/dev/video10",
    device.WithBufferType(v4l2.BufTypeVideoCaptureMPlane),
    device.WithBufferSize(4),
    device.WithPixFormatMPlane(v4l2.PixFormatMPlane{
        Width: 1920, Height: 1080,
    }),
)
defer dev.Close()
dev.Start(ctx)

for frame, err := range dev.Frames(ctx) {
    yPlane, uvPlane := frame.Planes[0], frame.Planes[1]
    handleYUV(yPlane, uvPlane)
}
// Inspect /dev/media* topology and /dev/v4l-subdev* pads
mfd, _ := v4l2.OpenDevice("/dev/media0", sys.O_RDWR, 0)
defer v4l2.CloseDevice(mfd)

topo, _ := v4l2.GetMediaTopology(mfd)
fmt.Printf("Entities: %d, Links: %d\n",
    len(topo.Entities), len(topo.Links))

sfd, _ := v4l2.OpenDevice("/dev/v4l-subdev0", sys.O_RDWR, 0)
defer v4l2.CloseDevice(sfd)
fmtPad, _ := v4l2.GetSubdevFormat(sfd, 0, v4l2.SubdevFormatActive)
routes, _ := v4l2.GetSubdevRouting(sfd, v4l2.SubdevFormatActive)
// Bind stateless HEVC controls to a MediaRequest FD
req, _ := v4l2.AllocMediaRequest(mfd)
defer req.Close()

ctrls := v4l2.NewExtControls()
ctrls.AddHEVCSPS(&v4l2.ControlHEVCSPS{
    Pic_width_in_luma_samples:  1920,
    Pic_height_in_luma_samples: 1080,
})
v4l2.TryExtControls(vfd, ctrls)

ctrls.SetRequestFD(req.Fd())
v4l2.SetExtControls(vfd, ctrls)
req.Queue() // commit atomic decode job to hardware

Two Tiers. Zero CGO. Full V4L2 Control.

Start with the ergonomic device package for everyday capture and output, or drop straight to v4l2 when you need direct ioctl control.

Layered Architecture

go4vl separates everyday application ergonomics from low-level kernel ABI details. High-level callers open a *device.Device, negotiate pixel formats with functional options, and stream frames through Go iterators or pooled channels.

Beneath that layer, v4l2 talks directly to /dev/video*, /dev/media*, and /dev/v4l-subdev* through golang.org/x/sys/unix and architecture-specific UAPI bindings generated from Linux kernel headers.

Explore the architecture
go4vl Two-Tier Architecture diagram

Zero-Copy Streaming & Pooled Channels

MMAP Ring Buffer and Frame Delivery Paths diagram

Range directly over memory-mapped kernel buffers using Device.Frames(ctx) (iter.Seq2[*device.Frame, error]). Each iteration waits with v4l2.Poll, yields a slice backed directly by the kernel MMAP ring with zero copies, and automatically re-queues the buffer via VIDIOC_QBUF—even on break or early return.

When your pipeline fans frames across goroutines or multiplexes Go channels with select, use Device.GetFrames() backed by FramePool and call frame.Release() to recycle memory.

Read the streaming & I/O guide

Multi-Planar Formats, Output & DMA-BUF

SoC image signal processors and hardware codecs store luma and chroma channels in separate, non-contiguous memory planes. go4vl handles both single-planar formats and multi-planar MPLANE streams uniformly via WithPixFormatMPlane, per-plane slices in frame.Planes, and WriteFramePlanes.

You can also drive video output devices with WriteFrame, share zero-copy DMA-BUF file descriptors with ExportBuffer and ExportBufferPlane, capture ISP metadata via MetaFormat, and negotiate HDMI EDID blocks.

Explore formats & MPLANE
Single-Planar vs Multi-Planar MPLANE Memory Layout diagram

Media Controller, Sub-devices & Hardware Codecs

Linux Media Controller and Sub-device Pipeline Topology diagram

Embedded cameras expose complex pipelines spanning /dev/media*, /dev/v4l-subdev*, and multiple video nodes. Query full graph topologies with GetMediaTopology, enable media links, negotiate sub-device pad formats and routing tables, and drive hardware codecs through M2MSession, v4l2.Poller, and MediaRequest FDs for H.264, HEVC, VP8, VP9, AV1, MPEG2, and FWHT.

Media Controller & Sub-devices  ·  Codecs & M2M pipelines

Cross-Compile Anywhere Without CGO

Checked-in UAPI definitions (zconsts_linux.go and per-architecture ztypes_linux_*.go) eliminate C toolchains and kernel header dependencies at build time. Cross-compile static Linux binaries directly from Linux, macOS, or Windows.

linux/amd64

Workstations, servers, and x86_64 capture appliances.

linux/arm64

Raspberry Pi 4/5 (64-bit), Jetson, and edge compute nodes.

linux/arm

32-bit Raspberry Pi OS and embedded ARMv7 boards.

linux/riscv64

64-bit RISC-V Linux single-board systems.

# Cross-compile static Linux binaries with CGO_ENABLED=0
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build ./examples/iter_capture
CGO_ENABLED=0 GOOS=linux GOARCH=arm go build ./examples/iter_capture
CGO_ENABLED=0 GOOS=linux GOARCH=riscv64 go build ./examples/iter_capture

See full build & Docker instructions