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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1430,10 +1430,12 @@ PSNR on some.

Speed, one core of a Ryzen 9 7950X (load about 2), 1280x720 to 1920x1080, ms per
frame: ffmpeg 6.1.1 `scale` bilinear 3.3, bicubic 2.0, lanczos 2.8 (source and
conversion subtracted) against `upscale` 239 (EASU alone 191); 1080p to 2160p takes
935 ms. **This is scalar Go and about 90 times slower than ffmpeg's SIMD scaler:
it is an open defect** (a vector kernel is tracked in the repository's issues), so
for now it suits stills, offline work and small frames, not real-time 1080p.
conversion subtracted) against `upscale` 242 scalar (EASU alone 193) and **82 with
`GOEXPERIMENT=simd`** (EASU alone 73), the portable Go 1.27 `simd` package on
AVX2/AVX-512 and NEON; the scalar build is the default and the two agree within
`ErrorBound`. Still about 30 times slower than ffmpeg's hand-written scaler, which
is an open defect (the 12-tap gather per output pixel dominates), so it suits
stills, offline work and small frames, not real-time 1080p.

## Guarantees

Expand Down
29 changes: 21 additions & 8 deletions upscale/easu.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@ func rcpMed(a float32) float32 {
return b * (-b*a + 2)
}

func sat(a float32) float32 { return min(1, max(0, a)) }
// fmin and fmax are the builtins, named so the kernels read alike. (The
// comparison forms were measured 28 % slower on amd64: the branches
// mispredict on image data.)
func fmin(a, b float32) float32 { return min(a, b) }

func fmax(a, b float32) float32 { return max(a, b) }

// clamp4 limits v to the range of a, b, c, d.
func clamp4(v, a, b, c, d float32) float32 {
lo, hi := fmin(fmin(a, b), fmin(c, d)), fmax(fmax(a, b), fmax(c, d))
return fmin(fmax(v, lo), hi)
}

func sat(a float32) float32 { return fmin(1, fmax(0, a)) }

func abs32(a float32) float32 { return math.Float32frombits(math.Float32bits(a) &^ (1 << 31)) }

Expand All @@ -30,13 +43,13 @@ func abs32(a float32) float32 { return math.Float32frombits(math.Float32bits(a)
// b c d
// e
func easuSet(dirX, dirY, ln *float32, w, a, b, c, d, e float32) {
lenX := rcpLo(max(abs32(d-c), abs32(c-b)))
lenX := rcpLo(fmax(abs32(d-c), abs32(c-b)))
dx := d - b
*dirX += dx * w
lenX = sat(abs32(dx) * lenX)
lenX *= lenX
*ln += lenX * w
lenY := rcpLo(max(abs32(e-c), abs32(c-a)))
lenY := rcpLo(fmax(abs32(e-c), abs32(c-a)))
dy := e - a
*dirY += dy * w
lenY = sat(abs32(dy) * lenY)
Expand All @@ -48,7 +61,7 @@ func easuSet(dirX, dirY, ln *float32, w, a, b, c, d, e float32) {
func easuTap(offx, offy, dirx, diry, lx, ly, lob, clp float32) float32 {
vx := (offx*dirx + offy*diry) * lx
vy := (offx*-diry + offy*dirx) * ly
d2 := min(vx*vx+vy*vy, clp)
d2 := fmin(vx*vx+vy*vy, clp)
wB := 2.0/5.0*d2 - 1
wA := lob*d2 - 1
wB *= wB
Expand Down Expand Up @@ -97,7 +110,7 @@ func easuPixel(p *planes, base int, ppx, ppy float32) (outR, outG, outB float32)
dirY *= dirR
ln *= 0.5
ln *= ln
stretch := (dirX*dirX + dirY*dirY) * rcpLo(max(abs32(dirX), abs32(dirY)))
stretch := (dirX*dirX + dirY*dirY) * rcpLo(fmax(abs32(dirX), abs32(dirY)))
lx := 1 + (stretch-1)*ln
ly := 1 + -0.5*ln
lob := 0.5 + ((1.0/4.0-0.04)-0.5)*ln
Expand All @@ -114,9 +127,9 @@ func easuPixel(p *planes, base int, ppx, ppy float32) (outR, outG, outB float32)
}
rcp := 1 / aW
// Dering against the four nearest: f, g, j, k.
r := min(max(aR*rcp, min(min(p.r[iF], p.r[iG]), min(p.r[iJ], p.r[iK]))), max(max(p.r[iF], p.r[iG]), max(p.r[iJ], p.r[iK])))
g := min(max(aG*rcp, min(min(p.g[iF], p.g[iG]), min(p.g[iJ], p.g[iK]))), max(max(p.g[iF], p.g[iG]), max(p.g[iJ], p.g[iK])))
b := min(max(aB*rcp, min(min(p.b[iF], p.b[iG]), min(p.b[iJ], p.b[iK]))), max(max(p.b[iF], p.b[iG]), max(p.b[iJ], p.b[iK])))
r := clamp4(aR*rcp, p.r[iF], p.r[iG], p.r[iJ], p.r[iK])
g := clamp4(aG*rcp, p.g[iF], p.g[iG], p.g[iJ], p.g[iK])
b := clamp4(aB*rcp, p.b[iF], p.b[iG], p.b[iJ], p.b[iK])
return r, g, b
}

Expand Down
178 changes: 178 additions & 0 deletions upscale/easu_simd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//go:build goexperiment.simd

package upscale

import "simd"

// Vector EASU (GOEXPERIMENT=simd, Go 1.27): the portable simd package maps to
// AVX2 on amd64 and NEON on arm64. The 12 taps of each output pixel are
// gathered by scalar loads into [12][maxLanes] stack arrays (there is no
// 2D gather in hardware worth using here), then the direction, length and
// weight arithmetic runs one pixel per lane. Every expression is the scalar
// one of easuPixel, evaluated in the same order, so results differ from it
// only by the compiler's FMA contraction.

const maxLanes = 16

// useVec reports whether easuRowVec can run here: the hardware vector must
// fit the stack buffers.
func useVec() bool { return bc(0).Len() <= maxLanes }

func init() {
if useVec() {
easuRowFn, rcasRowFn = easuRowVec, rcasRowVec
}
}

type tapBuf [12][maxLanes]float32

func ld(a *[maxLanes]float32) simd.Float32s { return simd.LoadFloat32s(a[:]) }

func bc(v float32) simd.Float32s { return simd.BroadcastFloat32s(v) }

func vRcpLo(a simd.Float32s) simd.Float32s {
return simd.BroadcastUint32s(0x7ef07ebb).Sub(a.ToBits()).BitsToFloat32()
}

func vRsqLo(a simd.Float32s) simd.Float32s {
return simd.BroadcastUint32s(0x5f347d74).Sub(a.ToBits().ShiftAllRight(1)).BitsToFloat32()
}

func vSat(a simd.Float32s) simd.Float32s { return a.Max(bc(0)).Min(bc(1)) }

// vEasuSet is easuSet, one pixel per lane.
func vEasuSet(dirX, dirY, ln, w, a, b, c, d, e simd.Float32s) (simd.Float32s, simd.Float32s, simd.Float32s) {
lenX := vRcpLo(d.Sub(c).Abs().Max(c.Sub(b).Abs()))
dx := d.Sub(b)
dirX = dirX.Add(dx.Mul(w))
lenX = vSat(dx.Abs().Mul(lenX))
lenX = lenX.Mul(lenX)
ln = ln.Add(lenX.Mul(w))
lenY := vRcpLo(e.Sub(c).Abs().Max(c.Sub(a).Abs()))
dy := e.Sub(a)
dirY = dirY.Add(dy.Mul(w))
lenY = vSat(dy.Abs().Mul(lenY))
lenY = lenY.Mul(lenY)
ln = ln.Add(lenY.Mul(w))
return dirX, dirY, ln
}

// easuRowVec computes one output row of EASU: the pixels 0..ow-1, whose "f"
// taps are at source row fy, fractional row position py.
func easuRowVec(p *planes, ax axis, fy int, py float32, ow int, dr, dg, db []float32) {
var tl, tr, tg, tb tapBuf
var ppxs, out [maxLanes]float32
n := bc(0).Len()
one, zero := bc(1), bc(0)
ppy := bc(py)
for x0 := 0; x0 < ow; x0 += n {
cnt := min(n, ow-x0)
for j := 0; j < n; j++ {
ox := x0 + min(j, cnt-1) // spare lanes repeat the last pixel
base := p.at(int(ax.fp[ox]), fy)
ppxs[j] = ax.pp[ox]
for k, off := range p.tapOff {
i := base + off
tl[k][j], tr[k][j], tg[k][j], tb[k][j] = p.l[i], p.r[i], p.g[i], p.b[i]
}
}
ppx := ld(&ppxs)
// Luma taps by the reference's names.
bL, cL, iL, jL, fL, eL := ld(&tl[0]), ld(&tl[1]), ld(&tl[2]), ld(&tl[3]), ld(&tl[4]), ld(&tl[5])
kL, lL, hL, gL, oL, nL := ld(&tl[6]), ld(&tl[7]), ld(&tl[8]), ld(&tl[9]), ld(&tl[10]), ld(&tl[11])

ipx, ipy := one.Sub(ppx), one.Sub(ppy)
dirX, dirY, ln := zero, zero, zero
dirX, dirY, ln = vEasuSet(dirX, dirY, ln, ipx.Mul(ipy), bL, eL, fL, gL, jL)
dirX, dirY, ln = vEasuSet(dirX, dirY, ln, ppx.Mul(ipy), cL, fL, gL, hL, kL)
dirX, dirY, ln = vEasuSet(dirX, dirY, ln, ipx.Mul(ppy), fL, iL, jL, kL, nL)
dirX, dirY, ln = vEasuSet(dirX, dirY, ln, ppx.Mul(ppy), gL, jL, kL, lL, oL)

dirR := dirX.Mul(dirX).Add(dirY.Mul(dirY))
small := dirR.Less(bc(1.0 / 32768.0))
dirR = one.IfElse(small, vRsqLo(dirR))
dirX = one.IfElse(small, dirX)
dirX = dirX.Mul(dirR)
dirY = dirY.Mul(dirR)
ln = ln.Mul(bc(0.5))
ln = ln.Mul(ln)
stretch := dirX.Mul(dirX).Add(dirY.Mul(dirY)).Mul(vRcpLo(dirX.Abs().Max(dirY.Abs())))
lx := one.Add(stretch.Sub(one).Mul(ln))
ly := one.Add(bc(-0.5).Mul(ln))
lob := bc(0.5).Add(bc((1.0/4.0 - 0.04) - 0.5).Mul(ln))
clp := vRcpLo(lob)

aR, aG, aB, aW := zero, zero, zero, zero
for k := range tapDX {
offx, offy := bc(tapDX[k]).Sub(ppx), bc(tapDY[k]).Sub(ppy)
vx := offx.Mul(dirX).Add(offy.Mul(dirY)).Mul(lx)
vy := offx.Mul(dirY.Neg()).Add(offy.Mul(dirX)).Mul(ly)
d2 := vx.Mul(vx).Add(vy.Mul(vy)).Min(clp)
wB := bc(2.0 / 5.0).Mul(d2).Sub(one)
wA := lob.Mul(d2).Sub(one)
wB = wB.Mul(wB)
wA = wA.Mul(wA)
wB = bc(25.0 / 16.0).Mul(wB).Sub(bc(25.0/16.0 - 1.0))
w := wB.Mul(wA)
aR = aR.Add(ld(&tr[k]).Mul(w))
aG = aG.Add(ld(&tg[k]).Mul(w))
aB = aB.Add(ld(&tb[k]).Mul(w))
aW = aW.Add(w)
}
rcp := one.Div(aW)
vFinish(aR, rcp, &tr, &out, dr[x0:x0+cnt])
vFinish(aG, rcp, &tg, &out, dg[x0:x0+cnt])
vFinish(aB, rcp, &tb, &out, db[x0:x0+cnt])
}
}

// vFinish normalises one channel and dering-clamps it to the range of the
// four nearest pixels f, g, j, k (taps 4, 9, 3, 6).
func vFinish(a, rcp simd.Float32s, t *tapBuf, out *[maxLanes]float32, dst []float32) {
f, g, j, k := ld(&t[4]), ld(&t[9]), ld(&t[3]), ld(&t[6])
lo, hi := f.Min(g).Min(j.Min(k)), f.Max(g).Max(j.Max(k))
a.Mul(rcp).Max(lo).Min(hi).Store(out[:])
copy(dst, out[:])
}

// vnmax is nmax, one pixel per lane: a NaN operand yields the other one.
func vnmax(a, b simd.Float32s) simd.Float32s {
r := a.Max(b)
r = b.IfElse(a.NotEqual(a), r)
return a.IfElse(b.NotEqual(b), r)
}

// rcasRowVec is rcasRow for the whole row: the interior in vectors, the
// first pixel and the ragged tail by the scalar code (edge clamping).
func rcasRowVec(w int, sharp float32, u, m, d, out rowBuf) {
n := bc(0).Len()
x := min(1, w)
rcasRow(w, 0, x, sharp, u.r, u.g, u.b, m.r, m.g, m.b, d.r, d.g, d.b, out.r, out.g, out.b)
one, four, vsharp := bc(1), bc(4), bc(sharp)
for ; x+n+1 <= w; x += n {
var lobe simd.Float32s
var chans [3]struct{ b, dd, e, f, h simd.Float32s }
for c, p := range [3][4][]float32{{u.r, m.r, d.r, out.r}, {u.g, m.g, d.g, out.g}, {u.b, m.b, d.b, out.b}} {
b, dd, e, f, h := simd.LoadFloat32s(p[0][x:]), simd.LoadFloat32s(p[1][x-1:]), simd.LoadFloat32s(p[1][x:]), simd.LoadFloat32s(p[1][x+1:]), simd.LoadFloat32s(p[2][x:])
chans[c] = struct{ b, dd, e, f, h simd.Float32s }{b, dd, e, f, h}
mn4, mx4 := b.Min(dd).Min(f.Min(h)), b.Max(dd).Max(f.Max(h))
hitMin := mn4.Min(e).Mul(one.Div(four.Mul(mx4)))
hitMax := one.Sub(mx4.Max(e)).Mul(one.Div(four.Mul(mn4).Sub(four)))
l := vnmax(hitMin.Neg(), hitMax)
if c == 0 {
lobe = l
} else {
lobe = vnmax(lobe, l)
}
}
lobe = vnmax(bc(-rcasLimit), lobe.Min(bc(0))).Mul(vsharp)
den := four.Mul(lobe).Add(one)
bb := simd.BroadcastUint32s(0x7ef19fff).Sub(den.ToBits()).BitsToFloat32()
rcp := bb.Mul(bb.Neg().Mul(den).Add(bc(2)))
for c, o := range [3][]float32{out.r, out.g, out.b} {
s := chans[c]
lobe.Mul(s.b).Add(lobe.Mul(s.dd)).Add(lobe.Mul(s.h)).Add(lobe.Mul(s.f)).Add(s.e).Mul(rcp).Store(o[x:])
}
}
rcasRow(w, x, w, sharp, u.r, u.g, u.b, m.r, m.g, m.b, d.r, d.g, d.b, out.r, out.g, out.b)
}
48 changes: 48 additions & 0 deletions upscale/easu_simd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//go:build goexperiment.simd

package upscale

import (
"image"
"math/rand"
"runtime"
"testing"
)

// TestVectorMatchesScalar compares the vector EASU row with easuPixel on
// noise, edges and flat patches, for widths that are not multiples of the
// vector length.
func TestVectorMatchesScalar(t *testing.T) {
if (runtime.GOARCH == "amd64" || runtime.GOARCH == "arm64") && !useVec() {
t.Fatal("vector kernel unavailable on a SIMD architecture")
}
rng := rand.New(rand.NewSource(7))
for _, c := range []struct{ sw, sh, ow, oh int }{{20, 14, 31, 21}, {17, 9, 17, 9}, {33, 12, 64, 24}, {5, 5, 9, 9}} {
src := image.NewNRGBA(image.Rect(0, 0, c.sw, c.sh))
for i := range src.Pix {
switch {
case i%97 < 30:
src.Pix[i] = 128 // flat patches
case i%2 == 0:
src.Pix[i] = uint8(rng.Intn(256))
default:
src.Pix[i] = uint8(i)
}
}
p := fromImage(src)
ax, ay := newAxis(c.sw, c.ow), newAxis(c.sh, c.oh)
dr, dg, db := make([]float32, c.ow), make([]float32, c.ow), make([]float32, c.ow)
for oy := 0; oy < c.oh; oy++ {
fy, py := int(ay.fp[oy]), ay.pp[oy]
easuRowVec(p, ax, fy, py, c.ow, dr, dg, db)
for ox := 0; ox < c.ow; ox++ {
r, g, b := easuPixel(p, p.at(int(ax.fp[ox]), fy), ax.pp[ox], py)
for _, d := range []float32{abs32(r - dr[ox]), abs32(g - dg[ox]), abs32(b - db[ox])} {
if d > ErrorBound {
t.Fatalf("%dx%d->%dx%d (%d,%d): vector differs from scalar by %g", c.sw, c.sh, c.ow, c.oh, ox, oy, d)
}
}
}
}
}
}
Loading
Loading