Upscaling frames
Upscaling frames
upscale.Upscale(src, w, h, opt) in go-avkit/avkit
(since v0.21.0) is a pure-Go port of AMD FidelityFX Super Resolution 1.0 (EASU edge-adaptive
upsampling, then RCAS sharpening) from ffx_fsr1.h, MIT licence. It takes an
image.Image (a decoder’s *image.YCbCr, or RGBA, NRGBA, Gray), scales 1x to 4x per
axis and returns an *image.RGBA. Sizes are checked before allocation; downscaling is
refused. Options.Sharpness is the RCAS attenuation in stops (0 sharpest, negative = no
RCAS, default 0.2).
Accuracy
Against a float64 transliteration of the reference, the float32 pipeline stays within
ErrorBound = 2e-4 on a 0..1 scale: 2.7e-5 measured over 662 520 samples, up to 1.1e-4 where
the compiler fuses multiply-adds. The vector rows (below) are tested against the scalar code
under the same bound.
v0.25.0 makes the oracle public (upscale/fsrref) and measures against it: the worst error is
5.7e-6 on the oracle corpus, on the CPU and on both GPU back ends.
ErrorBound is not a bound on every input. EASU measures an edge as
|d-b| / max(|d-c|, |c-b|) on the lumas of three neighbours, which is 0/0 where different
colours share a luma; the reference defines no answer there and each precision answers with its
rounding noise. On noise of at most 3 levels of 255, 3.6 % of samples have such a neighbourhood
and 0.9 % are outside ErrorBound, by up to 4.7e-3. This is present since v0.21.0, on the scalar
and vector CPU paths and on Metal alike, and is not an arithmetic defect. v0.25.1 states the
contract: ErrorBound on every sample whose analysis is well conditioned (1.13 million measured,
worst 3.4e-5), and ErrorBound plus the range of the four nearest source colours on any sample
(TestOracleLowContrast).
Quality
Measured with ffmpeg 6.1.1’s psnr and ssim filters. The ground truth is a real frame,
scaled with ffmpeg area, downscaled by the factor and upscaled back. PSNR dB / SSIM:
| Source (factor) | bilinear | bicubic | lanczos | FSR EASU | FSR EASU+RCAS 0.2 |
|---|---|---|---|---|---|
| Wikipedia 1080p (2x) | 22.07 / 0.863 | 22.64 / 0.881 | 22.71 / 0.877 | 22.67 / 0.889 | 22.75 / 0.892 |
| Slides 1080p (2x) | 25.03 / 0.923 | 26.43 / 0.939 | 26.64 / 0.939 | 26.40 / 0.944 | 26.47 / 0.945 |
| KristenAndSara 720p (2x) | 31.68 / 0.950 | 33.22 / 0.961 | 33.59 / 0.961 | 33.38 / 0.961 | 33.29 / 0.956 |
| GlassHalf 1080p (2x) | 31.55 / 0.936 | 32.42 / 0.947 | 32.51 / 0.946 | 32.99 / 0.953 | 32.91 / 0.955 |
| SolLevanteFace 1080p (1.5x) | 37.23 / 0.929 | 38.72 / 0.950 | 39.24 / 0.955 | 38.93 / 0.951 | 38.56 / 0.953 |
FSR has the highest SSIM on four of five sources; lanczos is slightly ahead on PSNR on some.
Speed
One core of a Ryzen 9 7950X (load about 2), 1280x720 to 1920x1080, milliseconds per frame:
| Scaler | ms per frame |
|---|---|
ffmpeg 6.1.1 scale bilinear | 3.3 |
| ffmpeg bicubic | 2.0 |
| ffmpeg lanczos | 2.8 |
upscale, scalar (default build) | 242 (EASU alone 193) |
upscale, GOEXPERIMENT=simd (v0.22.0) | 82 (EASU alone 73) |
upscale, GOEXPERIMENT=simd (v0.23.0) | 55 (EASU alone 44) |
upscale, GOEXPERIMENT=simd (v0.24.0) | 37 (EASU alone 28) |
ffmpeg’s figures subtract the source and the pixel conversion. The vector build uses the
portable Go 1.27 simd package (AVX2 and AVX-512 on amd64, NEON on arm64); it is chosen at
start-up, so the default build is unchanged. v0.23.0 gathers each source row’s taps once
(not once per output pixel), computes the edge analysis once per source pixel and packs
RGBA with vectors; v0.24.0 gathers with AVX-512 permutes, converts YCbCr inline and hoists
the per-row broadcasts.
Several cores, v0.25.0
v0.25.0 adds upscale.NewScaler(opt) and (*Scaler).Upscale(dst, src), which writes into a
caller-owned *image.RGBA. Options.Workers splits the frame into bands of rows, one goroutine
each (0 = GOMAXPROCS, at most MaxWorkers = 64); the bytes are the same for every worker count.
A dst whose buffer does not hold its rectangle, or whose stride is under a row, is refused
with ErrDst. With one worker a warm frame allocates nothing; with several, -benchmem
reports 0 allocs/op and 1 to 522 B/op, an allocation every few frames whose origin is not traced.
1280x720 YCbCr 4:2:0 to 1920x1080 RGBA, sharpness 0.2, GOEXPERIMENT=simd (AVX-512), Ryzen 9 7950X, Go 1.27.4,
milliseconds per frame:
| Threads | upscale v0.24.0 | upscale v0.25.0 | ffmpeg 6.1.1 lanczos |
|---|---|---|---|
| 1 | 37 | 28.7 to 29.3 | 2.77 |
| 2 | 14.5 | ||
| 4 | 7.5 | ||
| 8 | 5.4 | 0.20 | |
| 16 | 3.6 | 0.20 |
The ffmpeg figures for several threads are coarse (a difference of about 0.1 s over 300 frames), an order of magnitude and not a precise figure. FSR is still 10.4 times slower than ffmpeg’s lanczos on one core and about 18 times on 16 threads. This is the remaining gap (go-avkit/avkit#141), and a CPU is not expected to close it: a separable filter applies fixed taps along each axis, where EASU computes for every output pixel an edge direction from twelve taps, then twelve rotated, anisotropic weights and a division. It is still an open defect. With 16 workers a 1080p frame takes 3.6 ms, which fits a 60 Hz frame (16.7 ms); on one core it does not.
The v0.25.0 one-core gain comes from resolving the taps once per output row, factoring the
rotation out of the twelve tap offsets and vectorising the luma and the last pixels of an RCAS row.
Sample positions are now computed in integers (fsrref.Phase), which costs 10 % on one core
(26.2 ms before it) for a reason not established; output pixels at positions that were on an
integer in float differ from v0.24.0.
GPU
A GPU compute path (Vulkan and Metal; module upscale/gpu) is released as upscale/gpu/v0.1.0
(go-avkit/avkit#151). On Vulkan it measured 5.77 to 5.87 ms per frame on a RADV NAVI32 including
upload and readback, about 2.0 ms for the dispatch alone. Metal was checked for correctness only,
not timed.
Today upscale suits stills, offline work and small frames on one core, and 60 Hz 1080p only
with many workers.
Reproduce
tools/fsrbench in the avkit repository (a module of its own; ffmpeg on the PATH):
go run ./tools/fsrbench quality --gt in.png --dir <workdir>
go run ./tools/fsrbench speed --core 9
GOEXPERIMENT=simd go test -bench 720to1080 ./upscale