From c848fcd13d54085b572f5883763125a77fae9a2b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 29 Nov 2026 08:31:23 +0100 Subject: [PATCH 1/2] Switch the VP8/VP9 decoders from govpx to go-avkit/vpx v0.1.0 (M0 D0c) decode now drives go-avkit/vpx's VP8 and VP9 decoders; govpx stays as a test-only differential oracle. Errors are classified from the port's avx errors; the size cap is asserted at the port (TestVPXPortSizeCap). Evidence: - libvpx test vectors, 4 configurations (0 and 4 threads, with and without Recycle, -race): 373 match, 3 4:4:0 refused, 0 differ, of 376; TestVPXDifferentialVectors passes against govpx v0.5.3. - TestVPXPerfFloor on the reference host, best of 5, us per frame: VP9 1 tile t1 7197 t2 4289 t4 3234; VP9 4 tiles t1 7261 t2 4219 t4 2526; VP8 t1 4931 t2 3494 t4 3465. The M0 floors (VP9 t1 <= 7645, VP8 t1 <= 5275) hold. Frame.Corrupt is never produced: the port exposes no error concealment, so a damaged frame is an error. The README says so. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 73 ++++++++++++++++++++--------- decode.go | 6 +-- vpx.go | 62 +++++++++++++------------ vpx_diff_test.go | 31 +++++++------ vpx_floor_test.go | 111 ++++++++++++++++++++++++++++++++++++++++++++ vpx_guard_test.go | 92 ++++++++++++++++++++++++------------ vpx_threads_test.go | 33 ++++++------- 7 files changed, 292 insertions(+), 116 deletions(-) create mode 100644 vpx_floor_test.go diff --git a/README.md b/README.md index 60bcf34..dcaa30e 100644 --- a/README.md +++ b/README.md @@ -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, @@ -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. @@ -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`. @@ -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 @@ -426,10 +438,10 @@ 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 @@ -437,8 +449,9 @@ 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): @@ -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 @@ -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 diff --git a/decode.go b/decode.go index bb15cb9..9633087 100644 --- a/decode.go +++ b/decode.go @@ -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. // @@ -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 } diff --git a/vpx.go b/vpx.go index 15a282e..5404ae7 100644 --- a/vpx.go +++ b/vpx.go @@ -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 } @@ -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) { @@ -109,10 +111,10 @@ 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) @@ -120,21 +122,21 @@ func newVPX(cfg Config, vp9 bool) (*Decoder, error) { 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 } @@ -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 @@ -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 } @@ -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) @@ -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] @@ -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 diff --git a/vpx_diff_test.go b/vpx_diff_test.go index b31faf1..691d37d 100644 --- a/vpx_diff_test.go +++ b/vpx_diff_test.go @@ -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 } } @@ -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 } @@ -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 } diff --git a/vpx_floor_test.go b/vpx_floor_test.go new file mode 100644 index 0000000..fae893e --- /dev/null +++ b/vpx_floor_test.go @@ -0,0 +1,111 @@ +// Copyright (c) 2026, go-avkit +// SPDX-License-Identifier: BSD-3-Clause + +package decode + +import ( + "os" + "path/filepath" + "strconv" + "strings" + "testing" + "time" +) + +// TestVPXPerfFloor is the performance floor of M0 (docs/design/vpx-decoder.md): on the crowd_run +// 1080p50 clips, VP9 one tile and VP8 decode a frame in at most 7645 and 5275 us on one thread, two +// threads take at most 0.8 of one, and four threads are no slower than 1.05 times govpx's (VP9 +// four tiles 2745 us, VP9 one tile 3448 us, VP8 3601 us per frame). It runs only on a quiet, +// dedicated machine: VPX_FLOOR_DIR names the directory holding crowd-vp8.ivf, crowd-vp9-1tile.ivf +// and crowd-vp9-4tile.ivf. Rounds are interleaved and the best of them is kept; a machine whose +// one-minute load is above 8 skips the test, which then says so. +func TestVPXPerfFloor(t *testing.T) { + dir := os.Getenv("VPX_FLOOR_DIR") + if dir == "" { + t.Skip("VPX_FLOOR_DIR is not set: the performance floor was NOT measured") + } + if b, err := os.ReadFile("/proc/loadavg"); err == nil { + if f := strings.Fields(string(b)); len(f) > 0 { + if l, err := strconv.ParseFloat(f[0], 64); err == nil && l > 8 { + t.Skipf("one-minute load %.1f is above 8: not measured", l) + } + } + } + rounds := 5 + if s := os.Getenv("VPX_FLOOR_ROUNDS"); s != "" { + n, err := strconv.Atoi(s) + if err != nil || n < 1 { + t.Fatalf("VPX_FLOOR_ROUNDS %q", s) + } + rounds = n + } + for _, c := range []struct { + file string + vp8 bool + t1max float64 // us per frame, 0 for no bound + govpxT4 float64 + checkT2x bool + }{ + {"crowd-vp9-1tile.ivf", false, 7645, 3448, true}, + {"crowd-vp9-4tile.ivf", false, 0, 2745, true}, + {"crowd-vp8.ivf", true, 5275, 3601, true}, + } { + t.Run(c.file, func(t *testing.T) { + path := filepath.Join(dir, c.file) + samples, err := libvpxSamples(path) + if err != nil { + t.Fatal(err) + } + best := map[int]float64{} + for r := 0; r < rounds; r++ { + for _, n := range []int{1, 2, 4} { + us := decodeMicrosPerFrame(t, samples, c.vp8, n) + if b, ok := best[n]; !ok || us < b { + best[n] = us + } + } + } + t.Logf("%s: t1 %.0f us, t2 %.0f us, t4 %.0f us per frame (best of %d)", c.file, best[1], best[2], best[4], rounds) + if c.t1max > 0 && best[1] > c.t1max { + t.Errorf("t1 %.0f us is above %.0f us", best[1], c.t1max) + } + if c.checkT2x && best[2] > 0.8*best[1] { + t.Errorf("t2 %.0f us is above 0.8 x t1 (%.0f us)", best[2], 0.8*best[1]) + } + if best[4] > c.govpxT4*1.05 { + t.Errorf("t4 %.0f us is above govpx's %.0f us x 1.05", best[4], c.govpxT4) + } + }) + } +} + +// decodeMicrosPerFrame decodes every sample once with n threads (Threads 1 is the serial decoder), +// giving each picture back, and returns the microseconds per picture. +func decodeMicrosPerFrame(t *testing.T, samples [][]byte, vp8 bool, n int) float64 { + mk := NewVP9 + if vp8 { + mk = NewVP8 + } + d, err := mk(Config{Threads: n}) + if err != nil { + t.Fatal(err) + } + defer d.Close() + pictures := 0 + start := time.Now() + for i, s := range samples { + frames, err := d.DecodeSample(s, int64(i)) + if err != nil { + t.Fatalf("sample %d: %v", i, err) + } + pictures += len(frames) + for _, f := range frames { + d.Recycle(f) + } + } + el := time.Since(start) + if pictures == 0 { + t.Fatal("no picture") + } + return float64(el.Microseconds()) / float64(pictures) +} diff --git a/vpx_guard_test.go b/vpx_guard_test.go index 73820f6..63a755b 100644 --- a/vpx_guard_test.go +++ b/vpx_guard_test.go @@ -6,9 +6,12 @@ package decode import ( "bytes" "errors" + "runtime" "testing" - "github.com/thesyncim/govpx" + avx "github.com/go-avkit/vpx" + avvp8 "github.com/go-avkit/vpx/vp8" + avvp9 "github.com/go-avkit/vpx/vp9" ) // An empty sample is ErrSample for both codecs, not a decoder error. @@ -33,7 +36,7 @@ func TestVPXFrameRefusesShortPlanes(t *testing.T) { t.Fatal(err) } defer d.Close() - good := govpx.Image{ + good := avx.Image{ Width: 3, Height: 3, Y: make([]byte, 9), U: make([]byte, 4), V: make([]byte, 4), YStride: 3, UStride: 2, VStride: 2, @@ -41,13 +44,13 @@ func TestVPXFrameRefusesShortPlanes(t *testing.T) { if f, err := d.vpxFrame(good, 7); err != nil || f.Width != 3 || len(f.Cb) != 4 || f.PTS != 7 { t.Fatalf("good picture: %+v, %v", f, err) } - for name, mutate := range map[string]func(*govpx.Image){ - "zero width": func(i *govpx.Image) { i.Width = 0 }, - "zero height": func(i *govpx.Image) { i.Height = 0 }, - "short Y": func(i *govpx.Image) { i.Y = i.Y[:8] }, - "short U": func(i *govpx.Image) { i.U = i.U[:3] }, - "short V": func(i *govpx.Image) { i.V = i.V[:3] }, - "narrow Y": func(i *govpx.Image) { i.YStride = 2 }, + for name, mutate := range map[string]func(*avx.Image){ + "zero width": func(i *avx.Image) { i.Width = 0 }, + "zero height": func(i *avx.Image) { i.Height = 0 }, + "short Y": func(i *avx.Image) { i.Y = i.Y[:8] }, + "short U": func(i *avx.Image) { i.U = i.U[:3] }, + "short V": func(i *avx.Image) { i.V = i.V[:3] }, + "narrow Y": func(i *avx.Image) { i.YStride = 2 }, } { img := good mutate(&img) @@ -64,8 +67,8 @@ func TestVPXFrameSamplings(t *testing.T) { t.Fatal(err) } defer d.Close() - img := govpx.Image{ - Width: 4, Height: 2, Subsampling: govpx.Subsampling422, BitDepth: 10, + img := avx.Image{ + Width: 4, Height: 2, Subsampling: avx.Subsampling422, BitDepth: 10, Y16: make([]uint16, 8), U16: make([]uint16, 4), V16: make([]uint16, 4), YStride: 4, UStride: 2, VStride: 2, } @@ -85,7 +88,7 @@ func TestVPXFrameSamplings(t *testing.T) { t.Errorf("bit depth 9: %v, want ErrBitstream", err) } e440 := img - e440.Subsampling = govpx.Subsampling440 + e440.Subsampling = avx.Subsampling440 if _, err := d.vpxFrame(e440, 0); !errors.Is(err, ErrUnsupported) { t.Errorf("4:4:0: %v, want ErrUnsupported", err) } @@ -120,7 +123,7 @@ func TestVP9FramesIndex(t *testing.T) { } } -// Flush has nothing to give back (no reordering) and Close releases the govpx decoder. +// Flush has nothing to give back (no reordering) and Close releases the VP8 or VP9 decoder. func TestVPXFlushAndClose(t *testing.T) { for name, mk := range map[string]func(Config) (*Decoder, error){"vp8": NewVP8, "vp9": NewVP9} { d, err := mk(fuzzConfig) @@ -137,7 +140,7 @@ func TestVPXFlushAndClose(t *testing.T) { t.Errorf("%s: Close = %v", name, err) } if d.vpx != nil { - t.Errorf("%s: Close left the govpx decoder", name) + t.Errorf("%s: Close left the decoder", name) } if err := d.Close(); err != nil { t.Errorf("%s: second Close = %v", name, err) @@ -152,15 +155,15 @@ func TestVPXPicture(t *testing.T) { t.Fatal(err) } defer d.Close() - good := govpx.Image{ + good := avx.Image{ Width: 2, Height: 2, Y: make([]byte, 4), U: make([]byte, 1), V: make([]byte, 1), YStride: 2, UStride: 1, VStride: 1, } - if fr, err := d.vpxPicture(govpx.Image{}, false, false, 0); fr != nil || err != nil { + if fr, err := d.vpxPicture(avx.Image{}, false, false, 0); fr != nil || err != nil { t.Errorf("hidden frame: %v, %v, want nil, nil", fr, err) } - if _, err := d.vpxPicture(govpx.Image{Width: 2, Height: 2}, true, false, 0); !errors.Is(err, ErrBitstream) { + if _, err := d.vpxPicture(avx.Image{Width: 2, Height: 2}, true, false, 0); !errors.Is(err, ErrBitstream) { t.Errorf("empty planes: %v, want ErrBitstream", err) } fr, err := d.vpxPicture(good, true, true, 5) @@ -172,13 +175,13 @@ func TestVPXPicture(t *testing.T) { } } -// A refusal of the govpx constructor is ErrConfig. +// A refusal of the go-avkit/vpx constructor is ErrConfig. func TestVPXConstructorRefusal(t *testing.T) { boom := errors.New("boom") - old8, old9 := newGovpx8, newGovpx9 - defer func() { newGovpx8, newGovpx9 = old8, old9 }() - newGovpx8 = func(govpx.DecoderOptions) (*govpx.VP8Decoder, error) { return nil, boom } - newGovpx9 = func(govpx.VP9DecoderOptions) (*govpx.VP9Decoder, error) { return nil, boom } + old8, old9 := newVPX8, newVPX9 + defer func() { newVPX8, newVPX9 = old8, old9 }() + newVPX8 = func(avvp8.Options) (*avvp8.Decoder, error) { return nil, boom } + newVPX9 = func(avvp9.Options) (*avvp9.Decoder, error) { return nil, boom } for name, mk := range map[string]func(Config) (*Decoder, error){"vp8": NewVP8, "vp9": NewVP9} { d, err := mk(fuzzConfig) if d != nil || !errors.Is(err, ErrConfig) || !errors.Is(err, boom) { @@ -187,6 +190,35 @@ func TestVPXConstructorRefusal(t *testing.T) { } } +// The cap of go-avkit/vpx itself: MaxWidth/MaxHeight refuse a larger key frame as +// ErrFrameRejected (ErrOverLimit here) before any plane is allocated, whatever Config says. +func TestVPXPortSizeCap(t *testing.T) { + d9, err := avvp9.New(avvp9.Options{MaxWidth: 64, MaxHeight: 64}) + if err != nil { + t.Fatal(err) + } + defer d9.Close() + d8, err := avvp8.New(avvp8.Options{MaxWidth: 64, MaxHeight: 64}) + if err != nil { + t.Fatal(err) + } + defer d8.Close() + var before, after runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&before) + err9 := d9.DecodeWithPTS(vp9Key(4096, 4096), 0) + err8 := d8.DecodeWithPTS(vp8Key(4096, 4096), 0) + runtime.ReadMemStats(&after) + for name, err := range map[string]error{"vp9": err9, "vp8": err8} { + if !errors.Is(err, avx.ErrFrameRejected) || !errors.Is(classifyVPX(err), ErrOverLimit) { + t.Errorf("%s: %v, want ErrFrameRejected, class ErrOverLimit", name, err) + } + } + if grown := int64(after.HeapAlloc) - int64(before.HeapAlloc); grown > 8<<20 { + t.Errorf("the heap grew by %d bytes", grown) + } +} + // bitString packs a string of 0 and 1 (spaces ignored), most significant bit first, zero padded. func bitString(s string) []byte { var out []byte @@ -247,19 +279,19 @@ func bitString16(v int) string { return s } -// classifyVPX maps govpx's errors onto this package's, and leaves any other alone. +// classifyVPX maps go-avkit/vpx's errors onto this package's, and leaves any other alone. func TestClassifyVPX(t *testing.T) { other := errors.New("other") for _, c := range []struct { in error want error }{ - {govpx.ErrVP9NotImplemented, ErrUnsupported}, - {govpx.ErrFrameRejected, ErrOverLimit}, - {govpx.ErrClosed, ErrClosed}, - {govpx.ErrInvalidData, ErrBitstream}, - {govpx.ErrInvalidVP9Data, ErrBitstream}, - {govpx.ErrNeedKeyFrame, ErrBitstream}, + {avx.ErrVP9NotImplemented, ErrUnsupported}, + {avx.ErrFrameRejected, ErrOverLimit}, + {avx.ErrClosed, ErrClosed}, + {avx.ErrInvalidData, ErrBitstream}, + {avx.ErrInvalidVP9Data, ErrBitstream}, + {avx.ErrNeedKeyFrame, ErrBitstream}, {other, other}, } { if got := classifyVPX(c.in); !errors.Is(got, c.want) { @@ -269,7 +301,7 @@ func TestClassifyVPX(t *testing.T) { if got := classifyVPX(other); got != other { t.Errorf("an unknown error was wrapped: %v", got) } - if got := classifyVPX(govpx.ErrClosed); got != ErrClosed { + if got := classifyVPX(avx.ErrClosed); got != ErrClosed { t.Errorf("ErrClosed = %v", got) } } diff --git a/vpx_threads_test.go b/vpx_threads_test.go index ba7e286..42f061a 100644 --- a/vpx_threads_test.go +++ b/vpx_threads_test.go @@ -7,7 +7,8 @@ import ( "errors" "testing" - "github.com/thesyncim/govpx" + avvp8 "github.com/go-avkit/vpx/vp8" + avvp9 "github.com/go-avkit/vpx/vp9" ) // TestVPXThreadsMatchSerial: on every committed stream, Config.Threads gives the serial @@ -38,8 +39,8 @@ func TestVPXThreadsMatchSerial(t *testing.T) { } } -// TestVPXThreadsOptions pins the mapping to govpx: serial below 2; VP9 Threads with row-MT and -// never loop-filter opt; VP8 two threads at most. +// TestVPXThreadsOptions pins the mapping to go-avkit/vpx: serial below 2; VP9 Threads with +// row-MT; VP8 two threads at most. func TestVPXThreadsOptions(t *testing.T) { for _, tc := range []struct { threads int @@ -49,21 +50,21 @@ func TestVPXThreadsOptions(t *testing.T) { }{{0, 0, false, 0}, {1, 0, false, 0}, {2, 2, true, 2}, {4, 4, true, 2}, {8, 8, true, 2}} { o9 := vp9Options(Config{Threads: tc.threads}, 64) o8 := vp8Options(Config{Threads: tc.threads}, 64) - if o9.Threads != tc.t9 || o9.DecoderRowMT != tc.rowMT || o9.DecoderLoopFilterOpt || o8.Threads != tc.t8 { - t.Errorf("Threads %d: VP9 %d rowMT %v lfopt %v, VP8 %d; want VP9 %d rowMT %v, VP8 %d", - tc.threads, o9.Threads, o9.DecoderRowMT, o9.DecoderLoopFilterOpt, o8.Threads, tc.t9, tc.rowMT, tc.t8) + if o9.Threads != tc.t9 || o9.RowMT != tc.rowMT || o8.Threads != tc.t8 { + t.Errorf("Threads %d: VP9 %d rowMT %v, VP8 %d; want VP9 %d rowMT %v, VP8 %d", + tc.threads, o9.Threads, o9.RowMT, o8.Threads, tc.t9, tc.rowMT, tc.t8) } } } -// TestVPXThreadsPassedToGovpx: the constructors receive the mapped options. -func TestVPXThreadsPassedToGovpx(t *testing.T) { - old8, old9 := newGovpx8, newGovpx9 - defer func() { newGovpx8, newGovpx9 = old8, old9 }() - var got8 govpx.DecoderOptions - var got9 govpx.VP9DecoderOptions - newGovpx8 = func(o govpx.DecoderOptions) (*govpx.VP8Decoder, error) { got8 = o; return old8(o) } - newGovpx9 = func(o govpx.VP9DecoderOptions) (*govpx.VP9Decoder, error) { got9 = o; return old9(o) } +// TestVPXThreadsPassedToVPX: the constructors receive the mapped options. +func TestVPXThreadsPassedToVPX(t *testing.T) { + old8, old9 := newVPX8, newVPX9 + defer func() { newVPX8, newVPX9 = old8, old9 }() + var got8 avvp8.Options + var got9 avvp9.Options + newVPX8 = func(o avvp8.Options) (*avvp8.Decoder, error) { got8 = o; return old8(o) } + newVPX9 = func(o avvp9.Options) (*avvp9.Decoder, error) { got9 = o; return old9(o) } for _, mk := range []func(Config) (*Decoder, error){NewVP8, NewVP9} { d, err := mk(Config{Threads: 4}) if err != nil { @@ -71,8 +72,8 @@ func TestVPXThreadsPassedToGovpx(t *testing.T) { } d.Close() } - if got8.Threads != 2 || got9.Threads != 4 || !got9.DecoderRowMT { - t.Errorf("govpx got VP8 %+v, VP9 %+v", got8, got9) + if got8.Threads != 2 || got9.Threads != 4 || !got9.RowMT { + t.Errorf("go-avkit/vpx got VP8 %+v, VP9 %+v", got8, got9) } } From 1835e2f2abd788c63f6a7c330ac788c3ca75114d Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 29 Nov 2026 08:31:40 +0100 Subject: [PATCH 2/2] Tidy tools/go.mod for go-avkit/vpx Co-Authored-By: Claude Sonnet 5.5 --- tools/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/go.mod b/tools/go.mod index 4202c0e..423cc8a 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -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 @@ -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