videocodec

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: BSD-2-Clause Imports: 7 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 videocodec binds the native video codecs a WebRTC peer encodes and decodes with: libvpx for VP8, and OpenH264 for H.264. Both are loaded at run time through purego from the shared libraries a distribution installs, so the build needs no C toolchain.

The frames the codecs exchange are I420 (planar YUV 4:2:0), converted to and from packed RGB by I420ToRGB and RGBToI420.

Index

Constants

View Source
const (
	DefaultBitrate = 500000
	MinBitrate     = 250000
	MaxBitrate     = 1500000
)

The VP8 encoder's bitrates, in bits per second: the one it starts at, and the range a receiver's estimate is held to.

Variables

View Source
var (
	// ErrVPXNotAvailable is returned when libvpx cannot be loaded, so a caller
	// can tell a missing install from a failure of the codec.
	ErrVPXNotAvailable = errors.New("videocodec: libvpx not available")
	// ErrOpenH264NotAvailable is returned when libopenh264 cannot be loaded.
	ErrOpenH264NotAvailable = errors.New("videocodec: libopenh264 not available")
)

Errors from loading the libraries.

Functions

func I420ToRGB

func I420ToRGB(f *I420Frame) []byte

I420ToRGB converts a picture to packed 8-bit RGB, three bytes a pixel, with the BT.601 limited-range coefficients.

Types

type H264Decoder

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

H264Decoder decodes H.264 access units, in Annex B form, to I420 with OpenH264. It is safe for concurrent use.

func NewH264Decoder

func NewH264Decoder(libPath string) (*H264Decoder, error)

NewH264Decoder builds an H.264 decoder over libopenh264, loaded from libPath or by its conventional names when libPath is empty.

func (*H264Decoder) Close

func (d *H264Decoder) Close()

Close frees the decoder.

func (*H264Decoder) Decode

func (d *H264Decoder) Decode(data []byte) ([]*I420Frame, error)

Decode decodes one access unit and returns the picture it completes, if any, copied out of the decoder.

type I420Frame

type I420Frame struct {
	Width, Height int
	Y, U, V       []byte
}

I420Frame is a picture in I420: a full-resolution luma plane followed by two chroma planes at half the width and height, rounded up. The rows of each plane are packed, with no padding.

func NewI420Frame

func NewI420Frame(width, height int) *I420Frame

NewI420Frame allocates a black picture of the given size.

func RGBToI420

func RGBToI420(rgb []byte, width, height int) (*I420Frame, error)

RGBToI420 converts packed 8-bit RGB, three bytes a pixel, to a picture, with the BT.601 limited-range coefficients. Each chroma sample is taken from the average of the pixels it covers.

func (*I420Frame) Planes

func (f *I420Frame) Planes() [3][]byte

Planes returns the luma and the two chroma planes.

func (*I420Frame) Stride

func (f *I420Frame) Stride(p int) int

Stride is the size of a row of plane p.

type VP8Decoder

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

VP8Decoder decodes VP8 frames to I420 with libvpx. It is safe for concurrent use.

func NewVP8Decoder

func NewVP8Decoder(libPath string) (*VP8Decoder, error)

NewVP8Decoder builds a VP8 decoder over libvpx, loaded from libPath or by its conventional names when libPath is empty.

func (*VP8Decoder) Close

func (d *VP8Decoder) Close()

Close frees the decoder.

func (*VP8Decoder) Decode

func (d *VP8Decoder) Decode(data []byte) ([]*I420Frame, error)

Decode decodes one compressed frame and returns the pictures it yields, copied out of the decoder.

type VP8Encoder

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

VP8Encoder encodes I420 frames to VP8 with libvpx, configured for live, constant-bitrate video. It is safe for concurrent use.

func NewVP8Encoder

func NewVP8Encoder(libPath string) (*VP8Encoder, error)

NewVP8Encoder builds a VP8 encoder over libvpx, loaded from libPath or by its conventional names when libPath is empty. The codec itself is created with the first frame, at that frame's size.

func (*VP8Encoder) Close

func (e *VP8Encoder) Close()

Close frees the codec.

func (*VP8Encoder) Encode

func (e *VP8Encoder) Encode(frame *I420Frame, pts int64, forceKeyframe bool) ([]byte, error)

Encode encodes one frame, presented at pts on the 90 kHz video clock, and returns the frame's compressed data. forceKeyframe makes it a keyframe. The codec is created again when the frame size changes.

func (*VP8Encoder) SetTargetBitrate

func (e *VP8Encoder) SetTargetBitrate(bitrate int)

SetTargetBitrate changes the bitrate the encoder aims for, held between MinBitrate and MaxBitrate. It applies from the next frame.

func (*VP8Encoder) TargetBitrate

func (e *VP8Encoder) TargetBitrate() int

TargetBitrate is the bitrate the encoder aims for, in bits per second.

Jump to

Keyboard shortcuts

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