diff --git a/docs/perf/speed-log.md b/docs/perf/speed-log.md index df5dd8e..0a99e4c 100644 --- a/docs/perf/speed-log.md +++ b/docs/perf/speed-log.md @@ -12,6 +12,8 @@ crowd_run 1080p 300-frame clips, median µs per frame. | 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 | | VP8 loop filters, AVX2 (`go-videodsp/kernels` v0.24.0 `lf`: inner, macroblock and simple edges, 16 luma lines or 2 x 8 chroma lines per call; they replace the fused macroblock paths of `internal/pixel`) | not affected | not affected | 4327 (1.142x, v0.4.0 4942 in the same run) | | VP8 six-tap and bilinear prediction, AVX2 (`go-videodsp/kernels` v0.25.0 `mc`: widths 4, 8, 16) | not affected | not affected | 3437 (1.438x, v0.4.0 4942) | +| VP9 inter prediction: `vp9InterPredictPlaneParams` filled field by field instead of from a composite literal (perf annotate: the literal was built in a temporary and copied with 16-byte loads of 8-byte stores, a store-forwarding stall worth 6 % of the clip) | 5156 (1.034x, v0.5.0 5331; a first run 1.037x) | not run | not affected | +| VP9 modes: `FrameRefSignBias` built as one 32-bit word (was byte stores read back as a word), and `readVP9InterBlockModeInfo` writes `mi.Mv` directly instead of through a local copied at the end (same store-forwarding stall) | 4956 (1.017x, previous row 5038 in the same run) | not run | not affected | libvpx single core, same clips (M0 protocol): 2623, 2610, 2427. diff --git a/internal/vp9/codec/vp9_decoder_modes.go b/internal/vp9/codec/vp9_decoder_modes.go index 6860eab..1378a6a 100644 --- a/internal/vp9/codec/vp9_decoder_modes.go +++ b/internal/vp9/codec/vp9_decoder_modes.go @@ -656,7 +656,7 @@ func (d *VP9Decoder) readVP9InterBlockModeInfo(r *bitstream.Reader, if mi.RefFrame[1] > vp9dec.IntraFrame { isCompound = 1 } - var mv, refMv, nearNearest [2]vp9dec.MV + var refMv, nearNearest [2]vp9dec.MV if mi.SbType < common.Block8x8 { num4x4W := int(common.Num4x4BlocksWideLookup[mi.SbType]) num4x4H := int(common.Num4x4BlocksHighLookup[mi.SbType]) @@ -719,17 +719,20 @@ func (d *VP9Decoder) readVP9InterBlockModeInfo(r *bitstream.Reader, nearNearest[ref] = best } } - if vp9dec.AssignMv(mi.Mode, &mv, &refMv, &nearNearest, + // AssignMv writes the 4-byte vectors straight into mi.Mv: filling a local pair and copying it + // with one 8-byte load right after cannot be store-forwarded (perf annotate: a fifth of + // this function's time sat on that copy). + mi.Mv = [2]vp9dec.MV{} + if vp9dec.AssignMv(mi.Mode, &mi.Mv, &refMv, &nearNearest, isCompound, hdr.AllowHighPrecisionMv, r, &d.fc) == 0 { return false } if !hdr.FrameParallelDecoding && mi.Mode == common.NewMv { halves := 1 + isCompound for ref := range halves { - d.countVP9NewMv(mv[ref], refMv[ref]) + d.countVP9NewMv(mi.Mv[ref], refMv[ref]) } } - mi.Mv = mv return true } @@ -990,32 +993,34 @@ func (d *VP9Decoder) reconstructVP9InterPredictBlock( } } srcFull, srcOrigin, srcBorderX, srcBorderY := vp9ReferencePlaneBorder(ref, plane) - params := vp9InterPredictPlaneParams{ - hdr: hdr, - pd: pd, - mi: mi, - dst: dst, - src: src, - srcFull: srcFull, - bsize: bsize, - miRow: miRow, - miCol: miCol, - srcBaseX: x0, - srcBaseY: y0, - dstBaseX: x0, - dstBaseY: y0, - bw: bw, - bh: bh, - dstStride: dstStride, - dstRows: len(dst) / dstStride, - srcStride: srcStride, - srcRows: len(src) / srcStride, - srcWidth: srcWidth, - srcHeight: srcHeight, - srcOrigin: srcOrigin, - srcBorderX: srcBorderX, - srcBorderY: srcBorderY, - } + // Field stores into a zeroed variable, not a composite literal: the literal + // is built in a temporary and copied with 16-byte loads of 8-byte stores + // (store-forwarding stalls, about 6 % of the 1-tile clip in perf annotate). + var params vp9InterPredictPlaneParams + params.hdr = hdr + params.pd = pd + params.mi = mi + params.dst = dst + params.src = src + params.srcFull = srcFull + params.bsize = bsize + params.miRow = miRow + params.miCol = miCol + params.srcBaseX = x0 + params.srcBaseY = y0 + params.dstBaseX = x0 + params.dstBaseY = y0 + params.bw = bw + params.bh = bh + params.dstStride = dstStride + params.dstRows = len(dst) / dstStride + params.srcStride = srcStride + params.srcRows = len(src) / srcStride + params.srcWidth = srcWidth + params.srcHeight = srcHeight + params.srcOrigin = srcOrigin + params.srcBorderX = srcBorderX + params.srcBorderY = srcBorderY if !d.reconstructVP9InterPredictPlaneAt(¶ms, 0, 0, bw, bh, mv) { return false } @@ -1179,34 +1184,33 @@ func (d *VP9Decoder) reconstructVP9InterPredictBlock( } continue } - params := vp9InterPredictPlaneParams{ - hdr: hdr, - pd: pd, - mi: mi, - sf: sf, - dst: dst, - src: src, - srcFull: srcFull, - bsize: bsize, - miRow: miRow, - miCol: miCol, - srcBaseX: x0, - srcBaseY: y0, - dstBaseX: x0, - dstBaseY: y0, - bw: bw, - bh: bh, - dstStride: dstStride, - dstRows: dstRows, - srcStride: srcStride, - srcRows: srcRows, - srcWidth: srcWidth, - srcHeight: srcHeight, - srcOrigin: srcOrigin, - srcBorderX: srcBorderX, - srcBorderY: srcBorderY, - avg: vp9dec.BoolInt(avg), - } + var params vp9InterPredictPlaneParams + params.hdr = hdr + params.pd = pd + params.mi = mi + params.sf = sf + params.dst = dst + params.src = src + params.srcFull = srcFull + params.bsize = bsize + params.miRow = miRow + params.miCol = miCol + params.srcBaseX = x0 + params.srcBaseY = y0 + params.dstBaseX = x0 + params.dstBaseY = y0 + params.bw = bw + params.bh = bh + params.dstStride = dstStride + params.dstRows = dstRows + params.srcStride = srcStride + params.srcRows = srcRows + params.srcWidth = srcWidth + params.srcHeight = srcHeight + params.srcOrigin = srcOrigin + params.srcBorderX = srcBorderX + params.srcBorderY = srcBorderY + params.avg = vp9dec.BoolInt(avg) if !d.reconstructVP9InterPredictPlaneAt(¶ms, 0, 0, bw, bh, mi.Mv[refIdx]) { return false } @@ -1340,32 +1344,31 @@ func (d *VP9Decoder) reconstructVP9InterPredictLumaTo( } continue } - params := vp9InterPredictPlaneParams{ - hdr: hdr, - pd: pd, - mi: mi, - sf: sf, - dst: dst, - src: src, - srcFull: srcFull, - bsize: bsize, - miRow: miRow, - miCol: miCol, - srcBaseX: x0, - srcBaseY: y0, - bw: bw, - bh: bh, - dstStride: dstStride, - dstRows: dstRows, - srcStride: srcStride, - srcRows: srcRows, - srcWidth: srcWidth, - srcHeight: srcHeight, - srcOrigin: srcOrigin, - srcBorderX: srcBorderX, - srcBorderY: srcBorderY, - avg: vp9dec.BoolInt(avg), - } + var params vp9InterPredictPlaneParams + params.hdr = hdr + params.pd = pd + params.mi = mi + params.sf = sf + params.dst = dst + params.src = src + params.srcFull = srcFull + params.bsize = bsize + params.miRow = miRow + params.miCol = miCol + params.srcBaseX = x0 + params.srcBaseY = y0 + params.bw = bw + params.bh = bh + params.dstStride = dstStride + params.dstRows = dstRows + params.srcStride = srcStride + params.srcRows = srcRows + params.srcWidth = srcWidth + params.srcHeight = srcHeight + params.srcOrigin = srcOrigin + params.srcBorderX = srcBorderX + params.srcBorderY = srcBorderY + params.avg = vp9dec.BoolInt(avg) if !d.reconstructVP9InterPredictPlaneAt(¶ms, 0, 0, bw, bh, mi.Mv[refIdx]) { return false } diff --git a/internal/vp9/decoder/header.go b/internal/vp9/decoder/header.go index 38e7a40..cb104e1 100644 --- a/internal/vp9/decoder/header.go +++ b/internal/vp9/decoder/header.go @@ -11,7 +11,9 @@ package decoder import ( + "encoding/binary" "errors" + "github.com/go-avkit/vpx/internal/vp9/common" ) @@ -358,10 +360,18 @@ func SupportedOutputFormat(hdr *UncompressedHeader) bool { // FrameRefSignBias converts the uncompressed-header reference sign-bias fields // into the full VP9 reference-frame bias table. func FrameRefSignBias(hdr *UncompressedHeader) [MaxRefFrames]uint8 { + // Built as one 32-bit store: byte stores followed by the 4-byte reload of the returned array + // cannot be forwarded and stalled every inter block (perf annotate, 2 % of the 1-tile clip). + const _ = uint(MaxRefFrames - 4) // the word below holds exactly four bytes + const _ = uint(4 - MaxRefFrames) // (both directions: MaxRefFrames == 4) + const _ = uint(LastFrame - 1) // slot 0 is intra, slots 1 to 3 are the references + const _ = uint(1 - LastFrame) // (LastFrame == 1) + const _ = uint(common.RefsPerFrame - 3) + const _ = uint(3 - common.RefsPerFrame) + sb := &hdr.InterRef.SignBias + word := uint32(sb[0])<<8 | uint32(sb[1])<<16 | uint32(sb[2])<<24 var signBias [MaxRefFrames]uint8 - for i := range common.RefsPerFrame { - signBias[LastFrame+i] = hdr.InterRef.SignBias[i] - } + binary.LittleEndian.PutUint32(signBias[:], word) return signBias }