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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testdata/** -text
165 changes: 165 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
name: ci

on:
push:
branches: [main]
pull_request:
branches: [main]

permissions:
contents: read

# CI pins the Go release below (Renovate moves it). The go.mod floor is 1.27.1.
jobs:
# Every file must be gofmt-clean.
gofmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
with:
go-version: '1.27.3'
- name: gofmt
run: |
unformatted=$(gofmt -l .)
if [ -n "$unformatted" ]; then
echo "::error::these files are not gofmt-clean:"; echo "$unformatted"
exit 1
fi

# Build, vet, race-test, and gate every statement of the library: covergate
# reads the profile itself, because `go tool cover -func` prints 99.97% as
# 100.0%. The tools module (the ffmpeg oracle harness and covergate) is tested
# on the same three systems; its own tests skip what needs ffmpeg.
test:
name: test (${{ matrix.os }})
runs-on: ${{ matrix.os }}
# PowerShell splits `-coverprofile=cover.out` at the dot.
defaults:
run:
shell: bash
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
with:
go-version: '1.27.3'
- run: go version
- run: go vet ./...
- run: go build ./...
- name: Test with the race detector and write the coverage profile
run: go test -race -coverprofile=cover.out ./...
- name: Coverage gate, every statement
working-directory: tools
run: go run ./covergate ../cover.out
- name: Tools module
working-directory: tools
run: go vet ./... && go test ./...

# A stream is untrusted input. The corpus in testdata/fuzz runs as ordinary
# tests above; this job also mutates it for a while. A crash fails the job and
# go test prints the input it found, to be committed to the corpus with its fix.
fuzz:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
with:
go-version: '1.27.3'
- run: go test -run XXX -fuzz FuzzDecodeSample -fuzztime 60s .
- run: go test -run XXX -fuzz FuzzDecodeAnnexB -fuzztime 60s .
- run: go test -run XXX -fuzz FuzzParseAVCC -fuzztime 20s .

vuln:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
with:
go-version: '1.27.3'
- run: go install golang.org/x/vuln/cmd/govulncheck@latest
- run: govulncheck ./...
- run: govulncheck ./...
working-directory: tools

# The six supported 64-bit targets: amd64/arm64 natively, the rest under
# qemu-user. s390x is big-endian, so it is also the byte-order gate for the
# length prefixes and the avcC reader.
arch-native:
name: arch ${{ matrix.label }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- { os: ubuntu-latest, label: amd64 }
- { os: ubuntu-24.04-arm, label: arm64 }
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
with:
go-version: '1.27.3'
- run: go env GOARCH && go test ./...

# The one 32-bit target, where int is 32 bits wide: a length converted to int
# before it is compared wraps only here. It runs natively on the amd64 runner.
# Coverage is gated on the 64-bit lanes; this one only vets and tests.
arch-386:
name: arch 386
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
with:
go-version: '1.27.2'
- run: GOARCH=386 go vet ./...
- run: GOARCH=386 go test ./...

arch-qemu:
name: arch ${{ matrix.goarch }} (qemu)
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- { goarch: riscv64, qemu: qemu-riscv64-static }
# loong64 carries a known upstream code-generation defect,
# golang/go#81000, that this lane cannot fully rule out: float to
# integer conversions are declared with a floating-point output on
# LOONG64, and an integer that inherits an FP register is spilled
# with an 8-byte store, which destroys its neighbour when it is a
# byte. go.264 stores pictures as bytes. If this lane goes red on
# loong64 alone, suspect that before the code. See
# go-avkit/avkit .github/workflows/ci.yml for the full account.
- { goarch: loong64, qemu: qemu-loongarch64-static }
- { goarch: ppc64le, qemu: qemu-ppc64le-static }
- { goarch: s390x, qemu: qemu-s390x-static }
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
with:
go-version: '1.27.3'
# `qemu-user-static` is a virtual package on Ubuntu 26.04; install the
# dynamic emulators, which is all `go test -exec` needs, and accept either
# name below.
- name: install qemu user-mode emulators
timeout-minutes: 10
run: sudo apt-get update && sudo apt-get install -y qemu-user qemu-user-binfmt
- name: Test under qemu (${{ matrix.goarch }})
run: |
base="${{ matrix.qemu }}"
q=""
for cand in "$base" "${base%-static}"; do
command -v "$cand" >/dev/null 2>&1 && q="$cand" && break
done
# A missing emulator FAILS this lane: a lane that reports success for
# an architecture it never ran is the outcome this matrix rules out.
if [ -z "$q" ]; then
echo "::error::no qemu user emulator for ${{ matrix.goarch }} (looked for $base and ${base%-static})"
exit 1
fi
echo "running ${{ matrix.goarch }} under $q"
GOARCH=${{ matrix.goarch }} go test -exec="$q" ./...
107 changes: 106 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,107 @@
# decode
H.264 video decoder, bit-exact against ffmpeg

[![ci](https://github.com/go-avkit/decode/actions/workflows/ci.yml/badge.svg)](https://github.com/go-avkit/decode/actions/workflows/ci.yml)

Video decoders for [avkit](https://github.com/go-avkit/avkit), pure Go, no cgo.
The first codec is **H.264** (ITU-T H.264 | ISO/IEC 14496-10). Wave D1 of
[go-avkit/avkit#89](https://github.com/go-avkit/avkit/issues/89).

```go
r, _ := container.NewReader(file) // MP4, Matroska, ...
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)
}
```

Streaming: `decode.NewTrack(trackConfig, cfg)` or `decode.NewAVCC(avcC, cfg)`,
then `DecodeSample(sample.Data, pts)` per sample and `Flush()` at the end.
Raw `.264` files: `decode.New(cfg, 4)` and `DecodeAnnexB`.

`Frame` carries the planes, strides, size, `Chroma` format, `BitDepth` and
`PTS`. Its field names match `go-videodsp/yuv.Picture` for 8-bit 4:2:0, so
converting to RGBA is a field copy; `yuv.Picture` has no bit depth or time, which
is why it is not used as is.

## What is decoded

The decoding is [`github.com/oops1/go.264`](https://github.com/oops1/go.264)
(BSD-2-Clause, pure Go with assembly kernels), pinned and forced onto its
software path. Where it is wrong it is fixed upstream by pull request.

| Profile | State |
| --- | --- |
| Constrained Baseline, Baseline without FMO/ASO | bit-exact |
| Main, progressive (CAVLC and CABAC, B pictures, weighted prediction, multiple references, long-term references, MMCO) | bit-exact |
| High, 4:2:0 8-bit, progressive (8x8 transform, scaling matrices) | bit-exact on the streams below; no JVT FRExt stream measured yet |
| Interlaced (PAFF, MBAFF), FMO/ASO, Extended (SP/SI, data partitioning) | refused with `ErrUnsupported` |
| High 10, 4:2:2, 4:4:4, lossless | refused with `ErrUnsupported` |

`Frame.BitDepth` and `Chroma` exist so the later profiles need no API change.

## Measured

Every picture's MD5 against `ffmpeg -f framemd5 -pix_fmt yuv420p`, single
thread, and against the reference yuv shipped in the suite where there is one.

* **ITU-T H.264.1 conformance suite**, AVCv1: 81 bitstreams tried, 73 match ffmpeg (and
the suite's yuv where it ships one): 69 with go.264 v1.12.0 as pinned here, and
4 more (MR2..MR5_TANDBERG) only with
[oops1/go.264#2](https://github.com/oops1/go.264/pull/2) "decoder: handle MMCO 5
and gaps in frame_num"; they are listed in `pendingUpstream` until it is
released. The other 8 are not claimed: 5 interlaced, 3 use FMO/ASO (ffmpeg
cannot decode two of them either). ffmpeg's MD5s are in `testdata/jvt`; the
bitstreams are not in the repository (their redistribution terms are not
stated), so `TestJVTConformance` runs when `DECODE_JVT_DIR` points at them
(`go run ./tools/jvt fetch` downloads them).
* **29 streams in `testdata/streams`** (from go.264, BSD-2-Clause; Baseline,
Main, High) run in CI against ffmpeg's MD5s in the `.md5` files.
* **5 real files** (MP4 and Matroska, 720x300 to 1920x1080, x264), 900 pictures
each: `testdata/real-files.txt`, hashes only.

## Bounds

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; go.264 also refuses pictures past its level ceiling and
bounds its reference frames by `max_num_ref_frames`. `TestBombRefusedUnderMemoryLimit`
runs three bomb SPSs under a 256 MiB memory limit and asserts a refusal and under
8 MiB allocated. `FuzzParseAVCC`, `FuzzDecodeSample` and `FuzzDecodeAnnexB` have a
committed corpus (`testdata/fuzz`, from `go run ./tools/jvt seeds`) and run in CI.

## Bibliography

* ITU-T H.264 (V16, 06/2026, in force), opened as the PDF. Read and checked:
7.3.2.1 (SPS syntax entry), 7.4.3 (frame_num semantics: a picture with
memory_management_control_operation 5 is inferred to have had frame_num 0;
Table 7-9), 8.2.1 (tempPicOrderCnt after operation 5; 8.2.1.1 to 8.2.1.3
prevPicOrderCntMsb/Lsb and prevFrameNumOffset after it), 8.2.5.1 to 8.2.5.4
(marking, gaps in frame_num, sliding window) and C.4.4 (output order and
operation 5). The fix upstream (oops1/go.264#2) follows those clauses,
including the rebased prevPicOrderCntLsb. ffmpeg (libavcodec h264_refs.c,
h264_picture.c, h264_slice.c, h264_parse.c, opened) sets frame_num to 0 on
MMCO_RESET and takes the frame order count as the Min of the two field
counts; I found no rebase of prev_poc_lsb in those files. The spec text is
the reference.
* ITU-T H.264.1, conformance specification and the bitstream list at
<https://www.itu.int/wftp3/av-arch/jvt-site/draft_conformance/AVCv1/> (listing
and each stream's readme opened).
* ISO/IEC 14496-15 (avcC): paywalled, **not opened**. The record layout is that
of mp4ff v0.62.0 `avc/avcdecoderconfigurationrecord.go` (opened), which
cites 14496-15 itself; it is fuzzed here.

## Performance

Not yet measured against ffmpeg: the machine's 1-minute load was 36 to 50 during
this work, above the limit for a measurement (8). Tracked in the issues.

## Development

```
go test -race ./... # library, 100% of statements
cd tools && go run ./covergate ../cover.out # the gate (reads the profile)
cd tools && go test ./... # oracle harness
```

BSD-3-Clause. The streams in `testdata/streams` are from go.264 (BSD-2-Clause,
`testdata/streams/LICENSE.go264`).
Loading
Loading