Skip to content

av1

av1

github.com/go-avkit/av1 is a clean-room AV1 decoder in pure Go, built in stages S0 to S8. Stages S0 and S1 (v0.2.0) parse syntax; stage S2 (v0.3.0, v0.3.1) reconstructs intra frames before the loop filters; stage S4 (v0.4.0) adds the post-filters and an intra-only decoder, see Intra reconstruction and Post-filters. Inter prediction is not there yet: Decode refuses inter frames and show_existing_frame with ErrUnsupported. S0 and S1 cover OBU headers and Section 5 or Annex B streams, the sequence header, the frame header with its decoder state, and the tile syntax of every frame type (the block package: intra and inter mode info, reference frames, the motion vector stack, motion field estimation, intra block copy, palette, transform sizes and types, coefficients). It is not yet behind decode.

Using it

units, err := obu.ParseSection5(data, obu.Limits{}) // or obu.ParseAnnexB
var dec header.Decoder
for _, u := range units {
    res, err := dec.Decode(u)
    // res.Seq, res.Frame and res.Tiles carry what the OBU held
}

Every size and count read from the stream is bounded by a Limits value and refused with a LimitError; a short payload returns ErrTruncated.

Tiles are parsed with block:

f, err := block.NewFrame(seq, fh, slots) // slots: *block.FrameState per reference slot
st, err := block.DecodeTileFrame(f, tileNum, tileData, &cdfs)
// st.Symbols, st.BitsConsumed; cdfs now holds the adapted CDFs
state := f.State() // keep it in the slots the frame refreshes

Motion vectors are exact. Intra frames are also reconstructed (next section); inter prediction is not implemented.

Intra reconstruction

From v0.3.0 the recon package holds the dequantiser (with quantizer matrices), every inverse transform (DCT 4 to 64, ADST 4/8/16, identity, Walsh-Hadamard for lossless), the intra predictors (DC, Paeth, smooth, directional with edge filter and upsampling, filter-intra), chroma from luma and the palette predictor, for 8 and 10 bits, 4:2:0 and monochrome (4:4:4 is fuzzed, with no oracle). block predicts and reconstructs while it parses:

st, err := block.DecodeTileFrame(f, tileNum, tileData, &cdfs)
y := f.Plane(0) // CurrFrame as uint16, after the last tile, before any loop filter

The code is scalar; SIMD kernels are tracked in go-avkit/av1#6.

Post-filters and the decoder

From v0.4.0 the postfilter package ports, literally from the specification, the deblocking filter (7.14), CDEF (7.15), super-resolution (7.16), loop restoration (7.17, Wiener and self-guided) and film grain synthesis (7.18.3, applied to a copy so references stay clean). Pipeline.Run does not allocate once warm. The root package ties it together for intra-only streams:

dec := av1.Decoder{Limits: av1.Limits{}}
pics, err := dec.Decode(temporalUnit) // key, intra-only and intra block copy frames

Decode returns the shown pictures. Inter frames and show_existing_frame return av1.ErrUnsupported; OperatingPoint selects the layers kept. block.NewFrame still allocates for every frame.

What it claims, and how it was checked

The parsers were written from the AV1 specification alone. The oracle is a set of 242 conformance vectors whose header fields and header bit counts were dumped by a reference decoder used as a black box; all 242 match. The parsers are also fuzzed (including a differential fuzz against mp4ff), and a mutation sweep deletes every refusal in turn: each deletion is caught by a test.

For tiles (S1), a second set of goldens records, for every frame of the same 242 vectors, the tile size, symbol count, bits consumed and the hash of the end-of-frame CDFs; all 242 match, inter frames and intra block copy included. The tile parser is fuzzed (corpora committed).

For reconstruction (S2), the planes of the 283 key and intra-only frames of the same vectors are hashed and compared with the reference decoder run with its loop filters off: all 283 match. The two frames of av1-1-b8-16-intra_only-intrabc-extreme-dv need intra block copy prediction, added in v0.3.1 (go-avkit/av1#7). Each recon function is also checked against a second transcription of the specification, and the tile decoder is fuzzed with quantizer matrices and lossless coding.

For the post-filters (S4), TestPostFilterGoldens compares every stage of the 283 intra frames with the reference hashes: all match. TestAllIntraMD5 compares decoded pictures with libaom’s per-frame .md5 (av1-1-b8-02-allintra, the key frames of the size matrix, film grain at 8 and 10 bits, quantizer-00, monochrome, and the two intra block copy frames): all match. FuzzDecode and FuzzPipeline ran 60 s each without a finding (corpus committed), and a bomb test asserts a typed LimitError under a memory limit.

What the oracle does not cover:

  • Super-resolution: no vector uses it, so it is a literal port with fuzzing and no oracle.
  • 4:4:4 and monochrome post-filters: synthetic tests only (monochrome key frames are also covered by the monochrome vectors).
  • Film grain: only two intra outputs are checked.
  • Quantizer matrices (using_qmatrix): no available vector sets the flag, so they are tested with hand-built bitstreams only and are not oracle-checked.
  • SVC L2T1 and L2T2 are compared against the 16-frame list.
  • Matroska vectors use the remuxed IVF files.
  • Annex B is covered by a round trip from Section 5.
  • Header bit counts exclude trailing and alignment bits; tile-group bits and redundant headers are not logged by the oracle.

Header parsing cost against the reference decoder is not measured yet.