decode

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 25 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

Overview

Package decode turns a file path into interleaved float32 PCM frames.

A codec is added by writing one file that implements Factory (and optionally Profile, Seeker, Prober, ReaderOpener) plus an init that calls Register. The dispatch logic in this file is deliberately codec-agnostic.

Index

Constants

This section is empty.

Variables

View Source
var Default = NewRegistry()

Default is the registry populated by the init functions in this package.

View Source
var ErrClosed = errors.New("decode: decoder is closed")

ErrClosed is returned by a decoder that is used after Close. Callers treat it as a programming error rather than end of stream.

View Source
var ErrEndOfStream = io.EOF

ErrEndOfStream is the sentinel a decoder returns once the last frame has been delivered. It is an alias of io.EOF so callers can use either spelling.

View Source
var ErrNotSeekable = errors.New("decode: source is not seekable")

ErrNotSeekable means the decoder cannot reposition on this source. A decoder returns it from SeekFrame for a forward-only stream (a network body or a pipe), so the streamer can fall back to reopening and discarding instead of failing the track.

View Source
var ErrUnknownCodec = errors.New("decode: unknown codec")

ErrUnknownCodec means OpenNamed was given a name no factory registered.

View Source
var ErrUnsupported = errors.New("decode: unsupported format")

ErrUnsupported means no registered factory recognised the file, either by extension or by content sniffing.

Functions

func Describe

func Describe(d Decoder) (decoder, parser string)

Describe returns the decoder and parser labels when d implements Descriptor, and empty strings otherwise. Test fakes and future codecs are free to omit it.

func ProfileOf

func ProfileOf(f Factory) (friendly string, weight int)

ProfileOf reports a factory's profile, or zero values when it has none.

func Register

func Register(f Factory)

Register adds a factory to the process-wide Default registry.

Types

type AacFactory

type AacFactory struct{}

AacFactory decodes AAC-LC in ADTS without cgo.

func NewAacFactory

func NewAacFactory() *AacFactory

NewAacFactory returns a factory for the AAC-LC in ADTS decoder.

func (*AacFactory) Exts

func (f *AacFactory) Exts() []string

func (*AacFactory) FriendlyName

func (f *AacFactory) FriendlyName() string

FriendlyName is the label a UI shows for this codec. It repeats the family name deliberately: the picker renders "{family} {friendly}" but suppresses the repetition, so "Aac" reads as one word rather than "Aac Aac". Unlike Opus, where the label names a property ("Portable"), there is no second AAC variant to distinguish, so no qualifier is added.

func (*AacFactory) Match

func (f *AacFactory) Match(magic []byte) bool

Match claims an ADTS syncword. The 12-bit sync is 0xFFF, so the first byte must be 0xFF and the second byte's high nibble must be 0xF. The mask 0xF6 additionally requires the two MPEG layer bits (byte 1, bits 2 and 1) to be zero, which ADTS mandates; it deliberately ignores the MPEG version bit and the protection bit, which legitimately vary between valid streams.

A match needs two bytes, so a lone 0xFF is never claimed, and MPEG audio frames set the layer bits non-zero: 0xFF 0xFB (Layer III) and its siblings fall out of the mask and are left to the codec that owns them.

func (*AacFactory) Name

func (f *AacFactory) Name() string

func (*AacFactory) Open

func (f *AacFactory) Open(path string) (Decoder, error)

func (*AacFactory) Probe

func (f *AacFactory) Probe(path string, opts ProbeOptions) (core.StreamInfo, error)

Probe reports the stream shape. DurationUnknown reads only the first header, so it stays as cheap as an open. DurationProbe scans the 7-byte headers the decoder indexes, which yields the exact frame count without decoding a sample; it is still far cheaper than draining the file.

func (*AacFactory) Weight

func (f *AacFactory) Weight() int

Weight only breaks ties between codecs that claim the same extension. No other factory claims .aac, so this value never competes.

type Codec

type Codec struct {
	Name         string // "opus"  <- configuration key
	FriendlyName string // "Portable"   <- UI label
	Weight       int    // 90
	Exts         []string

	// Default is true when this codec wins automatic selection for at least
	// one of the extensions it claims. Selection is per-path, so more than one
	// codec can be a default at once: a codec can win one extension and lose
	// another.
	Default bool
}

Codec describes one registered factory for a UI that has to offer a choice.

type Decoder

type Decoder interface {
	Info() core.StreamInfo

	// ReadFrames fills dst with whole interleaved frames and returns how many
	// it wrote. dst is consumed left to right; a short read is not an error.
	ReadFrames(dst []float32) (frames int, err error)

	Close() error
}

Decoder is the streaming contract every codec satisfies. Implementations always emit interleaved float32 in the format reported by Info, regardless of the source layout.

type Descriptor

type Descriptor interface {
	DecoderName() string
	ParserName() string
}

Descriptor is implemented by decoders that can name the pieces they are built from, for diagnostics. Both labels are free-form and for display.

type Factory

type Factory interface {
	Name() string
	Exts() []string
	Match(magic []byte) bool
	Open(path string) (Decoder, error)
}

Factory is the registration unit for a codec. Match is a cheap content check used to resolve extension conflicts, so it must not open or seek the file.

type FlacFactory

type FlacFactory struct{}

FlacFactory decodes native FLAC without cgo.

func NewFlacFactory

func NewFlacFactory() *FlacFactory

NewFlacFactory returns a factory for the lossless FLAC decoder.

func (*FlacFactory) Exts

func (f *FlacFactory) Exts() []string

func (*FlacFactory) FriendlyName

func (f *FlacFactory) FriendlyName() string

FriendlyName is the label a UI shows for this codec. FLAC is the lossless choice, which is the one property that distinguishes it from every Opus variant.

func (*FlacFactory) Match

func (f *FlacFactory) Match(magic []byte) bool

Match claims the native FLAC marker. The Ogg FLAC mapping starts with "OggS" and is deliberately not claimed: go-flac rejects it, so pretending to support it would turn a clear "unsupported" into a confusing decode error.

func (*FlacFactory) Name

func (f *FlacFactory) Name() string

func (*FlacFactory) Open

func (f *FlacFactory) Open(path string) (Decoder, error)

func (*FlacFactory) Probe

func (f *FlacFactory) Probe(path string, _ ProbeOptions) (core.StreamInfo, error)

Probe reads the STREAMINFO and reports the stream shape. It never decodes audio, so even DurationScan stays cheap: FLAC's total is a header field.

func (*FlacFactory) Weight

func (f *FlacFactory) Weight() int

Weight only breaks ties between codecs that claim the same extension. No other factory claims .flac, so this value never competes.

type LibopusfileFactory

type LibopusfileFactory struct{}

LibopusfileFactory decodes Ogg Opus through the native library.

func NewLibopusfileFactory

func NewLibopusfileFactory() *LibopusfileFactory

NewLibopusfileFactory returns a factory for the native Ogg Opus decoder.

func (*LibopusfileFactory) Exts

func (f *LibopusfileFactory) Exts() []string

func (*LibopusfileFactory) FriendlyName

func (f *LibopusfileFactory) FriendlyName() string

FriendlyName is the label a UI shows for this codec.

func (*LibopusfileFactory) Match

func (f *LibopusfileFactory) Match(magic []byte) bool

Match claims any Ogg stream, the same way the pure-Go factory does. Content sniffing cannot distinguish Opus from Vorbis without reading pages, so Open is what actually validates the payload.

func (*LibopusfileFactory) Name

func (f *LibopusfileFactory) Name() string

func (*LibopusfileFactory) Open

func (f *LibopusfileFactory) Open(path string) (Decoder, error)

func (*LibopusfileFactory) Probe

func (f *LibopusfileFactory) Probe(path string, opts ProbeOptions) (core.StreamInfo, error)

Probe opens the file just long enough to read its frame total. DurationUnknown short-circuits before touching the library so the cheapest mode stays cheap.

func (*LibopusfileFactory) Weight

func (f *LibopusfileFactory) Weight() int

Weight leaves this below the pure-Go factory as the automatic default; the native library is faster but requires a runtime dependency.

type M4aFactory

type M4aFactory struct{}

M4aFactory decodes AAC-LC audio from an MP4/M4A container.

func NewM4aFactory

func NewM4aFactory() *M4aFactory

NewM4aFactory returns a factory for the AAC-in-MP4 decoder.

func (*M4aFactory) Exts

func (f *M4aFactory) Exts() []string

Exts claims both container spellings. An .mp4 audio file has the same ISO-BMFF structure and the same ftyp brand box as an .m4a, so the same decoder handles it.

func (*M4aFactory) FriendlyName

func (f *M4aFactory) FriendlyName() string

FriendlyName is the label a UI shows for this codec.

func (*M4aFactory) Match

func (f *M4aFactory) Match(magic []byte) bool

Match claims an ISO base media file. Such a file begins with a 4-byte big-endian box size followed by the box type, so the "ftyp" brand box type starts at offset 4, not 0: bytes 0..3 are the size field. Checking offset 0 would reject every real MP4, and a file whose first bytes happened to spell "ftyp" would not be one.

func (*M4aFactory) Name

func (f *M4aFactory) Name() string

func (*M4aFactory) Open

func (f *M4aFactory) Open(path string) (Decoder, error)

func (*M4aFactory) Probe

func (f *M4aFactory) Probe(path string, _ ProbeOptions) (core.StreamInfo, error)

Probe reads the container metadata and reports the stream shape. It never decodes audio, so even DurationScan stays cheap: the total comes from the movie header and the edit list.

func (*M4aFactory) Weight

func (f *M4aFactory) Weight() int

Weight breaks ties against another codec registered for the same extension, the same role the FLAC factory's 90 plays.

type Mp3Factory

type Mp3Factory struct{}

Mp3Factory decodes MPEG audio without cgo.

func NewMp3Factory

func NewMp3Factory() *Mp3Factory

NewMp3Factory returns a factory for the MPEG audio decoder.

func (*Mp3Factory) Exts

func (f *Mp3Factory) Exts() []string

func (*Mp3Factory) FriendlyName

func (f *Mp3Factory) FriendlyName() string

FriendlyName is the label a UI shows for this codec.

func (*Mp3Factory) Match

func (f *Mp3Factory) Match(magic []byte) bool

Match claims an MPEG-1/2/2.5 Layer III frame sync, and also a leading ID3v2 tag. A tagged file starts with "ID3" and reaches its first frame sync only after the tag body, so a magic-only check that demanded a sync would reject every tagged file: the extension would carry it, but a file with no .mp3 suffix would fall through sniffing and be unrecognized. Claiming "ID3" lets the decoder itself skip the tag and find the audio.

func (*Mp3Factory) Name

func (f *Mp3Factory) Name() string

func (*Mp3Factory) Open

func (f *Mp3Factory) Open(path string) (Decoder, error)

func (*Mp3Factory) Probe

func (f *Mp3Factory) Probe(path string, _ ProbeOptions) (core.StreamInfo, error)

Probe reads the first frame and its Xing/Info or VBRI tag without decoding audio, so DurationScan stays cheap. The total is exact when a tag carries a frame count (the fixture's Info tag does) or when a constant-bitrate stream's byte length divides by its first frame size; go-mp3 fills TotalSamples with whichever applies and leaves it zero when neither can be determined, in which case this reports -1 rather than a guess.

func (*Mp3Factory) Weight

func (f *Mp3Factory) Weight() int

Weight only breaks ties between codecs that claim the same extension. No other factory claims .mp3, so this value never competes.

type OpusFactory added in v1.1.0

type OpusFactory struct{}

OpusFactory decodes Ogg Opus in pure Go.

func NewOpusFactory added in v1.1.0

func NewOpusFactory() *OpusFactory

NewOpusFactory returns a factory for the pure-Go Ogg Opus decoder.

func (*OpusFactory) Exts added in v1.1.0

func (f *OpusFactory) Exts() []string

func (*OpusFactory) FriendlyName added in v1.1.0

func (f *OpusFactory) FriendlyName() string

FriendlyName is the label a UI shows for this codec.

func (*OpusFactory) Match added in v1.1.0

func (f *OpusFactory) Match(magic []byte) bool

Match accepts any Ogg stream; Open then rejects non-Opus payloads. Checking deeper would mean seeking, which sniffing must not do.

func (*OpusFactory) Name added in v1.1.0

func (f *OpusFactory) Name() string

func (*OpusFactory) Open added in v1.1.0

func (f *OpusFactory) Open(path string) (Decoder, error)

func (*OpusFactory) OpenReader added in v1.1.0

func (f *OpusFactory) OpenReader(r io.Reader) (Decoder, error)

OpenReader decodes from a stream rather than a path. A reader that can seek keeps native seek; one that cannot plays forward but refuses to reposition.

func (*OpusFactory) Probe added in v1.1.0

func (f *OpusFactory) Probe(path string, opts ProbeOptions) (core.StreamInfo, error)

Probe reads the tail of the file for the final granule position. It never decodes audio, so even a probe on a large file stays cheap.

func (*OpusFactory) Weight added in v1.1.0

func (f *OpusFactory) Weight() int

Weight makes this the automatic default over libopusfile: it needs no runtime shared library.

type ProbeOptions

type ProbeOptions struct {
	Duration core.DurationMode
}

ProbeOptions tells a Prober how much work it may do. DurationUnknown must stay cheap and may leave TotalFrames at -1.

type Prober

type Prober interface {
	Probe(path string, opts ProbeOptions) (core.StreamInfo, error)
}

Prober reports stream metadata without producing PCM. It is separate from Factory and Decoder because a probe must not have to decode audio.

type Profile

type Profile interface {
	FriendlyName() string // "Portable"
	Weight() int          // higher wins the automatic default
}

Profile is implemented by factories that describe themselves for a UI. Both values are optional: a factory without it still works and is listed by Name, with an empty label and weight 0.

type ReaderOpener

type ReaderOpener interface {
	OpenReader(r io.Reader) (Decoder, error)
}

ReaderOpener is implemented by pure-Go decoders that can work from any stream. Path-based codecs (FFI wrappers, for example) simply omit it.

type Registry

type Registry interface {
	Register(f Factory)
	Open(path string) (Decoder, error)

	// OpenNamed opens a path through the named factory, bypassing weight
	// entirely. An empty name means automatic selection, identical to Open.
	OpenNamed(name, path string) (Decoder, error)

	// OpenReader opens a decoder over a stream with no path, choosing a
	// factory by content sniffing. Only factories that implement ReaderOpener
	// are eligible; a stream no such factory claims is ErrUnsupported. A source
	// that is seekable keeps native seek, because the reader is handed to the
	// factory unchanged.
	OpenReader(r io.Reader) (Decoder, error)

	// OpenReaderNamed is OpenReader through the named factory, bypassing
	// sniffing. A name that does not implement ReaderOpener is ErrUnknownCodec.
	OpenReaderNamed(name string, r io.Reader) (Decoder, error)

	Probe(path string) (Prober, bool)
	Supported() []string

	// Codecs lists the registered factories for a UI, sorted by weight
	// descending and then by registration order.
	Codecs() []Codec
}

Registry dispatches paths to the factories registered at init time.

func NewRegistry

func NewRegistry() Registry

NewRegistry returns an empty registry. Codec packages register into the package-level Default instead; this exists for tests and for embedders that want an isolated set of decoders.

type Seeker

type Seeker interface {
	SeekFrame(frame int64) error
}

Seeker is implemented by decoders that can jump natively. Decoders without it rely on the streamer's reopen-and-discard fallback.

type WavFactory

type WavFactory struct{}

WavFactory decodes RIFF, RF64 and BW64 WAVE streams without cgo.

func NewWavFactory

func NewWavFactory() *WavFactory

NewWavFactory returns a factory for the uncompressed WAV decoder.

func (*WavFactory) Exts

func (f *WavFactory) Exts() []string

func (*WavFactory) FriendlyName

func (f *WavFactory) FriendlyName() string

FriendlyName is the label a UI shows for this codec. WAV is the uncompressed choice, which is the one property that distinguishes it from every compressed variant.

func (*WavFactory) Match

func (f *WavFactory) Match(magic []byte) bool

Match claims the three WAVE container magics. The first four bytes name the RIFF flavour but say nothing about the form inside it: "RIFF" also introduces an AVI, a WebP or a plain RIFF palette, and handing one of those to go-wav would turn a clear "unsupported" into a confusing decode error. The form type at offset 8 is what makes it WAVE, so it is required too. The length gate is offset 12, the same window wav.Sniff uses; a shorter slice cannot carry the form type and is not claimed.

func (*WavFactory) Name

func (f *WavFactory) Name() string

func (*WavFactory) Open

func (f *WavFactory) Open(path string) (Decoder, error)

func (*WavFactory) Probe

func (f *WavFactory) Probe(path string, _ ProbeOptions) (core.StreamInfo, error)

Probe reads the RIFF header and reports the stream shape. It never decodes audio: go-wav derives the frame count from the data chunk size, so even DurationScan stays cheap.

func (*WavFactory) Weight

func (f *WavFactory) Weight() int

Weight only breaks ties between codecs that claim the same extension. No other factory claims .wav, so this value never competes.

type WebMOpusFactory

type WebMOpusFactory struct{}

WebMOpusFactory reads Opus audio out of a WebM/Matroska container.

func NewWebMOpusFactory

func NewWebMOpusFactory() *WebMOpusFactory

NewWebMOpusFactory returns a factory for the WebM/Matroska Opus decoder.

func (*WebMOpusFactory) Exts

func (f *WebMOpusFactory) Exts() []string

func (*WebMOpusFactory) FriendlyName

func (f *WebMOpusFactory) FriendlyName() string

FriendlyName is the label a UI shows for this codec.

func (*WebMOpusFactory) Match

func (f *WebMOpusFactory) Match(magic []byte) bool

Match accepts any EBML stream. Open then rejects a non-WebM DocType or a missing Opus track. Checking deeper would mean seeking, which sniffing must not do.

func (*WebMOpusFactory) Name

func (f *WebMOpusFactory) Name() string

func (*WebMOpusFactory) Open

func (f *WebMOpusFactory) Open(path string) (Decoder, error)

func (*WebMOpusFactory) OpenReader

func (f *WebMOpusFactory) OpenReader(r io.Reader) (Decoder, error)

OpenReader decodes from a stream. A reader that can seek keeps native seek; one that cannot plays forward but refuses to reposition.

func (*WebMOpusFactory) Probe

func (f *WebMOpusFactory) Probe(path string, opts ProbeOptions) (core.StreamInfo, error)

Probe reads the Duration element in Info, which sits near the front of a WebM file, so DurationProbe is a small read rather than a tail walk. DurationScan walks the clusters when Info has no usable duration.

func (*WebMOpusFactory) Weight

func (f *WebMOpusFactory) Weight() int

Weight sits above the Ogg Opus factory for .webm and .weba, so a WebM file uses this reader; the extensions do not overlap, so it does not disturb the Ogg default.

Jump to

Keyboard shortcuts

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