Skip to content

decode

decode

github.com/go-avkit/decode decodes H.264 and HEVC / H.265 (4:0:0, 4:2:0, 4:2:2 and 4:4:4 at 8 to 16 bits since v0.15.0; 4:2:0 at 8 and 10 bits before) to planar YUV and FLAC, MP3, Opus, since v0.6.0, AAC and, since v0.9.0, linear PCM (WAVE) to PCM, in pure Go with no cgo. Vorbis and MP2 are not decoded. H.264 is decoded by our own h264, whose decoding layer began as oops1/go.264 v1.12.0 (BSD-2-Clause) and which has run the H.264 path since v0.12.0; HEVC, since v0.10.0, by our own h265 v0.22.0 since v0.11.0 (it was gen2brain/h265 up to v0.9.0; that dependency is gone; until v0.14.0 a format a Frame could not carry was refused at the SPS with an ErrUnsupported that names it, and since v0.15.0 only separate colour planes are). Speed is on the history page: v0.10.0’s x86 8-bit slowdown (decode#101) is closed, and no stream is slower than v0.9.0 since v0.11.0. decode is the avkit-facing layer around them and the place where claims are measured. It is the only module in this organisation that produces pictures or sound.

Using it

r, _ := container.NewReader(file)
frames, err := decode.DecodeTrack(r, trackID, decode.Config{})
for _, f := range frames { // display order
    use(f.Y, f.Cb, f.Cr, f.YStride, f.CStride, f.Width, f.Height, f.PTS)
}

Since v0.14.0, decode.Frames is the streaming form of DecodeTrack, for H.264 and HEVC, with the same edit-list and timestamp handling:

for f, err := range decode.Frames(r, trackID, decode.Config{}) {
    if err != nil {
        return err
    }
    use(f)
}

It returns an iter.Seq2[*Frame, error]. Each *Frame is a fresh allocation the caller owns, so it can be kept or sent to another goroutine; leaving the loop (break, return) stops the decoding; an error is the last pair, after every picture decoded before it. On the 15 files of the edit-list corpus it yields exactly DecodeTrack’s pictures, and both equal ffmpeg’s md5. A caller that uses each frame and lets it go saves the slice: on Zen 4, one core, Frames against DecodeTrack, H.264 alien 2.63 s to 2.53 s (-4 %) with page faults 194 000 to 25 000, fade 1.05 s to 1.00 s (-5 %), HEVC alien-main 2.66 s to 2.47 s (-7 %).

Since v0.15.0, decode uses h265 v0.24.0 and releases each picture to it once it is converted to a Frame, so the decoder reuses the planes; the output is unchanged. Zen 4, one core, 7 ABBA rounds, median of 14 runs per side, against v0.14.0 (this measures that change alone): alien-main Frames 2.401 s to 2.213 s (-7.8 %), DecodeTrack 2.580 s to 2.444 s (-5.3 %); fade-main Frames 1.358 s to 1.261 s (-7.2 %), DecodeTrack 1.461 s to 1.386 s (-5.1 %); minor page faults per frame on alien-main Frames 1437 to 283. H.264 runs on h264 v0.12.0 (a store-forwarding stall removed), 2.2 to 2.7 % faster on Zen 4: alien 2.561 s to 2.491 s, fade 1.034 s to 1.011 s. Since v0.15.1, decode uses h264 v0.13.0: H.264 decoding is 3.3 to 3.6 % faster on Zen 4 (alien 2.266 s to 2.185 s, fade 0.881 s to 0.852 s, from the h264 notes). Since v0.15.2, it uses h264 v0.14.0: 5.9 % faster on alien (2.207 s to 2.076 s) and 3.5 % on fade (0.849 s to 0.819 s) on Zen 4; ffmpeg 6.1.1 is still faster on H.264, 2.07 times on alien and 2.28 times on fade. Since v0.15.3, it uses h264 v0.15.0 (CABAC residual decoding in one assembly kernel call, output identical to v0.14.0 under a differential fuzz test): alien 2.051 s to 2.025 s (1.3 %) on Zen 4; ffmpeg is about 2.02 times faster on alien (2.025 s against 1.004 s). Since v0.16.1, the SBR filter banks of HE-AAC v2 run in float32 (go-simd/floats v0.33.0): 699 ms to 670 ms (-4.2 %) on a short clip, one Zen 4 core; the PCM is no longer bit identical across CPU routes and is held to stated error bounds instead. Since v0.17.1, the parametric-stereo all-pass plan is driven by tick (go-simd/floats v0.41.0, AllPass3Plan.RunTick): BenchmarkPSDecode/ps 2053 us to 2044 us (-0.45 %, within a noise of about 0.3 %), output unchanged.

  • H.264 input: an avcC record (decode.NewAVCC) or a TrackConfig (decode.NewTrack) with length-prefixed samples, fed with DecodeSample and ended with Flush; or a raw Annex B stream (decode.New, DecodeAnnexB).
  • HEVC input: the same calls choose HEVC for a track whose sample entry is hvc1 or hev1; decode.NewHVCC takes an hvcC record, decode.NewHEVC takes in-band parameter sets, and DecodeAnnexB a raw .265 stream. Main 10 frames hold little-endian 16-bit samples.
  • Output: frames in display order, with planes, strides, size, chroma format, bit depth and PTS. Behaviour change in v0.15.0: a Frame may now be 4:0:0, 4:2:0, 4:2:2 or 4:4:4, at 8 to 16 bits, with luma and chroma depths that differ. Read Frame.Chroma, BitDepth and BitDepthC rather than assuming 4:2:0: (Frame).SampleBytes(plane) gives the bytes per sample of a plane (1 at 8 bits, otherwise 2, a little-endian 16-bit word) and (Frame).ChromaSize() the size of the chroma planes. A 4:0:0 frame has no Cb or Cr, and its BitDepthC is 0. Frames are bounded by their bytes, computed without overflow, so the same limit applies on every platform (decode#108, a 32-bit overflow, is fixed).

One picture

id, _ := decode.FirstVideoTrack(r)
f, err := decode.PosterFrame(r, id, 12.5, decode.PosterConfig{})

PosterFrame (since decode v0.2.0) returns the picture with the greatest presentation time at or before t, the very bytes DecodeTrack returns for it, without decoding the whole track: decoding starts at the last sync sample at or before the picture’s sample and stops as soon as the picture is out. It takes H.264 (avc1, avc3), HEVC (hvc1, hev1), VP8 (vp08) and VP9 (vp09, since v0.16.0; profiles 1 to 3 since v0.17.0); another codec (AV1, …) is refused by name, since v0.9.0, with a *CodecError (codec "vp09" is not supported; errors.Is(err, ErrUnsupported) holds), not reported as a corrupt sample. A negative or NaN t is ErrTime.

Edit lists

Since v0.5.0, DecodeTrack and PosterFrame apply an MP4 track’s edit list by default, as ffmpeg does: a picture presented before an edit’s media time, or at its end or after, is decoded but not returned, presentation times are shifted so the pictures of the edit start at 0 (or at the length of the empty edit that delays the track), and PosterFrame’s t is a presentation time. This is a change from v0.3.0 for a file whose edit list trims or delays the track. Config.IgnoreEditList (ffmpeg’s -ignore_editlist 1) returns every picture labelled with track time, as v0.3.0 did.

An edit list the package does not apply is refused with ErrEditList rather than guessed at (a media rate other than 1, an edit lasting to the end of the media, an empty edit after the first edit that shows media, a media time before the track). Matroska has no per-track video edit list, so the first picture’s time is now 0, as in ffmpeg. Known divergence: for a fragmented MP4 with a trimmed start, ffmpeg and decode return different pictures (21 against 19 on the fixture), because ffmpeg switches its edit list support off for a fragmented file and decode cannot tell the file is fragmented yet (go-avkit/decode#20).

Audio

id, _ := decode.FirstAudioTrack(r)
pcm, err := decode.DecodeAudioTrack(r, id, decode.AudioConfig{})
for _, a := range pcm { // presentation order, trimmed as the container states
    use(a.Data, a.Format, a.SampleRate, a.Channels, a.Layout, a.PTS)
}

NewAudioTrack streams it with DecodeSample and Flush. Data is interleaved little-endian PCM (s16le, s32le or f32le). The start of the track is dropped as its container states it (edit list, Opus pre-skip).

CodecLibraryOutputChecked against ffmpeg
FLACmewkiz/flacs16le up to 16 bits, s32le up to 24bit-exact on 13 fixtures
MP3 (Layer III)tphakala/go-mp3f32lemaximum error 1.07e-6 on 10 fixtures, sample counts equal
Opuspion/opusf32le, 48 kHzpasses opus_compare on all 12 RFC 8251 test vectors, mono and stereo
Linear PCM (WAVE; ipcm, fpcm, raw ), since v0.9.0this packages16le, s32le or f32lebit-exact on 6 WAVE fixtures (s16, s24, s32, f32, f64, u8); 64-bit float is rounded to f32le, which is lossy

Linear PCM takes its width and byte order from the pcmC record of the track’s sample entry; an entry without the record is ErrAudioConfig.

  • FLAC: 4 to 24 bits, 1 to 8 channels; a 32-bit stream is refused with ErrAudioUnsupported.
  • MP3: MPEG-1, 2 and 2.5, mono and stereo. MP2 (Layer II) is not decoded and is refused with ErrAudioUnsupported.
  • Opus: mono and stereo (mapping family 0); more channels (for example 5.1) are refused with ErrAudioUnsupported. Opus is checked against the RFC 8251 conformance vectors, not against ffmpeg’s output sample for sample.
  • Vorbis is not decoded: the one pure-Go decoder tried failed the comparison with ffmpeg, and no decoder was written.

AAC (since v0.6.0)

AAC is available since decode v0.6.0; v0.5.4 and earlier do not decode it. HE-AAC v2 follows in v0.7.0, with its full profile by default in v0.8.0.

  • AAC-LC, written in Go from ISO/IEC 14496-3 (mono to 7.1, 8 to 96 kHz; M/S and intensity stereo, perceptual noise substitution, TNS, pulses, short windows), output f32le. Main, LTP, SSR, 960-sample frames, coupling channels and channel configurations 11, 12 and 14 are refused with ErrAudioUnsupported.
  • HE-AAC v1 (SBR over AAC-LC) decodes at twice the core rate, whether the AudioSpecificConfig signals it or the stream only carries it. SBR in its downsampled mode is refused.
  • Gapless: an MP4’s iTunSMPB item and edit lists are applied to AAC as ffmpeg does. DecodeAudioTrack applies it, NewAudioTrack does not.
  • HE-AAC v2 (Parametric Stereo) is supported since v0.7.0, and the full profile is the default since v0.8.0: procedure B for ICC modes 3 to 5, and IPD/OPD with their smoothing and interpolation, as the _ur references of ISO’s vectors are. 34-band streams keep 34 bands (a baseline decoder maps them to 20, so one ISO vector, al_sbr_ps_06, matches its _ur reference but not its baseline one, 2226 LSB off). v0.7.0 mixed with baseline mixing only (procedure A, IPD/OPD ignored). A mono core that signals SBR is announced as stereo from the first frame. Checked: all 7 of 7 ISO PS vectors match the _ur references at 0.033 LSB, and 801 mono PS files of the author’s library are within 1.1e-5 of ffmpeg 9.0.2.
  • Checked: the error against a float64 reference of the same bitstream is about half of ffmpeg’s on the five fixtures; HE-AAC is within 3e-6 of ffmpeg 9.0.2 on four; 12 of the 15 ISO/IEC 14496-26 conformance vectors pass.

Speed (go-avkit/decode#35): a decode-only benchmark (BenchmarkAACDecodeReuse, one core) against the median utime of ffmpeg 9.0.2 -threads 1, which is a whole process that also opens and demuxes the file. Go 1.27.4, Apple M4 Max, 10-minute inputs, five interleaved rounds at 1-minute load 6.02 to 6.46:

Inputdecode (ms)ffmpeg utime (ms)decode / ffmpeg
5.1, 48 kHz793.48210.966
stereo, 48 kHz309.04720.655

HE-AAC v2 is slower than ffmpeg on one core: about 1.5 times its utime on an x86 server core and about 1.1 times on an Apple M4, an open defect (go-avkit/decode#77). Since v0.16.1 the SBR filter banks run in float32: 699 ms to 670 ms (-4.2 %) on Zen 4 for a short parametric-stereo clip, 1.59 to 1.52 times ffmpeg; v0.17.1 drives the parametric-stereo all-pass plan by tick (2053 to 2044 us, -0.45 %, marginal), and the whole decode was measured at 1.42 times ffmpeg’s user time at v0.17.1 (2026-11-25, Zen 4, one pinned core, load about 1, 9 interleaved rounds, ps4-he2.mp4: decode 643 ms median, ffmpeg 6.1.1 with one thread 453 ms user plus 209 ms system, 659 ms real; the system time is not paid by our in-process benchmark, so user time is the comparable figure; 1.74 when decode#77 opened). v0.18.0 runs the SBR envelope adjustment in binary32 (643 to 630.5 ms) and v0.18.1 the parametric-stereo hybrid filter bank in binary32 (630.5 to 579.5 ms): the same benchmark (BenchmarkAACDecodeReuse, taskset -c 9, median) now gives 579.5 ms against ffmpeg’s 453 ms user, 1.28 times (measurement). The PCM output moves within a stated bound of 2^-17 of full scale (measured 2^-23), and the float64 versions are kept as the oracle. Since then v0.19.1 runs the QMF analysis as one binary32 DCT-IV per slot (decode#135, about 16 ms) and the parametric-stereo stage on binary32 split planes with stated bounds (decode#137, about 1 %): 557.5 ms median against ffmpeg’s 453 ms user, 1.23 times (Zen 4, one core, interleaved, BenchmarkAACDecodeReuse). v0.19.2 adds a float32 SIMD parametric-stereo mixing kernel (go-simd/floats v0.44.0, floats#98, -1.67 % over 10 interleaved pairs): 547.1 ms median, 1.21 times. Same method, a 620 s HE-AAC v2 file, as measured for v0.8.0 (Go 1.27.4, interleaved rounds, one row per machine; the Zen 4 row below is the v0.16.0 measurement, the M4 row was not repeated):

Machine, datedecode / ffmpeg1-minute loadffmpeg
AMD Ryzen 9 7950X (Zen 4), Ubuntu 24.04, 2026-11-151.62 (1.60 to 1.65 in three runs)0.13 to 0.936.1.1
Apple M4 Max, 2026-11-15, indicative1.117.0 to 7.79.0.2

The two ffmpeg versions differ, so the rows do not compare the machines. The M4 ratio is at a load just under the gate and moves with it (1.37 to 1.47 earlier at a load of 1.6 and 6 to 7). It was 1.74 on the M4 before the speed work of v0.8.0.

Mono was not measured. The README of decode states each of these with its conditions.

What it decodes

VP8 and VP9 run faster when the program is built with GOEXPERIMENT=simd (Go 1.27, amd64 AVX2 or arm64): the native-SIMD kernels of go-avkit/vpx are compiled only then, and a library cannot set it for its importers. On one core that is 2.07x to 4.96x. How to build and the measurements are in the decode README.

Threads (v0.18.0). Config.Threads sets how many goroutines decode one VP8 or VP9 stream; the default is 1, the serial decoder, and the pictures are the same for every value (libvpx’s 373 matching vectors also match with 4 threads). Measured on a Ryzen 9 7950X pinned to 8 cores, 300 frames of 1080p testsrc2, 5 interleaved rounds, microseconds per picture:

clip1 thread248
VP9, 4 tiles39282687 (1.46x)2101 (1.87x)2173 (1.81x)
VP9, 1 tile37302713 (1.38x)2457 (1.52x)2450 (1.52x)
VP828742008 (1.43x)2011 (1.43x)2010 (1.43x)

More than 4 threads buys nothing on these clips. The clips are synthetic, and arm64 was not measured.

Recycle (v0.19.0). (*Decoder).Recycle(f) is an opt-in that hands a Frame’s planes back so that a later DecodeSample writes into them instead of allocating. The ownership rule: after Recycle(f) the caller must not read or write f.Y, f.Cb or f.Cr, nor those of a copy of f, and calls it once per Frame. A decoder whose Recycle is never called behaves as before; it is a no-op for H.264 and HEVC. On one thread it saved 7.0 % to 8.7 % of the time per picture and cut the allocation of the 300-frame clips from about 955 MB to 20 to 25 MB. Threads and Recycle can be used together (the decode test runs 4 threads with Recycle; their combined speed was not measured).

go-avkit/vpx (v0.20.0). VP8 and VP9 now run on our own decoder, go-avkit/vpx v0.1.0; thesyncim/govpx v0.5.3 stays in the tests only, as the oracle of a picture-for-picture differential test. Measured on the reference host (linux/amd64, GOEXPERIMENT=simd, load 1 to 2), best of 5 interleaved rounds, microseconds per picture, with Recycle:

clip1 thread24
VP9, 1 tile719742893234
VP9, 4 tiles726142192526
VP8493134943465

The decode test fails if 1 thread is slower than 7645 (VP9, 1 tile) or 5275 (VP8) microseconds, or 4 threads more than 5 % over 3448 (VP9, 1 tile), 2745 (VP9, 4 tiles) or 3601 (VP8). These are not the Ryzen figures of the Threads table above: another host and other clips. A damaged frame is an error, never a Frame with Corrupt set, because the decoders run without error concealment. The function-level profile is in go-avkit/vpx docs/perf; a libvpx column is not available (its symbols could not be obtained).

go-avkit/vpx v0.5.0 (decode v0.20.3). The AVX2 kernels of go-videodsp/kernels v0.25.0 now run the VP9 loop filters and vertical 8-tap convolution (vpx v0.3.0, v0.4.0) and the VP8 loop filters and six-tap and bilinear prediction (vpx v0.5.0). One thread, one pinned core of the host above, microseconds per picture of the crowd_run 1080p clip, interleaved against the previous release:

clipbefore the kernelsv0.20.3libvpx v1.17.0
VP9, 1 tile731353322648
VP9, 4 tiles727753732534
VP8494234372281

The first column is the build before the loop-filter kernels (VP9 from the v0.3.0 measurement, VP8 from v0.4.0); the gap to libvpx is 2.0 times for VP9 and 1.5 times for VP8.

Codec and profileState
H.264 Constrained Baseline; Baseline without FMO/ASObit-exact
H.264 Main, progressive (CAVLC, CABAC, B pictures, weighted prediction, multiple and long-term references, MMCO)bit-exact
H.264 High, 4:2:0 8-bit, progressivebit-exact on the streams measured; no JVT FRExt stream measured yet
H.264 interlaced (PAFF, MBAFF), FMO/ASO, Extendedrefused with ErrUnsupported
H.264 High 10, 4:2:2, 4:4:4, losslessrefused with ErrUnsupported
VP8 since v0.16.0, and VP9 profiles 0 to 3 (4:2:0, 4:2:2, 4:4:4, 8, 10 and 12 bits) since v0.17.0, over go-avkit/vpx v0.1.0 since decode v0.20.0libvpx’s decoder test vectors match on every picture: 373 of 376 (all 62 VP8, 311 of 314 VP9), none differs, with 0 and 4 threads, with and without Recycle; the test fails on any differing vector
VP9 4:4:0 (yuv440 vectors)refused with ErrUnsupported naming 4:4:0 (Frame has no such sampling); 3 of the 376 vectors
HEVC Main, Main Still Picture, 4:2:0 8-bitbit-exact against ffmpeg
HEVC Main 10, 4:2:0 10-bitbit-exact against ffmpeg
HEVC 4:0:0, 4:2:2 and 4:4:4, 8 to 16 bits, luma and chroma depths that differ (since v0.15.0, on by default)the 47 JCT-VC range-extension streams the test can run match the suite’s md5
HEVC 4:4:4 coded as separate colour planesrefused with ErrUnsupported
HEVC frame of more than 2³¹−1 bytesrefused with ErrOverLimit

How it was measured

Every picture’s MD5 is compared with ffmpeg’s -f framemd5, single thread.

  • ITU-T H.264.1 conformance suite (AVCv1): 81 streams tried, 73 are claimed. Measured on h264 (whose decoding layer comes from go.264 v1.12.0), 69 give the same pictures as ffmpeg today, as h264’s README also states (69 of its 73 streams); 4 more (MR2 to MR5_TANDBERG) need MMCO 5 and frame_num gaps (oops1/go.264#2) and are skipped until h264 carries the fix. The other 8 are not claimed: 5 interlaced and 3 using FMO/ASO.
  • JCT-VC conformance suite HEVC_v1 (ITU-T H.265.1): 147 streams. 143 give the same pictures as ffmpeg. Three are decided by the suite’s own MD5 because ffmpeg disagrees with it and decode agrees with it (SAODBLK_A_MainConcept_4, SAODBLK_B_MainConcept_4, VPSSPSPPS_A_MainConcept_1). One, TSUNEQBD_A_MAIN10_Technicolor_2 (luma and chroma bit depths differ), is refused by ffmpeg; since v0.15.0 decode decodes it to the suite’s MD5. The bitstreams are not in the repository, so the conformance test runs only when pointed at them.
  • JCT-VC range extensions (since v0.15.0): all 47 streams the test can run decode to the suite’s md5, including Bitdepth_A, Bitdepth_B and TSUNEQBD (10-bit luma, 9-bit chroma).
  • Streams and real files in the repository: 29 H.264 and 5 HEVC streams run in CI against ffmpeg’s MD5s; real MP4 and Matroska files (5 H.264, 7 HEVC) are recorded as hashes.

Limits

A stream is untrusted. Config.MaxFrameMBs (default 36864, a 4096x2304 picture) and MaxSampleBytes (default 64 MiB) are checked before anything sized by the stream is allocated. Bomb tests assert a refusal under a memory limit for both video codecs, and the parsers and sample decoders are fuzzed with a committed corpus. AudioConfig limits the channel count, the block size, a coded sample and the PCM a track may return. PosterFrame has bounds of its own in PosterConfig: MaxSamples (default 600) caps the samples decoded for one picture, past it the error is ErrPosterLimit, and MaxTrackSamples (default 1«20) refuses a longer sample list.

Speed

decode is slower than ffmpeg on every video input measured, and that is an open defect (go-avkit/decode#2). Measured on decode v0.3.0, single core, on an Apple M4 Max with ffmpeg 9.0.2 -threads 1, 5 interleaved rounds:

Inputdecode (fps)ffmpeg (fps)decode / ffmpeg
H.264 High 1080p, real clip36.5173.10.21
HEVC Main 10 2160p, real clip10.327.90.37
committed H.264 and HEVC streams0.21 to 0.41

This table is the v0.3.0 measurement, when HEVC ran on gen2brain/h265; the HEVC speed since v0.10.0 is on the history page. The profiles put the hot spots mostly in the wrapped libraries: the old go.264 wrapper had no arm64 assembly kernels (h264 has them), and gen2brain/h265 took its assembly motion compensation at 8 bits only. The measurement is on arm64; an amd64 one is still needed. The audio decoders are not measured yet.