Skip to content
go-avkitPublic

About

Clean-room AV1 decoder in pure Go (stage S0: OBU and headers)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

av1

Clean-room AV1 decoder in pure Go. No cgo, no assembly, no third-party runtime dependency.

Status: stage S4 of S0-S8 (intra-only decoding with every post-filter). The module decodes key frames and intra-only frames to pixels: deblocking, CDEF, superres, loop restoration and film grain included. A stream that needs inter prediction is refused with av1.ErrUnsupported; the pictures decoded before it are still returned.

import "github.com/go-avkit/av1"

var dec av1.Decoder // zero value is ready; set Limits to tighten bounds
pics, err := dec.Decode(temporalUnit) // one Section 5 temporal unit, e.g. an IVF frame
for _, p := range pics {
	// p.Planes[i] holds uint16 samples (Pix, Stride, W, H); valid until the next Decode
}

The parsers are usable on their own:

import (
	"github.com/go-avkit/av1/header"
	"github.com/go-avkit/av1/obu"
)

units, err := obu.ParseSection5(data, obu.Limits{}) // zero Limits means DefaultLimits; or obu.ParseAnnexB
var dec header.Decoder // zero value is ready; set Limits to tighten bounds
for _, u := range units {
	res, err := dec.Decode(u)
	// res.Seq, res.Frame and res.Tiles carry what the OBU held
}

Packages

Package Role
obu OBU headers, LEB128, Section 5 low-overhead streams, Annex B length-delimited units
header Sequence header, frame header, decoder state (reference slots, order hints, film grain loading)
entropy, block, recon Symbol decoder and CDFs, tile syntax and reconstruction, transforms and predictors
postfilter Deblocking (7.14), CDEF (7.15), superres (7.16), loop restoration (7.17), film grain (7.18.3)
av1 (root) Decoder: temporal unit in, pictures out
internal/bits Bit reader for the spec descriptors (f(n), su(n), ns(n), uvlc(), le(n))

Every size and count taken from the stream is checked against a Limits value and refused with a LimitError; a truncated payload returns ErrTruncated. Nothing in the parsers panics on hostile input (see the fuzz targets).

How it is checked

Written from the AV1 specification only. The oracle is a set of golden dumps of 242 conformance vectors (header fields and header bit counts) produced by a reference decoder used as a black box; all 242 match. Beyond the goldens:

  • guard tests for truncation, limits and decoder state;
  • fuzz targets for the OBU and header parsers, plus a differential fuzz against mp4ff;
  • mutsweep over every refusal in the parsers: each deletion is caught by a test.

Known limits of the oracle:

  • Quantizer matrices (using_qmatrix) are not oracle-checked: no available vector sets the flag. They are covered by hand-built bitstreams only.
  • SVC streams L2T1 and L2T2 are compared against the 16-frame list.
  • Matroska vectors use the remuxed IVF files.
  • Annex B is covered by TestAnnexBEqualsSection5, which converts a Section 5 stream to Annex B and back.
  • Header bit counts exclude trailing and alignment bits; tile-group bits and redundant headers are not logged by the oracle.

Stages

S0 OBU and headers is released as v0.1.0. Stages S1-S8 add the pixel pipeline.

S1 (entropy and block syntax) is done and released as v0.2.0. entropy has the symbol decoder (checked against a literal port of specification 8.2 and fuzzed), the default CDF tables and the CDFs frame context, all generated from the specification text by internal/gen/gentables. block has the constants, the lookup and scan tables and the tile syntax of every frame type: partition, intra and inter mode info, reference frames, the motion vector stack (7.10.2), motion field estimation (7.9), intra block copy, palette, loop restoration units, transform size and type, coefficients. Only the syntax is parsed: nothing is predicted or reconstructed yet, but motion vectors are exact.

block.NewFrame prepares a frame from its headers and the reference slots, block.DecodeTileFrame decodes one tile of it, and Frame.State returns what later frames need (motion field and segmentation map). block.DecodeTile is the one-call form for intra frames. Both return the symbol count, the bits consumed and the adapted CDFs of a tile.

TestTileSyntaxGoldens checks, for every frame of the 242 vectors, the tile size, symbol count, bits consumed and the end-of-frame CDF hash against the reference decoder; all 242 match. FuzzDecodeTile and FuzzDecodeInter (corpora committed) check that no input makes the tile parser panic.

S2 (intra reconstruction) is released as v0.3.0, completed by v0.3.1 (intra block copy prediction). recon has the dequantiser (7.12.3, with quantiser matrices), every inverse transform (7.13: DCT 4 to 64, ADST 4/8/16, identity, Walsh-Hadamard for lossless), the intra predictors (7.11.2: DC, Paeth, smooth, directional with the edge filter, corner filter and upsampling, recursive filter-intra), chroma from luma (7.11.5) and the palette predictor (7.11.4), for 8 and 10 bits, 4:2:0 and monochrome (4:4:4 is fuzzed but has no oracle). Each is checked against a second, map-based transcription of the specification text. block now predicts and reconstructs while it parses: Frame.Plane returns CurrFrame of a plane (as uint16) after the last tile, before any loop filter.

TestIntraReconGoldens hashes the planes of the 283 key and intra-only frames of the 242 vectors (and the superblocks, where the golden lists them), and compares them with the reference decoder run with its loop filters off: all 283 match. v0.3.0 left the two frames of av1-1-b8-16-intra_only-intrabc-extreme-dv pending; v0.3.1 predicts intra block copy blocks (7.11.3 with the current frame as the reference, bilinear filter, non-compound rounding), checked against a literal transcription of 7.11.3.3 and 7.11.3.4, and block/testdata/pending_recon.txt is empty. recon.Inverse2D no longer allocates.

S4 (post-filters, intra-only decoding) is done. postfilter ports sections 7.14 to 7.17 and 7.18.3 literally: the deblocking filter, CDEF, super-resolution upscaling, loop restoration (Wiener and self-guided, with the stripe boundary rules) and film grain synthesis. block records the side information the filters read (skip, transform size, reference, segment, loop filter deltas, CDEF indices, restoration units) and av1.Decoder runs the chain per frame.

TestPostFilterGoldens compares the output of every stage, for the 283 key and intra-only frames of the vectors, with hashes of the reference decoder: all match. TestAllIntraMD5 decodes av1-1-b8-02-allintra, the key frames of the size vectors, the film grain vectors (8 and 10 bit), quantizer-00, the monochrome vectors and the two intra block copy frames of av1-1-b8-16-intra_only-intrabc-extreme-dv through av1.Decoder and compares every picture with the vector's libaom .md5: all match. The film grain checks cover two intra outputs only.

Known limits of the oracle:

  • Superres has no oracle: no available vector uses it. It is a literal port of 7.16, checked by synthetic tests and fuzzed (FuzzPipeline).
  • Monochrome and 4:4:4 post-filters are covered by synthetic tests and the monochrome vectors' key frames; 4:4:4 has no vector.
  • The two lower-layer key frames of the SVC vectors are not in libaom's md5 lists.

FuzzDecode and FuzzPipeline check that no input panics; a bomb test asserts a typed LimitError under GOMEMLIMIT and ulimit -v. Pipeline.Run does not allocate once warm; block.NewFrame still allocates per frame. Inter prediction comes in later stages; the code is scalar, SIMD kernels follow.

License

See LICENSE and PATENTS.

About

Clean-room AV1 decoder in pure Go (stage S0: OBU and headers)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages