diff --git a/docs/perf/speed-log.md b/docs/perf/speed-log.md index ada66fd..d6e11dd 100644 --- a/docs/perf/speed-log.md +++ b/docs/perf/speed-log.md @@ -9,6 +9,7 @@ crowd_run 1080p 300-frame clips, median µs per frame. | v0.1.0 (M0) | 7210 | 7581 | 4969 | | horizontal 8-tap with VPMADDUBSW (from govpx 43f826d8) | 6944 (1.038x) | 7304 (1.038x) | 4961 (1.002x) | | VP9 loop filters, AVX2 (`go-videodsp/kernels` v0.22.0: 4, 8 and 16 wide, H and V, Dual edges) | 6090 (1.172x, v0.2.0 7136 in the same run) | 6151 (1.167x, v0.2.0 7180) | not run, VP8 does not use it | +| VP9 8-tap convolution, AVX2 (`go-videodsp/kernels` v0.23.0 `mc`: horizontal, vertical, and their averaging forms; the 2-D paths run through them) | 5332 (1.181x, v0.3.0 6296 in the same run) | 5373 (1.182x, v0.3.0 6351) | not run, VP8 does not use it | libvpx single core, same clips (M0 protocol): 2623, 2610, 2427. @@ -18,6 +19,13 @@ per frame) and is not used. Kernel speed against the scalar reference, per 16-li (`go-videodsp/kernels` `docs/bench/vp9-lf-zen4-avx2.txt`): H4 8.5x, V4 5.8x, H8 7.7x, V8 5.5x, H16 7.8x, V16 5.4x. The decoder is still about 2.3 times libvpx's time per frame. +The convolution row was measured on core 14 at load 2.0 to 2.6, six interleaved rounds against the +`v0.3.0` test binary, `BenchmarkDecodeClip`. Kernel speed against the scalar reference per block on +Zen 4 (`go-videodsp/kernels` `docs/bench/vp9-conv-zen4-avx2.txt`): H and V 2.5x (4x4), 8x (8x8), +24x (16x16), 67x (32x32), 88x (64x64); 2-D 64x64 61x. The 752 libvpx vectors (both test +functions) pass. The horizontal kernel is tried before the `VPMADDUBSW` one of M1; the vertical +pass and the two averaging forms are new. The split of the gain between them was not measured. + ## Tried and not kept - VP8 six-tap horizontal through the same kernel (taps padded with two zeros): 5131 against @@ -36,4 +44,11 @@ H16 7.8x, V16 5.4x. The decoder is still about 2.3 times libvpx's time per frame - Loop filter (K1), the parts left: the single-edge `Thr` entry points (`vpxLpf*4/8/16`, one 8-line edge) and the high-bit-depth paths still use the scalar or `pixel.LoopFilter` code; the NEON kernels are an open issue in `go-videodsp/kernels`. The VP8 filters are untouched. -- The move of the convolution into `go-videodsp/kernels`. +- Convolution, the parts left: the fused 2-D kernel `mc.VP9Convolve8` and `mc.VP9Convolve8Avg` is + not wired (their 71x64 stack intermediate is zeroed on every call, which costs about as much as + the passes at 4x4 and 8x8; the decoder's separate horizontal and vertical passes through its + pooled scratch already run the kernels). Fusing the averaging into the vertical pass of the + 2-D average (horizontal into the scratch, `mc.VP9Convolve8AvgV` into dst, instead of the 2-D + result then `VpxConvolveAvg`) was tried and measured 1.004x on the 1-tile clip, under the 5 % + bar, and not kept. Widths other than 4, 8, 16 and the multiples of 32, filter rows the + exactness proof rejects, and non-unit steps keep the old code. NEON kernels: go-videodsp/kernels#42. diff --git a/go.mod b/go.mod index 0e93cc7..6647365 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/go-avkit/vpx go 1.27.4 -require github.com/go-videodsp/kernels v0.22.0 +require github.com/go-videodsp/kernels v0.23.0 diff --git a/go.sum b/go.sum index f39949f..2bb80d3 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/go-videodsp/kernels v0.22.0 h1:YXfSOwzjUb0xsoG4SpS3kYXsJ/r5bcr1JEVG6wLLkq4= -github.com/go-videodsp/kernels v0.22.0/go.mod h1:+nQCn5NvYhjT8DtUqSplgir5dB4D66rqiGd8R3fimBE= +github.com/go-videodsp/kernels v0.23.0 h1:VoDJQSOTnHef9ZAWPy/BgZ9LYZLNSn3O86CCVTjJnCU= +github.com/go-videodsp/kernels v0.23.0/go.mod h1:+nQCn5NvYhjT8DtUqSplgir5dB4D66rqiGd8R3fimBE= diff --git a/internal/vp9/dsp/conv_kernels_amd64.go b/internal/vp9/dsp/conv_kernels_amd64.go new file mode 100644 index 0000000..1dd9631 --- /dev/null +++ b/internal/vp9/dsp/conv_kernels_amd64.go @@ -0,0 +1,48 @@ +//go:build amd64 + +package dsp + +import ( + "github.com/go-videodsp/kernels/cpu" + "github.com/go-videodsp/kernels/mc" +) + +// The step-16 8-tap convolutions of go-videodsp/kernels (AVX2). Each reports +// whether it ran; false sends the caller to the paths it had before. src[off] +// is the first-tap sample of the first output. The kernels take the widths 4, +// 8, 16 and the multiples of 32, and the filters they can prove exact (not the +// full-sample row, whose 128 does not fit a signed byte). + +func convKernelWidth(w int) bool { return w == 4 || w == 8 || w == 16 || (w > 0 && w%32 == 0) } + +func convKernelH(src []byte, ss int, dst []byte, ds int, taps *[8]int16, off, w, h int) bool { + if !cpu.HasAVX2 || !convKernelWidth(w) || taps[3] == 128 { + return false + } + mc.VP9Convolve8H(dst, ds, src, off, ss, taps, w, h) + return true +} + +func convKernelV(src []byte, ss int, dst []byte, ds int, taps *[8]int16, off, w, h int) bool { + if !cpu.HasAVX2 || !convKernelWidth(w) || taps[3] == 128 { + return false + } + mc.VP9Convolve8V(dst, ds, src, off, ss, taps, w, h) + return true +} + +func convKernelAvgH(src []byte, ss int, dst []byte, ds int, taps *[8]int16, off, w, h int) bool { + if !cpu.HasAVX2 || !convKernelWidth(w) || taps[3] == 128 { + return false + } + mc.VP9Convolve8AvgH(dst, ds, src, off, ss, taps, w, h) + return true +} + +func convKernelAvgV(src []byte, ss int, dst []byte, ds int, taps *[8]int16, off, w, h int) bool { + if !cpu.HasAVX2 || !convKernelWidth(w) || taps[3] == 128 { + return false + } + mc.VP9Convolve8AvgV(dst, ds, src, off, ss, taps, w, h) + return true +} diff --git a/internal/vp9/dsp/conv_kernels_other.go b/internal/vp9/dsp/conv_kernels_other.go new file mode 100644 index 0000000..cd24940 --- /dev/null +++ b/internal/vp9/dsp/conv_kernels_other.go @@ -0,0 +1,18 @@ +//go:build !amd64 + +package dsp + +// Without the AVX2 kernels the callers keep the paths they had before. + +func convKernelH(src []byte, ss int, dst []byte, ds int, taps *[8]int16, off, w, h int) bool { + return false +} +func convKernelV(src []byte, ss int, dst []byte, ds int, taps *[8]int16, off, w, h int) bool { + return false +} +func convKernelAvgH(src []byte, ss int, dst []byte, ds int, taps *[8]int16, off, w, h int) bool { + return false +} +func convKernelAvgV(src []byte, ss int, dst []byte, ds int, taps *[8]int16, off, w, h int) bool { + return false +} diff --git a/internal/vp9/dsp/convolve.go b/internal/vp9/dsp/convolve.go index 354a0c3..2bde41c 100644 --- a/internal/vp9/dsp/convolve.go +++ b/internal/vp9/dsp/convolve.go @@ -150,6 +150,9 @@ func convolveHoriz(src []byte, srcStride int, dst []byte, dstStride int, ) { if xStepQ4 == 16 && w >= 4 && h > 0 { base := srcOffset - 3 + (x0Q4 >> 4) + if convKernelH(src, srcStride, dst, dstStride, &xFilters[x0Q4&15], base, w, h) { + return + } pixel.Filter(src[base:], srcStride, dst, dstStride, w, h, 1, xFilters[x0Q4&15][:]) return } @@ -180,6 +183,9 @@ func convolveVert(src []byte, srcStride int, dst []byte, dstStride int, ) { if yStepQ4 == 16 && w >= 4 && h > 0 { base := srcOffset + (y0Q4>>4)*srcStride - 3*srcStride + if convKernelV(src, srcStride, dst, dstStride, &yFilters[y0Q4&15], base, w, h) { + return + } pixel.Filter(src[base:], srcStride, dst, dstStride, w, h, srcStride, yFilters[y0Q4&15][:]) return } @@ -208,6 +214,12 @@ func convolveAvgHoriz(src []byte, srcStride int, dst []byte, dstStride int, xFilters *[tables.SubpelShifts][tables.SubpelTaps]int16, x0Q4, xStepQ4, w, h, srcOffset int, ) { + if xStepQ4 == 16 && h > 0 { + base := srcOffset - 3 + (x0Q4 >> 4) + if convKernelAvgH(src, srcStride, dst, dstStride, &xFilters[x0Q4&15], base, w, h) { + return + } + } srcStart := srcOffset - (tables.SubpelTaps/2 - 1) for y := range h { xQ4 := x0Q4 @@ -231,6 +243,12 @@ func convolveAvgVert(src []byte, srcStride int, dst []byte, dstStride int, yFilters *[tables.SubpelShifts][tables.SubpelTaps]int16, y0Q4, yStepQ4, w, h, srcOffset int, ) { + if yStepQ4 == 16 && h > 0 { + base := srcOffset + (y0Q4>>4)*srcStride - 3*srcStride + if convKernelAvgV(src, srcStride, dst, dstStride, &yFilters[y0Q4&15], base, w, h) { + return + } + } srcStart := srcOffset - srcStride*(tables.SubpelTaps/2-1) for x := range w { yQ4 := y0Q4 diff --git a/tools/portgovpx/main.go b/tools/portgovpx/main.go index f8f50ae..749dddc 100644 --- a/tools/portgovpx/main.go +++ b/tools/portgovpx/main.go @@ -423,6 +423,39 @@ var Patches = map[string][]Patch{ }, // The VP9 loop filters of the amd64 port come from go-videodsp/kernels. "internal/vp9/dsp/loopfilter_thr.go": lfKernelPatches(), + // The step-16 8-tap convolutions of the amd64 port come from the same module. + "internal/vp9/dsp/convolve.go": convKernelPatches(), +} + +// convKernelPatches makes the four one-direction convolutions try the AVX2 +// kernels of go-videodsp/kernels first (internal/vp9/dsp/conv_kernels_*.go), +// keeping the code they had as the fallback. The 2-D forms go through them. +func convKernelPatches() []Patch { + const ( + posH = "base := srcOffset - 3 + (x0Q4 >> 4)" + posV = "base := srcOffset + (y0Q4>>4)*srcStride - 3*srcStride" + ) + // call is the kernel try, at the depth of the body of the step-16 branch. + call := func(name, filters, q4 string) string { + return "\t\tif " + name + "(src, srcStride, dst, dstStride, &" + filters + "[" + q4 + "&15], base, w, h) {\n\t\t\treturn\n\t\t}\n" + } + // before puts the try in front of the pixel.Filter line the branch ends with. + before := func(name, filters, q4, stride string) Patch { + old := "\t\tpixel.Filter(src[base:], srcStride, dst, dstStride, w, h, " + stride + ", " + filters + "[" + q4 + "&15][:])\n" + return Patch{old, call(name, filters, q4) + old} + } + // avg gives the averaging form, which had no step-16 branch, one. + avg := func(fn, name, filters, q4, pos string) Patch { + old := "func " + fn + "(src []byte, srcStride int, dst []byte, dstStride int,\n\t" + filters + + " *[tables.SubpelShifts][tables.SubpelTaps]int16,\n\t" + q4[:1] + "0Q4, " + q4[:1] + "StepQ4, w, h, srcOffset int,\n) {\n" + return Patch{old, old + "\tif " + q4[:1] + "StepQ4 == 16 && h > 0 {\n\t\t" + pos + "\n" + call(name, filters, q4) + "\t}\n"} + } + return []Patch{ + before("convKernelH", "xFilters", "x0Q4", "1"), + before("convKernelV", "yFilters", "y0Q4", "srcStride"), + avg("convolveAvgHoriz", "convKernelAvgH", "xFilters", "x0Q4", posH), + avg("convolveAvgVert", "convKernelAvgV", "yFilters", "y0Q4", posV), + } } // lfKernelPatches makes the six sixteen-line loop filters try the AVX2 kernels