Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 51 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Video and audio decoders for [avkit](https://github.com/go-avkit/avkit), pure Go
no cgo. Video: **H.264** (ITU-T H.264 | ISO/IEC 14496-10) and **HEVC / H.265**
(ITU-T H.265 | ISO/IEC 23008-2), Main and Main 10 in 4:2:0, and since v0.15.0 every
chroma format (4:0:0, 4:2:2, 4:4:4) at 8 to 16 bits for HEVC, and **VP8** and **VP9**
(profile 0, 8-bit 4:2:0) through [govpx](https://github.com/thesyncim/govpx) (see [VP8 and VP9](#vp8-and-vp9)). Audio: FLAC, MP3 and
(profile 0, 8-bit 4:2:0) through [go-avkit/vpx](https://github.com/go-avkit/vpx) (see [VP8 and VP9](#vp8-and-vp9)). Audio: FLAC, MP3 and
Opus, AAC-LC and HE-AAC v1 (SBR) since v0.6.0, and HE-AAC v2 (Parametric Stereo)
since v0.7.0, with the full profile (procedure B, IPD/OPD) by default since v0.8.0; on one core it takes
about 1.6 times ffmpeg's CPU time on an x86 server core and about 1.1 times on an Apple M4,
Expand Down Expand Up @@ -389,8 +389,12 @@ track may return.
entry is `vp08` or `vp09`; `decode.NewVP8(cfg)` and `decode.NewVP9(cfg)` take samples directly
(one VP8 frame, or one VP9 frame or superframe, as MP4 and Matroska store them: no parameter
sets, no length prefix, and `DecodeAnnexB` is `ErrUnsupported`). The decoding is
[`github.com/thesyncim/govpx`](https://github.com/thesyncim/govpx) (BSD-3, pure Go, a port of
libvpx; it is a dependency of this module, its decoder only). The pictures come out in decoding
[`github.com/go-avkit/vpx`](https://github.com/go-avkit/vpx) v0.1.0 (BSD-3, pure Go, a port of
libvpx; the decoders only, no encoder). Since this release it replaces
[govpx](https://github.com/thesyncim/govpx), which the port was taken from and which is now used
only by the tests, as the oracle of a differential comparison (`TestVPXDifferentialVectors`,
`TestVPXDifferentialStreams`: the two decoders give the same pictures, errors and frame counts on
all 376 libvpx vectors, serial and with 4 threads). The pictures come out in decoding
order (these codecs do not reorder), hidden frames (alt-ref) update the references and give no
picture, and `Frame.PTS` is the sample's.

Expand All @@ -401,12 +405,18 @@ picture, and `Frame.PTS` is the sample's.

Bounds: the size a frame header declares is read here (VP8 key frame, VP9 uncompressed header of
every frame of a superframe) and refused as `ErrOverLimit` when it is over `Config.MaxFrameMBs`
or its Frame is over 2^31-1 bytes, before govpx allocates anything (`TestVPXSizeBomb`, under a
memory limit). Fuzzed (`FuzzVP8Sample`, `FuzzVP9Sample`, `FuzzVP9FrameSize`, corpus committed):
govpx did not panic on any of the inputs tried, so there is no `recover` around it.

Equality with ffmpeg (its native decoders, `-f framemd5 -pix_fmt yuv420p`; govpx claims
byte-exactness with libvpx): the 5 committed streams in `testdata/vpx` (VP8 with alt-ref frames,
or its Frame is over 2^31-1 bytes, before the decoder allocates anything (`TestVPXSizeBomb`, under a
memory limit); the decoders also carry their own cap (`TestVPXPortSizeCap`: a key frame over
`MaxWidth` or `MaxHeight` is refused before any plane exists). Fuzzed (`FuzzVP8Sample`,
`FuzzVP9Sample`, `FuzzVP9FrameSize`, corpus committed, and `FuzzVP8Differential` and
`FuzzVP9Differential` against govpx): the decoders did not panic on any of the inputs tried, so
there is no `recover` around them. `Frame.Corrupt` is carried from the decoder's own flag, but the
decoders run without error concealment, so a damaged frame is an error and not a flagged picture
(195 bit-flipped samples of the committed streams gave 113 errors and 82 clean pictures, never a
Corrupt one).

Equality with ffmpeg (its native decoders, `-f framemd5 -pix_fmt yuv420p`; the port is checked
against libvpx's own vectors, below): the 5 committed streams in `testdata/vpx` (VP8 with alt-ref frames,
VP8 and VP9 at 161x97, VP9 with alt-ref superframes, VP9 with two tile columns; how they were
made is in `testdata/vpx/README.md`) match on every picture, through `DecodeTrack`, `Frames` and
`PosterFrame`.
Expand All @@ -415,9 +425,11 @@ libvpx's own decoder test vectors (`test/test_vectors.cc`: 376 streams; the per-
files are committed in `testdata/libvpx`, the streams are not): `TestLibvpxVectors` runs when
`LIBVPX_VECTORS_DIR` points at them (`go run ./vpxfetch` in `tools` downloads them) and skips
loudly otherwise. With the directory set, a vector missing from it fails the test. Results with
govpx v0.5.2: **373 match libvpx on every picture** (all VP8 vectors, and all VP9 vectors of
go-avkit/vpx v0.1.0: **373 match libvpx on every picture** (all VP8 vectors, and all VP9 vectors of
profiles 0 to 3 except the 4:4:0 ones), **3 are refused** (the `yuv440` vectors, as `ErrUnsupported`
naming 4:4:0), **0 differ**, with and without `GOEXPERIMENT=simd`. There is no list of known
naming 4:4:0), **0 differ**, in the four configurations (0 and 4 threads, with and without
`Recycle`, under `-race`) and, as `TestVPXDifferentialVectors`, picture for picture against govpx
v0.5.3 (before the switch: govpx v0.5.2, same counts; with and without `GOEXPERIMENT=simd`). There is no list of known
failures: until govpx v0.3.1 (out-of-frame coefficient contexts at partial frame edges, fixed in
govpx PR 136) 32 profile 0 vectors failed (the `resize` streams, three `size-WxH` streams and
`size-lf-1920x1080`), and any vector that differs now fails the test. The `container` reader fails
Expand All @@ -426,19 +438,20 @@ test cuts their blocks itself.

### Building with SIMD

govpx has native-SIMD kernels (Go 1.27 `simd/archsimd`, no assembly) for the VP9 8-tap convolve,
the VP8 and VP9 loop filters, the VP8 six-tap and the transforms. They are compiled **only** when
the program is built with `GOEXPERIMENT=simd` (amd64 with AVX2, or arm64); otherwise govpx runs
its scalar Go code, which is what a plain `go build` gives you. A library cannot set
go-avkit/vpx has native-SIMD kernels (Go 1.27 `simd/archsimd`, no assembly) for the VP9 8-tap
convolve, the VP8 and VP9 loop filters, the VP8 six-tap and the transforms. They are compiled
**only** when the program is built with `GOEXPERIMENT=simd` (amd64 with AVX2, or arm64); otherwise
it runs its scalar Go code, which is what a plain `go build` gives you. A library cannot set
`GOEXPERIMENT` for the programs that import it, so the choice is yours at build time:

```sh
GOEXPERIMENT=simd go build ./...
```

It needs the Go 1.27 toolchain (CI uses 1.27.4; the go.mod floor is 1.27.1) and changes nothing
in the API or the output. govpx exports no way to ask which path is active, so this package does
not either.
in the API or the output. The decoder exports no way to ask which path is active, so this package
does not either. The tables below were measured with govpx, before the switch; the figures of the
current decoder are under [Performance floor](#performance-floor).

Measured on one core of an AMD Zen 4 (amd64; ABBA rounds, one test binary per mode, same
commit, `BenchmarkDecode*` whole-file decodes, median time per frame):
Expand Down Expand Up @@ -517,10 +530,9 @@ whole-file decode:

`Config.Threads` sets the worker count of the VP8 and VP9 decoders. **The default is 1** (0 and 1
decode serially): threads cost CPU, and a caller on a shared machine should choose them knowingly.
VP9 with `Threads >= 2` turns on govpx's row-parallel decoding (`Threads` with `DecoderRowMT`);
`Threads` without row-MT would slow a one-tile stream by 11 %, and the threaded loop filter adds
nothing once row-MT is on, so neither is used. VP8 uses 2 threads for any `Threads >= 2`, because
govpx's gain is flat from 2 to 8. VP9 above 8 bits is decoded serially whatever is asked. The
VP9 with `Threads >= 2` turns on row-parallel decoding (`Threads` with `RowMT`);
`Threads` without row-MT would slow a one-tile stream by 11 % and is not used. VP8 uses 2 threads
for any `Threads >= 2`, because the gain was flat from 2 to 8 when it was measured. VP9 above 8 bits is decoded serially whatever is asked. The
pictures are byte-identical to the serial decoder's: `TestLibvpxVectorsThreaded` gives 373 of 376
libvpx vectors matching (3 4:4:0 refused, 0 differ) with 4 threads, with and without
`GOEXPERIMENT=simd` and under `-race`, and `TestVPXThreadsMatchSerial` compares 1, 2, 4 and 8
Expand All @@ -539,7 +551,24 @@ decode time includes the copy of each picture into a fresh `Frame`:
| VP8 | 2874 | 2008 (1.43x) | 2011 (1.43x) | 2010 (1.43x) |

The useful ceiling is about 4 threads. The clips are synthetic, and the arm64 gain was not
measured (the output was checked identical there).
measured (the output was checked identical there). These figures are govpx's.

### Performance floor

`TestVPXPerfFloor` (set `VPX_FLOOR_DIR` to a directory holding `crowd-vp8.ivf`,
`crowd-vp9-1tile.ivf` and `crowd-vp9-4tile.ivf`, the `crowd_run` 1080p50 clip encoded with libvpx;
it skips loudly otherwise, and above a 1-minute load of 8) fails when the decoder is slower than
the floor of milestone M0 of [go-avkit/vpx](https://github.com/go-avkit/vpx): VP9 one tile at most
7645 us per frame on one thread and VP8 at most 5275 us, two threads at most 0.8 of one, and four
threads at most 1.05 times govpx's (VP9 four tiles 2745 us, one tile 3448 us, VP8 3601 us).
Measured on fractal (Ryzen 9 7950X, 1-minute load 1.1 to 2.3), a test binary built with
`GOEXPERIMENT=simd`, best of 5 interleaved rounds, `Recycle` on, microseconds per picture:

| clip | 1 thread | 2 threads | 4 threads | floor |
|---|---|---|---|---|
| VP9, 1 tile column | 7197 | 4289 | 3234 | 7645, 0.8 x t1, 3620 |
| VP9, 4 tile columns | 7261 | 4219 | 2526 | 0.8 x t1, 2882 |
| VP8 | 4931 | 3494 | 3465 | 5275, 0.8 x t1, 3781 |

## HEVC

Expand Down
6 changes: 3 additions & 3 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// github.com/go-avkit/h264/decoder (go-avkit/h264, whose decoding layer was imported from oops1/go.264), the
// second HEVC (ITU-T H.265), through
// github.com/go-avkit/h265 (written from the specification), and VP8 and VP9 (profile 0) come
// through github.com/thesyncim/govpx, a port of libvpx: pure Go decoders that
// through github.com/go-avkit/vpx, a port of libvpx: pure Go decoders that
// are checked here, picture for picture, against ffmpeg. See the README for the profiles that claim holds
// for and the conformance bitstreams it was measured on.
//
Expand Down Expand Up @@ -101,9 +101,9 @@ type Config struct {
// Threads is the worker count of the VP8 and VP9 decoders; H.264 and HEVC ignore it. 0 and 1
// decode serially, which is the default: threads cost CPU (VP9 with 8 threads uses about 4.6
// cores for 1.7x), so a caller chooses them knowingly. VP9 with 2 or more threads turns on
// govpx's row-parallel mode, which also speeds up a one-tile stream; the useful ceiling is
// the row-parallel mode, which also speeds up a one-tile stream; the useful ceiling is
// about 4. VP8 uses 2 threads whatever n >= 2 is asked. VP9 above 8 bits is decoded serially
// by govpx whatever is asked. The pictures are byte-identical to the serial decoder's. A
// whatever is asked. The pictures are byte-identical to the serial decoder's. A
// negative value is ErrConfig.
Threads int
}
Expand Down
2 changes: 1 addition & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/go-avkit/bitstream v0.4.0 // indirect
github.com/go-avkit/h264 v0.15.0 // indirect
github.com/go-avkit/h265 v0.24.2 // indirect
github.com/go-avkit/vpx v0.1.0 // indirect
github.com/go-dsp/signal v0.2.0 // indirect
github.com/go-fft/fft v0.45.0 // indirect
github.com/go-numerics/special v0.4.0 // indirect
Expand All @@ -28,7 +29,6 @@ require (
github.com/mewpkg/term v0.0.0-20241026122259-37a80af23985 // indirect
github.com/pion/opus v0.1.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
github.com/thesyncim/govpx v0.5.3 // indirect
github.com/tphakala/go-mp3 v0.3.0 // indirect
github.com/tphakala/simd v1.16.0 // indirect
golang.org/x/sys v0.49.0 // indirect
Expand Down
62 changes: 32 additions & 30 deletions vpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"slices"

"github.com/go-avkit/avkit/container"
"github.com/thesyncim/govpx"
avx "github.com/go-avkit/vpx"
avvp8 "github.com/go-avkit/vpx/vp8"
avvp9 "github.com/go-avkit/vpx/vp9"
)

// vpx holds the VP8 or VP9 decoder of a Decoder (github.com/thesyncim/govpx, a pure Go port
// vpx holds the VP8 or VP9 decoder of a Decoder (github.com/go-avkit/vpx, a pure Go port
// of libvpx).
type vpx struct {
vp8 *govpx.VP8Decoder
vp9 *govpx.VP9Decoder
vp8 *avvp8.Decoder
vp9 *avvp9.Decoder
// free holds the planes of Frames given back with Recycle, at most maxFreePlanes sets.
free []planeSet
}
Expand Down Expand Up @@ -94,10 +96,10 @@ func NewVP9(cfg Config) (*Decoder, error) {
return newVPX(cfg, true)
}

// The govpx constructors, replaceable so that a test can make one refuse.
// The go-avkit/vpx constructors, replaceable so that a test can make one refuse.
var (
newGovpx8 = govpx.NewVP8Decoder
newGovpx9 = govpx.NewVP9Decoder
newVPX8 = avvp8.New
newVPX9 = avvp9.New
)

func newVPX(cfg Config, vp9 bool) (*Decoder, error) {
Expand All @@ -109,32 +111,32 @@ func newVPX(cfg Config, vp9 bool) (*Decoder, error) {
var err error
if vp9 {
side := int(min(int64(cfg.frameMBs())*16, 1<<16)) // VP9 sizes are 16 bits plus one
d.vpx.vp9, err = newGovpx9(vp9Options(cfg, side))
d.vpx.vp9, err = newVPX9(vp9Options(cfg, side))
} else {
side := int(min(int64(cfg.frameMBs())*16, 1<<14-1)) // VP8 sizes are 14 bits
d.vpx.vp8, err = newGovpx8(vp8Options(cfg, side))
d.vpx.vp8, err = newVPX8(vp8Options(cfg, side))
}
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrConfig, err)
}
return d, nil
}

// vp9Options maps Config.Threads: n >= 2 is govpx Threads with row-MT. Threads without row-MT
// slows a one-tile stream, and loop-filter threading adds nothing once row-MT is on (study of
// 2026-11-26, section 4.2), so neither DecoderLoopFilterOpt nor Threads alone is ever set.
func vp9Options(cfg Config, side int) govpx.VP9DecoderOptions {
o := govpx.VP9DecoderOptions{MaxWidth: side, MaxHeight: side}
// vp9Options maps Config.Threads: n >= 2 is Threads with row-MT. Threads without row-MT
// slows a one-tile stream (study of 2026-11-26, section 4.2), so RowMT is never set without
// Threads, nor Threads without RowMT.
func vp9Options(cfg Config, side int) avvp9.Options {
o := avvp9.Options{MaxWidth: side, MaxHeight: side}
if cfg.Threads >= 2 {
o.Threads, o.DecoderRowMT = cfg.Threads, true
o.Threads, o.RowMT = cfg.Threads, true
}
return o
}

// vp8Options maps Config.Threads: n >= 2 is 2 threads, since govpx's two-stage row pipeline gains
// vp8Options maps Config.Threads: n >= 2 is 2 threads, since the two-stage row pipeline gains
// the same 1.46x from 2 to 8 and no more.
func vp8Options(cfg Config, side int) govpx.DecoderOptions {
o := govpx.DecoderOptions{MaxWidth: side, MaxHeight: side}
func vp8Options(cfg Config, side int) avvp8.Options {
o := avvp8.Options{MaxWidth: side, MaxHeight: side}
if cfg.Threads >= 2 {
o.Threads = 2
}
Expand All @@ -157,7 +159,7 @@ func (d *Decoder) vpxSample(data []byte, pts int64) ([]Frame, error) {
}
v := d.vpx
var (
img govpx.Image
img avx.Image
ok bool
corrupted bool
err error
Expand All @@ -181,7 +183,7 @@ func (d *Decoder) vpxSample(data []byte, pts int64) ([]Frame, error) {

// vpxPicture turns the decoder's output for one sample into the frames to return: none for a
// hidden frame (references updated, nothing to show), one otherwise.
func (d *Decoder) vpxPicture(img govpx.Image, ok, corrupted bool, pts int64) ([]Frame, error) {
func (d *Decoder) vpxPicture(img avx.Image, ok, corrupted bool, pts int64) ([]Frame, error) {
if !ok {
return nil, nil
}
Expand All @@ -194,15 +196,15 @@ func (d *Decoder) vpxPicture(img govpx.Image, ok, corrupted bool, pts int64) ([]
}

// vpxFrame copies the decoder's picture, whose planes alias decoder storage, into a Frame.
func (d *Decoder) vpxFrame(img govpx.Image, pts int64) (Frame, error) {
func (d *Decoder) vpxFrame(img avx.Image, pts int64) (Frame, error) {
w, h := img.Width, img.Height
var chroma Chroma
switch img.Subsampling {
case govpx.Subsampling420:
case avx.Subsampling420:
chroma = Chroma420
case govpx.Subsampling422:
case avx.Subsampling422:
chroma = Chroma422
case govpx.Subsampling444:
case avx.Subsampling444:
chroma = Chroma444
default: // 4:4:0, which Frame cannot describe
return Frame{}, fmt.Errorf("%w: VP9 4:4:0 chroma sampling (Frame has 4:0:0, 4:2:0, 4:2:2 and 4:4:4)", ErrUnsupported)
Expand Down Expand Up @@ -323,7 +325,7 @@ func vp9Sizes(data []byte) [][2]int {
}

// vp9Frames splits a superframe by its index (VP9 specification Annex B); a packet without a
// well-formed index is one frame, as govpx reads it.
// well-formed index is one frame, as go-avkit/vpx reads it.
func vp9Frames(data []byte) [][]byte {
n := len(data)
m := data[n-1]
Expand Down Expand Up @@ -440,16 +442,16 @@ func (r *bitReader) u(n int) uint32 {
return v
}

// classifyVPX maps govpx's errors onto this package's.
// classifyVPX maps go-avkit/vpx's errors onto this package's.
func classifyVPX(err error) error {
switch {
case errors.Is(err, govpx.ErrVP9NotImplemented):
case errors.Is(err, avx.ErrVP9NotImplemented):
return fmt.Errorf("%w: %w", ErrUnsupported, err)
case errors.Is(err, govpx.ErrFrameRejected):
case errors.Is(err, avx.ErrFrameRejected):
return fmt.Errorf("%w: %w", ErrOverLimit, err)
case errors.Is(err, govpx.ErrClosed):
case errors.Is(err, avx.ErrClosed):
return ErrClosed
case errors.Is(err, govpx.ErrInvalidData), errors.Is(err, govpx.ErrInvalidVP9Data), errors.Is(err, govpx.ErrNeedKeyFrame):
case errors.Is(err, avx.ErrInvalidData), errors.Is(err, avx.ErrInvalidVP9Data), errors.Is(err, avx.ErrNeedKeyFrame):
return fmt.Errorf("%w: %w", ErrBitstream, err)
}
return err
Expand Down
31 changes: 16 additions & 15 deletions vpx_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@ var sentinelPairs = []struct{ port, ref error }{
}

// errClass names the class decode gives an error (classifyVPX), one of its sentinels or "other".
// An error of govpx is first replaced by its twin in go-avkit/vpx.
func errClass(err error, port bool) string {
if err == nil {
return ""
}
if port {
if !port {
for _, p := range sentinelPairs {
if errors.Is(err, p.port) {
err = p.ref
if errors.Is(err, p.ref) {
err = p.port
break
}
}
Expand Down Expand Up @@ -159,15 +160,15 @@ func newDiffPair(vp9on bool, threads int) (*diffPair, error) {
cfg := Config{Threads: threads}
if vp9on {
side = int(min(int64(fuzzConfig.frameMBs())*16, 1<<16))
ref, err := govpx.NewVP9Decoder(vp9Options(cfg, side))
refo := govpx.VP9DecoderOptions{MaxWidth: side, MaxHeight: side}
if threads >= 2 {
refo.Threads, refo.DecoderRowMT = threads, true
}
ref, err := govpx.NewVP9Decoder(refo)
if err != nil {
return nil, err
}
o := vp9.Options{MaxWidth: side, MaxHeight: side}
if threads >= 2 {
o.Threads, o.RowMT = threads, true
}
port, err := vp9.New(o)
port, err := vp9.New(vp9Options(cfg, side))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -203,15 +204,15 @@ func newDiffPair(vp9on bool, threads int) (*diffPair, error) {
stop: func() { ref.Close(); port.Close() },
}, nil
}
ref, err := govpx.NewVP8Decoder(vp8Options(cfg, side))
refo := govpx.DecoderOptions{MaxWidth: side, MaxHeight: side}
if threads >= 2 {
refo.Threads = 2
}
ref, err := govpx.NewVP8Decoder(refo)
if err != nil {
return nil, err
}
o := vp8.Options{MaxWidth: side, MaxHeight: side}
if threads >= 2 {
o.Threads = 2
}
port, err := vp8.New(o)
port, err := vp8.New(vp8Options(cfg, side))
if err != nil {
return nil, err
}
Expand Down
Loading
Loading