Commit 4be4e2dcbb for ffmpeg
commit 4be4e2dcbb5bd1e43d955822b3634557eae34f58
Author: Zuxy Meng <zuxy.meng@gmail.com>
Date: Tue Nov 10 16:07:21 2026 -0800
avcodec/x86/vp9: split the loop filter template into pipeline stages
Split the shared LOOPFILTER macro into pipeline stage macros so each
filter instantiation reads as the stages it runs.
No functional or performance change: all 34 symbols assemble
byte-identically on x86-64 and i686; checkasm --test=vp9dsp and
fate-vp9 pass on both ABIs.
Signed-off-by: Zuxy Meng <zuxy.meng@gmail.com>
diff --git a/libavcodec/x86/vp9lpf.asm b/libavcodec/x86/vp9lpf.asm
index 22f81ccffe..cc4076a397 100644
--- a/libavcodec/x86/vp9lpf.asm
+++ b/libavcodec/x86/vp9lpf.asm
@@ -79,61 +79,63 @@ SECTION .text
por %1, %4
%endmacro
+; Resolve one p/q operand to something usable as an instruction source
+; and leave it in LPF_SRC. p3..q3 are m8..m15 on x86-64 but memory on
+; x86-32, so every mask helper below faces the same three cases, which
+; this answers once:
+;
+; already a register use it as is
+; memory, scratch given load it into the scratch
+; memory, no scratch use the memory operand in place
+;
+; The last case emits a legacy-SSE memory operand, so it is only legal
+; where the address is mmsize-aligned. LPF_SRC is clobbered by the next
+; LPF_RESOLVE, so snapshot it with %xdefine if it must outlive one.
+%macro LPF_RESOLVE 2 ; operand, scratch ("" for none)
+%ifnum sizeof%1
+ %xdefine LPF_SRC %1
+%elifidn %2, ""
+ %xdefine LPF_SRC %1
+%else
+ mova %2, %1
+ %xdefine LPF_SRC %2
+%endif
+%endmacro
+
+; %1 = max(%1 or %5, %3), %2 = min(%2 or %5, %3)
%macro MINMAX 3-5 ; maxdst, mindst, src, scratch reg, [initial value]
%if %0 == 5
- %ifnum sizeof%5 ; register
- %define %%srcmax %5
- %define %%srcmin %5
- %else
- mova %2, %5
- %define %%srcmax %2
- %define %%srcmin %2
- %endif
+ LPF_RESOLVE %5, %2 ; seed both accumulators from %5
+ %xdefine %%srcmax LPF_SRC
+ %xdefine %%srcmin LPF_SRC
%else
- %define %%srcmax %1
+ %define %%srcmax %1 ; accumulate into the destinations
%define %%srcmin %2
%endif
-
-%ifnum sizeof%3 ; src is register
- %define %%src %3
-%else ; src is memory
- %ifnidn %4, ""
- %define %%src %4
- mova %4, %3
- %else
- %define %%src %3
- %endif
-%endif
-
- pmaxub %1, %%srcmax, %%src
- pminub %2, %%srcmin, %%src
+ LPF_RESOLVE %3, %4
+ pmaxub %1, %%srcmax, LPF_SRC
+ pminub %2, %%srcmin, LPF_SRC
%endmacro
-%macro ABSDIFF_MAX 5-6 ; max [dst], min, common subtrahend, combine op, scratch reg, [dst]
-%ifidn %5, ""
- ; no spare reg; must have separate dst reg
- %define %%sub
- psubusb %1, %3
- %4 %6, %1
- mova %1, %3
- psubusb %1, %2
- %4 %6, %1
-%else
-%ifnum sizeof%3
- psubusb %1, %3
- %define %%sub %3
-%else
- mova %5, %3
- psubusb %1, %5
- %define %%sub %5
-%endif
+; Combine max(%1,%3) - %3 and %3 - min(%2,%3) into %6 (or %1): the
+; larger of the two saturating differences from the common value %3.
+;
+; Two disjoint algorithms, picked by whether a scratch register exists.
+; They share no code, so they are separate macros behind one dispatcher
+; rather than one body wrapped in a conditional.
+
+; With a scratch: %1 and the scratch each hold one difference, so %3 is
+; read once and both differences survive to the combine.
+%macro ABSDIFF_MAX_TMP 5-6 ; max [dst], min, subtrahend, combine op, scratch, [dst]
+ LPF_RESOLVE %3, %5
+ psubusb %1, LPF_SRC ; max(..) - %3
%if avx_enabled
%define %%min %2
- psubusb %2, %%sub, %2
+ psubusb %2, LPF_SRC, %2 ; %3 - min(..)
%else
%define %%min %5
- %ifnidn %%sub, %5
- mova %5, %%sub
+ %ifnidn LPF_SRC, %5
+ mova %5, LPF_SRC
%endif
psubusb %5, %2
%endif
@@ -143,6 +145,25 @@ SECTION .text
%else
%4 %1, %%min
%endif
+%endmacro
+
+; Without a scratch: %1 is the only free register, so it is reused for
+; the second difference and %3 is read twice. Needs a separate dst.
+%macro ABSDIFF_MAX_NOTMP 5 ; max, min, subtrahend, combine op, dst
+ psubusb %1, %3 ; max(..) - %3
+ %4 %5, %1
+ mova %1, %3
+ psubusb %1, %2 ; %3 - min(..)
+ %4 %5, %1
+%endmacro
+
+%macro ABSDIFF_MAX 5-6 ; max [dst], min, subtrahend, combine op, scratch, [dst]
+%ifidn %5, ""
+ ABSDIFF_MAX_NOTMP %1, %2, %3, %4, %6
+%elif %0 == 6
+ ABSDIFF_MAX_TMP %1, %2, %3, %4, %5, %6
+%else
+ ABSDIFF_MAX_TMP %1, %2, %3, %4, %5
%endif
%endmacro
@@ -416,51 +437,16 @@ SECTION .text
%endif
%endmacro
-%macro LOOPFILTER 5 ; %1=v/h %2=size1 %3+%4=stack, %5=mmx/32bit stack only
-%assign %%ext 0
-%if ARCH_X86_32 || mmsize == 8
-%assign %%ext %5
-%endif
-
-%if UNIX64
-cglobal vp9_loop_filter_%1_%2_ %+ mmsize, 5, 9, 16, %3 + %4 + %%ext, dst, stride, E, I, H, mstride, dst2, stride3, mstride3
-%else
-%if WIN64
-cglobal vp9_loop_filter_%1_%2_ %+ mmsize, 4, 8, 16, %3 + %4 + %%ext, dst, stride, E, I, mstride, dst2, stride3, mstride3
-%else
-cglobal vp9_loop_filter_%1_%2_ %+ mmsize, 2, 6, 16, %3 + %4 + %%ext, dst, stride, mstride, dst2, stride3, mstride3
-%define Ed dword r2m
-%define Id dword r3m
-%endif
-%define Hd dword r4m
-%endif
-
- mov mstrideq, strideq
- neg mstrideq
-
- lea stride3q, [strideq*3]
- lea mstride3q, [mstrideq*3]
-
-%ifidn %1, h
-%if %2 != 16
-%if mmsize == 16
-%define movx movh
-%else
-%define movx mova
-%endif
- lea dstq, [dstq + 4*strideq - 4]
-%else
-%define movx movu
- lea dstq, [dstq + 4*strideq - 8] ; go from top center (h pos) to center left (v pos)
-%endif
-%else
- lea dstq, [dstq + 4*mstrideq]
-%endif
- ; FIXME we shouldn't need two dts registers if mmsize == 8
- lea dst2q, [dstq + 8*strideq]
-
- DEFINE_REAL_P7_TO_Q7
-
+; Load the 8 or 16 source columns and transpose them into the stack
+; slots the filter works on. Horizontal filters only: the vertical ones
+; already have their rows laid out in memory and skip this entirely.
+;
+; On entry dst/stride/stride3 point at the block; P7..Q7 are frame
+; addresses (DEFINE_REAL_P7_TO_Q7).
+; On exit transpose slots cover P3..Q3 for all horizontal filters
+; and P7..Q7 for width 16 (DEFINE_TRANSPOSED_P7_TO_Q7).
+; On x86-64, m4..m11 hold p3..q3. LPF_MASKS remaps them to m8..m15.
+%macro LPF_TRANSPOSE_IN 4 ; %1=v/h %2=size1 %3+%4=stack
%ifidn %1, h
movx m0, [P7]
movx m1, [P6]
@@ -609,7 +595,19 @@ cglobal vp9_loop_filter_%1_%2_ %+ mmsize, 2, 6, 16, %3 + %4 + %%ext, dst, stride
%endif ; %2
%endif ; x86-32/64
%endif ; %1 == h
+%endmacro
+; Compute the three masks that select which filter each pixel gets:
+; fm the block edge is smooth enough to filter at all
+; hev high edge variance; picks filter2 over filter4
+; flat8in the inner 8 pixels are flat (not for the 4-wide widths)
+; flat8out the outer 8 are flat too; 16-wide only, gates filter14
+;
+; On exit m0 = hev, m3 = fm and m4 = pb_80. m2 = flat8in except for
+; 4 and 44. Width 16 also sets m1 = flat8out. The p/q operands come
+; from m8..m15 on x86-64 and from memory on x86-32; LPF_RESOLVE in
+; the helpers hides the difference.
+%macro LPF_MASKS 4 ; %1=v/h %2=size1 %3+%4=stack
; calc fm mask
%if %2 == 16 || mmsize == 8
%if cpuflag(ssse3)
@@ -760,6 +758,19 @@ cglobal vp9_loop_filter_%1_%2_ %+ mmsize, 2, 6, 16, %3 + %4 + %%ext, dst, stride
SWAP 1, 6
%endif
+%endmacro
+
+; filter2_4() and filter6(): the narrow filters.
+;
+; if (fm) {
+; if (out && in) filter_14() <- LPF_FILTER14, below
+; else if (in) filter_6()
+; else filter_2_4()
+; }
+;
+; filter2_4 always runs and masks its result; filter6 is skipped for the
+; 4-wide widths, which have no flat8in mask.
+%macro LPF_FILTER2_4_6 4 ; %1=v/h %2=size1 %3+%4=stack
; if (fm) {
; if (out && in) filter_14()
; else if (in) filter_6()
@@ -852,6 +863,16 @@ cglobal vp9_loop_filter_%1_%2_ %+ mmsize, 2, 6, 16, %3 + %4 + %%ext, dst, stride
UNSCRATCH 1, 8, rsp+%3+%4+16
%endif
+%endmacro
+
+; filter14(): the 15-tap filter, applied where flat8out holds. Only the
+; 16-wide filters reach it; for every other width the %if below is false
+; and this expands to nothing.
+;
+; Vertical filters update frame rows and horizontal filters update
+; transposed stack slots. FILTER_UPDATE keeps the running sums in
+; registers and uses the cached unpacked pixels at [rsp+%4].
+%macro LPF_FILTER14 4 ; %1=v/h %2=size1 %3+%4=stack
; (m0: 0, [m1: flat8out], m2: fm & flat8in, m8..15: q2 q3 p1 p0 q0 q1 p3 p2)
; filter14()
;
@@ -922,6 +943,16 @@ cglobal vp9_loop_filter_%1_%2_ %+ mmsize, 2, 6, 16, %3 + %4 + %%ext, dst, stride
FILTER_UPDATE m4, m5, m6, m7, [Q6], %4, 2, 13, 14, 15, 4, m1, rq6s ; [q6] -p1 -q5 +q6 +q7
%endif
+%endmacro
+
+; Write the filtered columns back. Vertical filters have already stored
+; through LPF_STORE and do nothing here; horizontal ones transpose the
+; stack slots back into the frame.
+;
+; Width 16 transposes a full 16x16 block. Widths 4 and 44 write four
+; bytes per row. Widths 8, 48, 84 and 88 write eight bytes per row.
+; Single-block filters write eight rows and mix2 filters write sixteen.
+%macro LPF_TRANSPOSE_OUT 4 ; %1=v/h %2=size1 %3+%4=stack
%ifidn %1, h
%if %2 == 16
mova m0, [P7]
@@ -1138,6 +1169,68 @@ cglobal vp9_loop_filter_%1_%2_ %+ mmsize, 2, 6, 16, %3 + %4 + %%ext, dst, stride
%endif
%endif
%endif
+%endmacro
+
+%macro LOOPFILTER 5 ; %1=v/h %2=size1 %3+%4=stack, %5=mmx/32bit stack only
+; Number of pixels along the edge this filter covers. This is the
+; second field of the symbol name. The single-block widths 4 and 8 cover
+; 8 pixels; the 16-wide filter and the mix2 widths, which are two blocks
+; side by side, cover 16.
+%if %2 == 4 || %2 == 8
+%define LPF_NPX 8
+%else
+%define LPF_NPX 16
+%endif
+%assign %%ext 0
+%if ARCH_X86_32 || mmsize == 8
+%assign %%ext %5
+%endif
+
+%if UNIX64
+cglobal vp9_loop_filter_%1_%2_ %+ LPF_NPX, 5, 9, 16, %3 + %4 + %%ext, dst, stride, E, I, H, mstride, dst2, stride3, mstride3
+%else
+%if WIN64
+cglobal vp9_loop_filter_%1_%2_ %+ LPF_NPX, 4, 8, 16, %3 + %4 + %%ext, dst, stride, E, I, mstride, dst2, stride3, mstride3
+%else
+cglobal vp9_loop_filter_%1_%2_ %+ LPF_NPX, 2, 6, 16, %3 + %4 + %%ext, dst, stride, mstride, dst2, stride3, mstride3
+%define Ed dword r2m
+%define Id dword r3m
+%endif
+%define Hd dword r4m
+%endif
+
+ mov mstrideq, strideq
+ neg mstrideq
+
+ lea stride3q, [strideq*3]
+ lea mstride3q, [mstrideq*3]
+
+%ifidn %1, h
+%if %2 != 16
+%if mmsize == 16
+%define movx movh
+%else
+%define movx mova
+%endif
+ lea dstq, [dstq + 4*strideq - 4]
+%else
+%define movx movu
+ lea dstq, [dstq + 4*strideq - 8] ; go from top center (h pos) to center left (v pos)
+%endif
+%else
+ lea dstq, [dstq + 4*mstrideq]
+%endif
+ ; FIXME we shouldn't need two dts registers if mmsize == 8
+ lea dst2q, [dstq + 8*strideq]
+
+ DEFINE_REAL_P7_TO_Q7
+
+ LPF_TRANSPOSE_IN %1, %2, %3, %4
+
+ LPF_MASKS %1, %2, %3, %4
+ LPF_FILTER2_4_6 %1, %2, %3, %4
+ LPF_FILTER14 %1, %2, %3, %4
+ LPF_TRANSPOSE_OUT %1, %2, %3, %4
RET
%endmacro