PR #26357 opened by zuxy
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26357
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26357.patch

Converts the 4- and 8-wide VP9 loop filters from MMXEXT to SSE2 in 
libavcodec/x86/vp9lpf.asm, dispatched from libavcodec/x86/vp9dsp_init.c. A 
first commit splits the shared LOOPFILTER template into pipeline stage macros 
with no functional change, so each filter reads as the stages it runs.

Commits: template split, then loop_filter_v_4, loop_filter_v_8, 
loop_filter_h_4, loop_filter_h_8. Each filter is assigned under EXTERNAL_SSE2, 
and SSE2 implies MMXEXT so CPU coverage is unchanged.

x86-64 is faster on every filter; i686 stays roughly even and 32-bit decode 
wall-clock is flat.


>From 4ef16414f6215fbf5406b6a5aaa831b0ea7e4f48 Mon Sep 17 00:00:00 2001
From: Zuxy Meng <[email protected]>
Date: Tue, 10 Nov 2026 16:07:21 -0800
Subject: [PATCH 1/5] 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 <[email protected]>
---
 libavcodec/x86/vp9lpf.asm | 273 +++++++++++++++++++++++++-------------
 1 file changed, 183 insertions(+), 90 deletions(-)

diff --git a/libavcodec/x86/vp9lpf.asm b/libavcodec/x86/vp9lpf.asm
index 22f81ccffee..a85cb026b12 100644
--- a/libavcodec/x86/vp9lpf.asm
+++ b/libavcodec/x86/vp9lpf.asm
@@ -79,61 +79,63 @@ SECTION .text
     por                 %1, %4
 %endmacro
 
-%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
+; 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
-    %define %%srcmax %1
-    %define %%srcmin %2
+    mova            %2, %1
+    %xdefine LPF_SRC %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
 %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
+; %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
+    LPF_RESOLVE         %5, %2          ; seed both accumulators from %5
+    %xdefine %%srcmax LPF_SRC
+    %xdefine %%srcmin LPF_SRC
 %else
-%ifnum sizeof%3
-    psubusb             %1, %3
-    %define %%sub %3
-%else
-    mova                %5, %3
-    psubusb             %1, %5
-    %define %%sub %5
+    %define %%srcmax %1                 ; accumulate into the destinations
+    %define %%srcmin %2
 %endif
+    LPF_RESOLVE         %3, %4
+    pmaxub              %1, %%srcmax, LPF_SRC
+    pminub              %2, %%srcmin, LPF_SRC
+%endmacro
+
+; 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,15 @@ 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   P7..Q7 are stack addresses (DEFINE_TRANSPOSED_P7_TO_Q7) and,
+;           on x86-64, m8..m15 hold p3..q3 ready for the mask stage.
+%macro LPF_TRANSPOSE_IN 4 ; %1=v/h %2=size1 %3+%4=stack
 %ifidn %1, h
     movx                    m0, [P7]
     movx                    m1, [P6]
@@ -609,7 +594,18 @@ 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, m2 = flat8in, m3 = fm, m4 = pb_80, and for 16-wide
+; 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 +756,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 +861,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.
+;
+; Runs on the transposed stack slots via FILTER_UPDATE, which keeps a
+; running sum in the cache region at [rsp+%4] and writes each result
+; through the mask in m1.
+%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 +941,17 @@ 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.
+;
+; Three shapes: the 16-wide path transposes a full 16x16 block, the
+; mix2 widths (44/48/84/88) write 4-byte columns with psrldq, and the
+; 4/8-wide path transposes 8 rows and stores only the p half, since the
+; q half of the window belongs to the next block.
+%macro LPF_TRANSPOSE_OUT 4 ; %1=v/h %2=size1 %3+%4=stack
 %ifidn %1, h
 %if %2 == 16
     mova                    m0, [P7]
@@ -1138,6 +1168,69 @@ 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. It was previously spelled `mmsize`, which held
+; only while one pixel was one byte lane of a full register.
+%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
-- 
2.54.1


>From bd34230d40def94dd48d10c91d346f863c1b40b2 Mon Sep 17 00:00:00 2001
From: Zuxy Meng <[email protected]>
Date: Mon, 9 Nov 2026 19:18:37 -0800
Subject: [PATCH 2/5] avcodec/x86/vp9: convert loop_filter_v_4 MMXEXT to SSE2

Convert the 4-wide vertical loop filter to SSE2 and assign it under
EXTERNAL_SSE2; SSE2 implies MMXEXT so CPU coverage is unchanged.

Benchmarks (checkasm --bench --duration=20000, Zen 4, cycles):
  vp9_loop_filter_v_4_8_8bpp: x86-64 29.4 -> 29.0; i686 29.4 -> 33.2.

checkasm --test=vp9dsp and fate-vp9 pass on x86-64 and i686.

Signed-off-by: Zuxy Meng <[email protected]>
---
 libavcodec/x86/vp9dsp_init.c |   3 +-
 libavcodec/x86/vp9lpf.asm    | 202 ++++++++++++++++++++++++++++++-----
 2 files changed, 175 insertions(+), 30 deletions(-)

diff --git a/libavcodec/x86/vp9dsp_init.c b/libavcodec/x86/vp9dsp_init.c
index 3e8e1fc8ca4..7d3a7e61308 100644
--- a/libavcodec/x86/vp9dsp_init.c
+++ b/libavcodec/x86/vp9dsp_init.c
@@ -131,6 +131,7 @@ void ff_vp9_loop_filter_h_##size1##_##size2##_##opt(uint8_t 
*dst, ptrdiff_t stri
 
 lpf_funcs(4, 8, mmxext);
 lpf_funcs(8, 8, mmxext);
+lpf_funcs(4, 8, sse2);
 lpf_funcs(16, 16, sse2);
 lpf_funcs(16, 16, ssse3);
 lpf_funcs(16, 16, avx);
@@ -278,7 +279,6 @@ av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int 
bpp, int bitexact)
 
     if (EXTERNAL_MMXEXT(cpu_flags)) {
         dsp->loop_filter_8[0][0] = ff_vp9_loop_filter_h_4_8_mmxext;
-        dsp->loop_filter_8[0][1] = ff_vp9_loop_filter_v_4_8_mmxext;
         dsp->loop_filter_8[1][0] = ff_vp9_loop_filter_h_8_8_mmxext;
         dsp->loop_filter_8[1][1] = ff_vp9_loop_filter_v_8_8_mmxext;
     }
@@ -303,6 +303,7 @@ av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int 
bpp, int bitexact)
         init_fpel_func(1, 1, 32, avg,  _8, sse2);
         init_fpel_func(0, 1, 64, avg,  _8, sse2);
         init_lpf(sse2);
+        dsp->loop_filter_8[0][1] = ff_vp9_loop_filter_v_4_8_sse2;
         dsp->itxfm_add[TX_4X4][ADST_DCT]  = ff_vp9_idct_iadst_4x4_add_sse2;
         dsp->itxfm_add[TX_4X4][DCT_ADST]  = ff_vp9_iadst_idct_4x4_add_sse2;
         dsp->itxfm_add[TX_4X4][ADST_ADST] = ff_vp9_iadst_iadst_4x4_add_sse2;
diff --git a/libavcodec/x86/vp9lpf.asm b/libavcodec/x86/vp9lpf.asm
index a85cb026b12..e17f8579869 100644
--- a/libavcodec/x86/vp9lpf.asm
+++ b/libavcodec/x86/vp9lpf.asm
@@ -49,6 +49,26 @@ mask_mix48: times 8 db 0x00
 
 SECTION .text
 
+; x86-32 has no m8..m15, so p3..q3 stay in memory there and every use of
+; them is a memory operand. Which memory that is depends on the
+; instantiation:
+;   - the horizontal filters read the transposed stack slots, which are
+;     mmsize-aligned, so an operand can still be folded into the ALU
+;     instruction exactly as it was under MMX;
+;   - the vertical 4/8-wide filters read frame rows, which carry only
+;     8 bytes of live data and are not 16-byte aligned. With 16-byte
+;     registers those can neither be folded (a legacy-SSE memory operand
+;     must be aligned) nor read with movu (an unaligned 16-byte load at
+;     an 8-mod-16 address always straddles a 16-byte boundary, and
+;     overreads the row). They are read with movq instead.
+; LPF_MLOAD/LPF_MFOLD carry that distinction into the shared macros.
+; LPF_NARROW marks the instantiations whose registers hold 8 live bytes,
+; so the high half of every unpack is dead. LOOPFILTER sets all three per
+; instantiation; the defaults below reproduce the 16-wide code exactly.
+%define LPF_MLOAD mova
+%assign LPF_MFOLD 1
+%assign LPF_NARROW 0
+
 %macro SCRATCH 3
 %ifdef m8
     SWAP                %1, %2
@@ -66,15 +86,25 @@ SECTION .text
 %endmacro
 
 ; %1 = abs(%2-%3)
-%macro ABSSUB 4 ; dst, src1 (RO), src2 (RO), tmp
+%macro ABSSUB 5 ; dst, src1 (RO), src2 (RO), tmp, scratch (must be dead at the 
call site)
 %ifdef m8
     psubusb             %1, %3, %2
     psubusb             %4, %2, %3
-%else
+%elif LPF_MFOLD
     mova                %1, %3
     mova                %4, %2
     psubusb             %1, %2
     psubusb             %4, %3
+%else
+; Unaligned frame memory: neither source may be a legacy-SSE memory
+; operand, so both saturating differences are computed from registers,
+; with %5 holding a copy of %3 so the second subtraction still sees the
+; original value.
+    LPF_MLOAD           %1, %3
+    LPF_MLOAD           %4, %2
+    mova                %5, %1
+    psubusb             %1, %4
+    psubusb             %4, %5
 %endif
     por                 %1, %4
 %endmacro
@@ -97,7 +127,7 @@ SECTION .text
 %elifidn %2, ""
     %xdefine LPF_SRC %1
 %else
-    mova            %2, %1
+    LPF_MLOAD       %2, %1
     %xdefine LPF_SRC %2
 %endif
 %endmacro
@@ -152,7 +182,7 @@ SECTION .text
 %macro ABSDIFF_MAX_NOTMP 5 ; max, min, subtrahend, combine op, dst
     psubusb             %1, %3                          ; max(..) - %3
     %4                  %5, %1
-    mova            %1, %3
+    LPF_MLOAD       %1, %3
     psubusb             %1, %2                          ; %3 - min(..)
     %4                  %5, %1
 %endmacro
@@ -181,11 +211,23 @@ SECTION .text
     por                 %1, %4              ; new&mask | old&~mask
 %endmacro
 
+; As MASK_APPLY, but %2 is an unaligned memory operand: the 3-operand
+; pandn above emits a legacy pandn against it, which faults unless the
+; address is 16-byte aligned. Load it first, then form old & ~mask as
+; (old | mask) ^ mask, without a second scratch.
+%macro MASK_APPLY_MEM 4 ; %1=new_data/dst %2=old_data %3=mask %4=tmp
+    pand                %1, %3              ; new &= mask
+    LPF_MLOAD           %4, %2              ; tmp = old
+    por                 %4, %3              ; tmp = old | mask
+    pxor                %4, %3              ; tmp = old & ~mask
+    por                 %1, %4              ; new&mask | old&~mask
+%endmacro
+
 %macro UNPACK 4
 %ifdef m8
     punpck%1bw          %2, %3, %4
 %else
-    mova                %2, %3
+    LPF_MLOAD           %2, %3
     punpck%1bw          %2, %4
 %endif
 %endmacro
@@ -212,10 +254,16 @@ SECTION .text
 ; FIXME interleave l/h better (for instruction pairing)
 %macro FILTER_INIT 9 ; tmp1, tmp2, cacheL, cacheH, dstp, stack_off, filterid, 
mask, source
     FILTER%7_INIT       %1, l, %3, %6 +      0
+%if LPF_NARROW
+; Only the low half of every register is live, so the high unpack and
+; its half of the filter cache are dead; pack the low half with itself.
+    packuswb            %1, %1
+%else
     FILTER%7_INIT       %2, h, %4, %6 + mmsize
     packuswb            %1, %2
+%endif
     MASK_APPLY          %1, %9, %8, %2
-    mova                %5, %1
+    LPF_STORE           %5, %1
 %endmacro
 
 
@@ -228,14 +276,30 @@ SECTION .text
 %endif
 %endif
     FILTER_SUBx2_ADDx2  %1, l, %3, %6 +      0, %7, %8, %9, %10, %11, %14, %16
+%if LPF_NARROW
+    packuswb            %1, %1
+%else
     FILTER_SUBx2_ADDx2  %2, h, %4, %6 + mmsize, %7, %8, %9, %10, %11, %14, %16
     packuswb            %1, %2
+%endif
 %ifnidn %13, ""
+%ifnum sizeof%13
+    MASK_APPLY          %1, %13, %12, %2
+%elif LPF_MFOLD
     MASK_APPLY          %1, %13, %12, %2
 %else
-    MASK_APPLY          %1, %5, %12, %2
+; %13 is an unaligned frame address: keep it as the old-data source of
+; MASK_APPLY_MEM, which loads it with LPF_MLOAD. It must not be copied
+; into %2 first: MASK_APPLY with old and tmp aliased breaks the
+; 2-operand pandn expansion on SSE2.
+    MASK_APPLY_MEM      %1, %13, %12, %2
 %endif
-    mova                %5, %1
+%elif LPF_MFOLD
+    MASK_APPLY          %1, %5, %12, %2
+%else
+    MASK_APPLY_MEM      %1, %5, %12, %2
+%endif
+    LPF_STORE           %5, %1
 %endmacro
 
 %macro SRSHIFT3B_2X 3 ; reg1, reg2, tmp
@@ -252,9 +316,22 @@ SECTION .text
 %endmacro
 
 %macro ADD_SUB_CLIP 8; dst0, dst1, u8_0, u8_1, i8_0, i8_1, pb_80, tmp
+; u8_0/u8_1 are registers on x86-64 (rp/rq are m8..m15) but memory on
+; x86-32, where the aligned load of the 3-operand pxor form faults on an
+; unaligned frame row. Load it explicitly there.
+%if LPF_MFOLD
     pxor                m%8, %7, %3
+%else
+    LPF_MLOAD           m%8, %3
+    pxor                m%8, %7
+%endif
     paddsb              m%1, m%5, m%8
+%if LPF_MFOLD
     pxor                m%8, %7, %4
+%else
+    LPF_MLOAD           m%8, %4
+    pxor                m%8, %7
+%endif
 %if avx_enabled
     psubsb              m%2, m%8, m%6
 %else
@@ -437,6 +514,55 @@ SECTION .text
 %endif
 %endmacro
 
+; Per-instantiation configuration. Four of the 34 filters -- the 4- and
+; 8-wide vertical ones -- carry only 8 bytes of live data in 16-byte
+; registers and read it straight from unaligned frame rows. Everything
+; that follows from that is decided once, here, instead of being
+; re-tested at each use:
+;
+;   LPF_LOAD/LPF_STORE  how p/q rows reach and leave frame memory.
+;                       Horizontal filters work on transposed stack
+;                       slots holding 16 meaningful bytes, so they must
+;                       use the full width: a half-width access would
+;                       leave a stale high half for the transpose to mix
+;                       into the output. Vertical filters have no
+;                       transpose-back and keep half-width accesses.
+;   LPF_MLOAD           how to load a p/q operand that lives in memory.
+;   LPF_MFOLD           whether such an operand may be folded into an
+;                       ALU instruction, which legacy SSE allows only at
+;                       mmsize-aligned addresses.
+;   LPF_NARROW          whether the high half of every register is dead,
+;                       so the high unpack of filter6 can be skipped.
+;
+; The defaults reproduce the 16-wide and mix2 code exactly, so those
+; instantiations are unaffected by all of this.
+%macro LPF_CONFIG 2 ; %1=v/h %2=size1
+%define LPF_LOAD mova
+%define LPF_STORE mova
+%define LPF_MLOAD mova
+%assign LPF_MFOLD 1
+%assign LPF_NARROW 0
+%if mmsize == 16 && (%2 == 4 || %2 == 8)
+%assign LPF_NARROW 1
+%ifidn %1, v
+%define LPF_LOAD movq
+%define LPF_STORE movh
+%define LPF_MLOAD movq
+%assign LPF_MFOLD 0
+%endif
+%endif
+%endmacro
+
+; Restore the file-scope defaults, so a later instantiation cannot
+; inherit the previous one's configuration.
+%macro LPF_CONFIG_RESET 0
+%undef LPF_LOAD
+%undef LPF_STORE
+%define LPF_MLOAD mova
+%assign LPF_MFOLD 1
+%assign LPF_NARROW 0
+%endmacro
+
 ; 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.
@@ -607,7 +733,7 @@ SECTION .text
 ; 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 %2 <= 16
 %if cpuflag(ssse3)
     pxor                m0, m0
 %endif
@@ -627,14 +753,14 @@ SECTION .text
     pxor                m3, m0
 %ifdef m8
 %ifidn %1, v
-    mova                m8, [P3]
-    mova                m9, [P2]
-    mova               m10, [P1]
-    mova               m11, [P0]
-    mova               m12, [Q0]
-    mova               m13, [Q1]
-    mova               m14, [Q2]
-    mova               m15, [Q3]
+    LPF_LOAD            m8, [P3]
+    LPF_LOAD            m9, [P2]
+    LPF_LOAD           m10, [P1]
+    LPF_LOAD           m11, [P0]
+    LPF_LOAD           m12, [Q0]
+    LPF_LOAD           m13, [Q1]
+    LPF_LOAD           m14, [Q2]
+    LPF_LOAD           m15, [Q3]
 %else
     ; In case of horizontal, P3..Q3 are already present in some registers due
     ; to the previous transpose, so we just swap registers.
@@ -663,16 +789,16 @@ SECTION .text
 %endif
     MINMAX              m5, m1, rp1, m6, rp3            ; max(p1,p3), 
min(p1,p3)
     ABSDIFF_MAX         m5, m1, rp2, pmaxub, m6         ; 
max(abs(p3-p2),abs(p2-p1))
-    ABSSUB              m1, rp1, rp0, m7                ; m1 = abs(p1-p0)
-    ABSSUB              m4, rq0, rq1, m7                ; m1 = abs(q1-q0)
+    ABSSUB              m1, rp1, rp0, m7, m6            ; m1 = abs(p1-p0)
+    ABSSUB              m4, rq0, rq1, m7, m6            ; m1 = abs(q1-q0)
     pmaxub              m4, m1
     MINMAX              m1, m6, rq3, m7, rq1            ; max(q1,q3), 
min(q1,q3)
     pmaxub              m5, m4
     ABSDIFF_MAX         m1, m6, rq2, pmaxub, m7, m5
     CMP_GT              m5, m2, m0
-    ABSSUB              m1, rp0, rq0, m7                ; abs(p0-q0)
+    ABSSUB              m1, rp0, rq0, m7, m6            ; abs(p0-q0)
     paddusb             m1, m1                          ; abs(p0-q0) * 2
-    ABSSUB              m2, rp1, rq1, m7                ; abs(p1-q1)
+    ABSSUB              m2, rp1, rq1, m7, m6            ; abs(p1-q1)
     pand                m2, [pb_fe]                     ; drop lsb so shift 
can work
     psrlq               m2, 1                           ; abs(p1-q1)/2
     paddusb             m1, m2                          ; abs(p0-q0)*2 + 
abs(p1-q1)/2
@@ -745,6 +871,9 @@ SECTION .text
 %ifdef m8
     %define M8 m8
 %else
+; M8 is only used inside `%if %2 == 16`. It must stay
+; empty here: m6 holds live pb_82 in that flow, and giving
+; M8 a real register clobbers it.
     %define M8 ""
 %endif
     MINMAX              m5, m7, [Q5], M8, [Q4]           ; max(q4,q5), 
min(q4,q5)
@@ -785,11 +914,23 @@ SECTION .text
 %if %2 == 16
     SCRATCH              1,  8, rsp+%3+%4+16
 %endif
+%if LPF_MFOLD
     pxor                m5, m4, rp1                     ; p1 - 128
     pxor                m7, m4, rq1                     ; q1 - 128
     pxor                m6, m4, rq0                     ; q0 - 128
     psubsb              m5, m7                          ; clip_i8(p1 - q1)
     pxor                m7, m4, rp0                     ; p0 - 128
+%else
+    LPF_MLOAD           m5, rp1                         ; p1 - 128
+    pxor                m5, m4
+    LPF_MLOAD           m7, rq1                         ; q1 - 128
+    pxor                m7, m4
+    LPF_MLOAD           m6, rq0                         ; q0 - 128
+    pxor                m6, m4
+    psubsb              m5, m7                          ; clip_i8(p1 - q1)
+    LPF_MLOAD           m7, rp0                         ; p0 - 128
+    pxor                m7, m4
+%endif
     ; mask so we can treat the !hev and hev cases in parallel
     pand                m5, m0                          ; hev ? clip_i8(p1 - 
q1) : 0
     psubsb              m6, m7                          ; clip_i8(q0 - p0)
@@ -809,8 +950,8 @@ SECTION .text
     SRSHIFT3B_2X        m6, m5, m7                      ; f1 and f2 sign byte 
shift by 3
     pandn               m0, m6                          ; hev ? 0 : f1
     ADD_SUB_CLIP         5, 6, rp0, rq0, 5, 6, m4, 7    ; m5 = p0 + f2, m6 = 
q0 - f1
-    mova              [Q0], m6
-    mova              [P0], m5
+    LPF_STORE         [Q0], m6
+    LPF_STORE         [P0], m5
     pxor                m0, m4                          ; f1 ^ 0x80
     pxor                m7, m7
     pavgb               m0, m7
@@ -820,8 +961,8 @@ SECTION .text
     SWAP                 2, 6                           ; m2 = q0
 %endif
     ADD_SUB_CLIP         6, 7, rp1, rq1, 0, 0, m4, 5    ; m6 = p1 + f, m7 = q1 
- f
-    mova              [P1], m6
-    mova              [Q1], m7
+    LPF_STORE         [P1], m6
+    LPF_STORE         [Q1], m7
 
     ; ([m1: flat8out], m2: flat8in, m3: fm, m10..13: p1 p0 q0 q1)
     ; filter6()
@@ -839,15 +980,15 @@ SECTION .text
 %endif
 %endif
 %ifdef m8
-    mova               m14, [P3]
-    mova                m9, [Q3]
+    LPF_LOAD           m14, [P3]
+    LPF_LOAD            m9, [Q3]
 %define rp3 m14
 %define rq3 m9
 %else
 %define rp3 [P3]
 %define rq3 [Q3]
 %endif
-    mova                m1, [P2]
+    LPF_LOAD            m1, [P2]
     FILTER_INIT         m4, m5, m6, m7, [P2], %4, 6,             m3,  m1       
      ; [p2]
     mova                m1, [Q2]
     FILTER_UPDATE       m4, m5, m6, m7, [P1], %4, 0, 1, 2, 5, 3, m3,  "", rq1, 
"", 1 ; [p1] -p3 -p2 +p1 +q1
@@ -1185,6 +1326,7 @@ SECTION .text
 %if ARCH_X86_32 || mmsize == 8
 %assign %%ext %5
 %endif
+    LPF_CONFIG %1, %2
 
 %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
@@ -1231,6 +1373,7 @@ cglobal vp9_loop_filter_%1_%2_ %+ LPF_NPX, 2, 6, 16, %3 + 
%4 + %%ext, dst, strid
     LPF_FILTER2_4_6 %1, %2, %3, %4
     LPF_FILTER14 %1, %2, %3, %4
     LPF_TRANSPOSE_OUT %1, %2, %3, %4
+    LPF_CONFIG_RESET
 
     RET
 %endmacro
@@ -1253,8 +1396,9 @@ LPF_16_VH_ALL_OPTS 48, 256, 128, 16
 LPF_16_VH_ALL_OPTS 84, 256, 128, 16
 LPF_16_VH_ALL_OPTS 88, 256, 128, 16
 
-INIT_MMX mmxext
+INIT_XMM sse2
 LOOPFILTER v, 4,   0,  0, 0
+INIT_MMX mmxext
 LOOPFILTER h, 4,   0, 64, 0
 LOOPFILTER v, 8, 128,  0, 8
 LOOPFILTER h, 8, 128, 64, 8
-- 
2.54.1


>From 2a97796429c045eeffb0a421e4f7f05c24a80b96 Mon Sep 17 00:00:00 2001
From: Zuxy Meng <[email protected]>
Date: Mon, 2 Nov 2026 21:56:51 -0800
Subject: [PATCH 3/5] avcodec/x86/vp9: convert loop_filter_v_8 MMXEXT to SSE2

Convert the 8-wide vertical loop filter to SSE2 and assign it under
EXTERNAL_SSE2.

Benchmarks (checkasm --bench --duration=20000, Zen 4, cycles):
  vp9_loop_filter_v_8_8_8bpp: x86-64 66.1 -> 40.1; i686 66.0 -> 67.6.

checkasm --test=vp9dsp and fate-vp9 pass on x86-64 and i686.

Signed-off-by: Zuxy Meng <[email protected]>
---
 libavcodec/x86/vp9dsp_init.c | 3 ++-
 libavcodec/x86/vp9lpf.asm    | 4 ++--
 2 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/libavcodec/x86/vp9dsp_init.c b/libavcodec/x86/vp9dsp_init.c
index 7d3a7e61308..b5be81d53a6 100644
--- a/libavcodec/x86/vp9dsp_init.c
+++ b/libavcodec/x86/vp9dsp_init.c
@@ -132,6 +132,7 @@ void ff_vp9_loop_filter_h_##size1##_##size2##_##opt(uint8_t 
*dst, ptrdiff_t stri
 lpf_funcs(4, 8, mmxext);
 lpf_funcs(8, 8, mmxext);
 lpf_funcs(4, 8, sse2);
+lpf_funcs(8, 8, sse2);
 lpf_funcs(16, 16, sse2);
 lpf_funcs(16, 16, ssse3);
 lpf_funcs(16, 16, avx);
@@ -280,7 +281,6 @@ av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int 
bpp, int bitexact)
     if (EXTERNAL_MMXEXT(cpu_flags)) {
         dsp->loop_filter_8[0][0] = ff_vp9_loop_filter_h_4_8_mmxext;
         dsp->loop_filter_8[1][0] = ff_vp9_loop_filter_h_8_8_mmxext;
-        dsp->loop_filter_8[1][1] = ff_vp9_loop_filter_v_8_8_mmxext;
     }
 
     if (EXTERNAL_SSE(cpu_flags)) {
@@ -304,6 +304,7 @@ av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int 
bpp, int bitexact)
         init_fpel_func(0, 1, 64, avg,  _8, sse2);
         init_lpf(sse2);
         dsp->loop_filter_8[0][1] = ff_vp9_loop_filter_v_4_8_sse2;
+        dsp->loop_filter_8[1][1] = ff_vp9_loop_filter_v_8_8_sse2;
         dsp->itxfm_add[TX_4X4][ADST_DCT]  = ff_vp9_idct_iadst_4x4_add_sse2;
         dsp->itxfm_add[TX_4X4][DCT_ADST]  = ff_vp9_iadst_idct_4x4_add_sse2;
         dsp->itxfm_add[TX_4X4][ADST_ADST] = ff_vp9_iadst_iadst_4x4_add_sse2;
diff --git a/libavcodec/x86/vp9lpf.asm b/libavcodec/x86/vp9lpf.asm
index e17f8579869..f83d394722f 100644
--- a/libavcodec/x86/vp9lpf.asm
+++ b/libavcodec/x86/vp9lpf.asm
@@ -990,7 +990,7 @@ SECTION .text
 %endif
     LPF_LOAD            m1, [P2]
     FILTER_INIT         m4, m5, m6, m7, [P2], %4, 6,             m3,  m1       
      ; [p2]
-    mova                m1, [Q2]
+    LPF_LOAD            m1, [Q2]
     FILTER_UPDATE       m4, m5, m6, m7, [P1], %4, 0, 1, 2, 5, 3, m3,  "", rq1, 
"", 1 ; [p1] -p3 -p2 +p1 +q1
     FILTER_UPDATE       m4, m5, m6, m7, [P0], %4, 0, 2, 3, 6, 3, m3,  "", m1   
      ; [p0] -p3 -p1 +p0 +q2
     FILTER_UPDATE       m4, m5, m6, m7, [Q0], %4, 0, 3, 4, 7, 3, m3,  "", rq3, 
"", 1 ; [q0] -p3 -p0 +q0 +q3
@@ -1398,7 +1398,7 @@ LPF_16_VH_ALL_OPTS 88, 256, 128, 16
 
 INIT_XMM sse2
 LOOPFILTER v, 4,   0,  0, 0
+LOOPFILTER v, 8, 256,  0, 16
 INIT_MMX mmxext
 LOOPFILTER h, 4,   0, 64, 0
-LOOPFILTER v, 8, 128,  0, 8
 LOOPFILTER h, 8, 128, 64, 8
-- 
2.54.1


>From 583697f401ab3fc0c9a939920889f24fda847389 Mon Sep 17 00:00:00 2001
From: Zuxy Meng <[email protected]>
Date: Mon, 9 Nov 2026 18:42:23 -0800
Subject: [PATCH 4/5] avcodec/x86/vp9: convert loop_filter_h_4 MMXEXT to SSE2

Convert the 4-wide horizontal loop filter to SSE2 and assign it under
EXTERNAL_SSE2. Zero the unused Q registers instead of loading and
storing past the 8-row window.

Benchmarks (checkasm --bench --duration=20000, Zen 4, cycles):
  vp9_loop_filter_h_4_8_8bpp: x86-64 52.6 -> 45.6; i686 52.3 -> 56.2.

checkasm --test=vp9dsp and fate-vp9 pass on x86-64 and i686.

Signed-off-by: Zuxy Meng <[email protected]>
---
 libavcodec/x86/vp9dsp_init.c |  2 +-
 libavcodec/x86/vp9lpf.asm    | 72 ++++++++++++++++++++++++++++--------
 2 files changed, 57 insertions(+), 17 deletions(-)

diff --git a/libavcodec/x86/vp9dsp_init.c b/libavcodec/x86/vp9dsp_init.c
index b5be81d53a6..17b0b8fe7a4 100644
--- a/libavcodec/x86/vp9dsp_init.c
+++ b/libavcodec/x86/vp9dsp_init.c
@@ -279,7 +279,6 @@ av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int 
bpp, int bitexact)
     }
 
     if (EXTERNAL_MMXEXT(cpu_flags)) {
-        dsp->loop_filter_8[0][0] = ff_vp9_loop_filter_h_4_8_mmxext;
         dsp->loop_filter_8[1][0] = ff_vp9_loop_filter_h_8_8_mmxext;
     }
 
@@ -303,6 +302,7 @@ av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int 
bpp, int bitexact)
         init_fpel_func(1, 1, 32, avg,  _8, sse2);
         init_fpel_func(0, 1, 64, avg,  _8, sse2);
         init_lpf(sse2);
+        dsp->loop_filter_8[0][0] = ff_vp9_loop_filter_h_4_8_sse2;
         dsp->loop_filter_8[0][1] = ff_vp9_loop_filter_v_4_8_sse2;
         dsp->loop_filter_8[1][1] = ff_vp9_loop_filter_v_8_8_sse2;
         dsp->itxfm_add[TX_4X4][ADST_DCT]  = ff_vp9_idct_iadst_4x4_add_sse2;
diff --git a/libavcodec/x86/vp9lpf.asm b/libavcodec/x86/vp9lpf.asm
index f83d394722f..19c487ae500 100644
--- a/libavcodec/x86/vp9lpf.asm
+++ b/libavcodec/x86/vp9lpf.asm
@@ -584,6 +584,16 @@ SECTION .text
 %endif
     movx                    m7, [P0]
 %ifdef m8
+%if %2 <= 8
+    pxor                    m8, m8
+    pxor                    m9, m9
+    pxor                   m10, m10
+    pxor                   m11, m11
+    pxor                   m12, m12
+    pxor                   m13, m13
+    pxor                   m14, m14
+    pxor                   m15, m15
+%else
     movx                    m8, [Q0]
     movx                    m9, [Q1]
     movx                   m10, [Q2]
@@ -592,6 +602,7 @@ SECTION .text
     movx                   m13, [Q5]
     movx                   m14, [Q6]
     movx                   m15, [Q7]
+%endif
     DEFINE_TRANSPOSED_P7_TO_Q7
 %if %2 == 16
     TRANSPOSE16x16B 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, [rsp]
@@ -702,11 +713,14 @@ SECTION .text
     SBUTTERFLY       bw, 0, 1, 6
     SBUTTERFLY       bw, 2, 3, 6
     SBUTTERFLY       bw, 4, 5, 6
+%if mmsize == 8
     mova [rsp+4*mmsize], m5
-    mova             m6, [P1]
+%endif
+    movx             m6, [P1]
     SBUTTERFLY       bw, 6, 7, 5
     DEFINE_TRANSPOSED_P7_TO_Q7
     TRANSPOSE4x4W     0, 2, 4, 6, 5
+%if mmsize == 8
     mova           [P3], m0
     mova           [P2], m2
     mova           [P1], m4
@@ -717,6 +731,27 @@ SECTION .text
     mova           [Q1], m3
     mova           [Q2], m5
     mova           [Q3], m7
+%else
+; The 8-byte row loads leave the high halves zero, so the odd registers
+; of the byte butterflies above are zero and TRANSPOSE4x4W already
+; produced all eight columns, packed two per register. Spill each
+; register twice, shifted, rather than running a second (all-zero)
+; transpose and splitting the pairs afterwards: the stores stay mmsize
+; wide, so the operand loads below forward from them, and only the low
+; half of each slot is ever read back.
+    mova           [P3], m0
+    psrldq           m0, 8
+    mova           [P2], m0
+    mova           [P1], m2
+    psrldq           m2, 8
+    mova           [P0], m2
+    mova           [Q0], m4
+    psrldq           m4, 8
+    mova           [Q1], m4
+    mova           [Q2], m6
+    psrldq           m6, 8
+    mova           [Q3], m6
+%endif
 %endif ; %2
 %endif ; x86-32/64
 %endif ; %1 == h
@@ -1182,48 +1217,53 @@ SECTION .text
     SBUTTERFLY  bw, 2, 3, 4
     SBUTTERFLY  wd, 0, 2, 4
     SBUTTERFLY  wd, 1, 3, 4
-%if mmsize == 16
     movd  [P7], m0
     movd  [P3], m2
+%if %2 != 4
     movd  [Q0], m1
     movd  [Q4], m3
+%endif
     psrldq  m0, 4
+%if %2 != 4
     psrldq  m1, 4
+%endif
     psrldq  m2, 4
+%if %2 != 4
     psrldq  m3, 4
+%endif
     movd  [P6], m0
     movd  [P2], m2
+%if %2 != 4
     movd  [Q1], m1
     movd  [Q5], m3
+%endif
     psrldq  m0, 4
+%if %2 != 4
     psrldq  m1, 4
+%endif
     psrldq  m2, 4
+%if %2 != 4
     psrldq  m3, 4
+%endif
     movd  [P5], m0
     movd  [P1], m2
+%if %2 != 4
     movd  [Q2], m1
     movd  [Q6], m3
+%endif
     psrldq  m0, 4
+%if %2 != 4
     psrldq  m1, 4
+%endif
     psrldq  m2, 4
+%if %2 != 4
     psrldq  m3, 4
+%endif
     movd  [P4], m0
     movd  [P0], m2
+%if %2 != 4
     movd  [Q3], m1
     movd  [Q7], m3
-%else
-    movd  [P7], m0
-    movd  [P5], m2
-    movd  [P3], m1
-    movd  [P1], m3
-    psrlq   m0, 32
-    psrlq   m2, 32
-    psrlq   m1, 32
-    psrlq   m3, 32
-    movd  [P6], m0
-    movd  [P4], m2
-    movd  [P2], m1
-    movd  [P0], m3
 %endif
 %else
     ; the following code do a transpose of 8 full lines to 16 half
@@ -1399,6 +1439,6 @@ LPF_16_VH_ALL_OPTS 88, 256, 128, 16
 INIT_XMM sse2
 LOOPFILTER v, 4,   0,  0, 0
 LOOPFILTER v, 8, 256,  0, 16
+LOOPFILTER h, 4,   0, 128, 0
 INIT_MMX mmxext
-LOOPFILTER h, 4,   0, 64, 0
 LOOPFILTER h, 8, 128, 64, 8
-- 
2.54.1


>From 6a4e50f4c764ecfd97804a8bc2e5532280cbaa3f Mon Sep 17 00:00:00 2001
From: Zuxy Meng <[email protected]>
Date: Mon, 2 Nov 2026 21:58:28 -0800
Subject: [PATCH 5/5] avcodec/x86/vp9: convert loop_filter_h_8 MMXEXT to SSE2

Convert the 8-wide horizontal loop filter to SSE2, completing the
4/8-wide conversions, and assign it under EXTERNAL_SSE2. Drop the
now-dead MMX-only macro branches.

Benchmarks (checkasm --bench --duration=20000, Zen 4, cycles):
  vp9_loop_filter_h_8_8_8bpp: x86-64 94.0 -> 61.4; i686 93.7 -> 97.5.

Series totals, same protocol (x86-64 mmxext -> sse2; i686 mmxext -> sse2):
  v_4: 29.4 -> 29.0; 29.4 -> 33.2.
  v_8: 66.1 -> 40.1; 66.0 -> 67.6.
  h_4: 52.6 -> 45.6; 52.3 -> 56.2.
  h_8: 94.0 -> 61.4; 93.7 -> 97.5.
x86-64 is faster on every filter; i686 stays roughly even, and 32-bit
decode wall-clock is flat.

checkasm --test=vp9dsp and fate-vp9 pass on x86-64 and i686.

Signed-off-by: Zuxy Meng <[email protected]>
---
 libavcodec/x86/vp9dsp_init.c |  7 +----
 libavcodec/x86/vp9lpf.asm    | 57 +++++++++++-------------------------
 tests/checkasm/vp9dsp.c      |  2 +-
 3 files changed, 19 insertions(+), 47 deletions(-)

diff --git a/libavcodec/x86/vp9dsp_init.c b/libavcodec/x86/vp9dsp_init.c
index 17b0b8fe7a4..71068d84ab6 100644
--- a/libavcodec/x86/vp9dsp_init.c
+++ b/libavcodec/x86/vp9dsp_init.c
@@ -129,8 +129,6 @@ void ff_vp9_loop_filter_v_##size1##_##size2##_##opt(uint8_t 
*dst, ptrdiff_t stri
 void ff_vp9_loop_filter_h_##size1##_##size2##_##opt(uint8_t *dst, ptrdiff_t 
stride, \
                                                     int E, int I, int H)
 
-lpf_funcs(4, 8, mmxext);
-lpf_funcs(8, 8, mmxext);
 lpf_funcs(4, 8, sse2);
 lpf_funcs(8, 8, sse2);
 lpf_funcs(16, 16, sse2);
@@ -278,10 +276,6 @@ av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int 
bpp, int bitexact)
         }
     }
 
-    if (EXTERNAL_MMXEXT(cpu_flags)) {
-        dsp->loop_filter_8[1][0] = ff_vp9_loop_filter_h_8_8_mmxext;
-    }
-
     if (EXTERNAL_SSE(cpu_flags)) {
         init_fpel_func(2, 0, 16, put, , sse);
         init_fpel_func(1, 0, 32, put, , sse);
@@ -304,6 +298,7 @@ av_cold void ff_vp9dsp_init_x86(VP9DSPContext *dsp, int 
bpp, int bitexact)
         init_lpf(sse2);
         dsp->loop_filter_8[0][0] = ff_vp9_loop_filter_h_4_8_sse2;
         dsp->loop_filter_8[0][1] = ff_vp9_loop_filter_v_4_8_sse2;
+        dsp->loop_filter_8[1][0] = ff_vp9_loop_filter_h_8_8_sse2;
         dsp->loop_filter_8[1][1] = ff_vp9_loop_filter_v_8_8_sse2;
         dsp->itxfm_add[TX_4X4][ADST_DCT]  = ff_vp9_idct_iadst_4x4_add_sse2;
         dsp->itxfm_add[TX_4X4][DCT_ADST]  = ff_vp9_iadst_idct_4x4_add_sse2;
diff --git a/libavcodec/x86/vp9lpf.asm b/libavcodec/x86/vp9lpf.asm
index 19c487ae500..74841982373 100644
--- a/libavcodec/x86/vp9lpf.asm
+++ b/libavcodec/x86/vp9lpf.asm
@@ -491,7 +491,6 @@ SECTION .text
 %define Q1 rsp +  5*mmsize + %1
 %define Q2 rsp +  6*mmsize + %1
 %define Q3 rsp +  7*mmsize + %1
-%if mmsize == 16
 %define P7 rsp +  8*mmsize + %1
 %define P6 rsp +  9*mmsize + %1
 %define P5 rsp + 10*mmsize + %1
@@ -500,7 +499,6 @@ SECTION .text
 %define Q5 rsp + 13*mmsize + %1
 %define Q6 rsp + 14*mmsize + %1
 %define Q7 rsp + 15*mmsize + %1
-%endif
 %endmacro
 
 ; ..............AB -> AAAAAAAABBBBBBBB
@@ -579,7 +577,7 @@ SECTION .text
     movx                    m3, [P4]
     movx                    m4, [P3]
     movx                    m5, [P2]
-%if (ARCH_X86_64 && mmsize == 16) || %2 > 16
+%if ARCH_X86_64 || %2 > 16
     movx                    m6, [P1]
 %endif
     movx                    m7, [P0]
@@ -713,25 +711,10 @@ SECTION .text
     SBUTTERFLY       bw, 0, 1, 6
     SBUTTERFLY       bw, 2, 3, 6
     SBUTTERFLY       bw, 4, 5, 6
-%if mmsize == 8
-    mova [rsp+4*mmsize], m5
-%endif
     movx             m6, [P1]
     SBUTTERFLY       bw, 6, 7, 5
     DEFINE_TRANSPOSED_P7_TO_Q7
     TRANSPOSE4x4W     0, 2, 4, 6, 5
-%if mmsize == 8
-    mova           [P3], m0
-    mova           [P2], m2
-    mova           [P1], m4
-    mova           [P0], m6
-    mova             m5, [rsp+4*mmsize]
-    TRANSPOSE4x4W     1, 3, 5, 7, 0
-    mova           [Q0], m1
-    mova           [Q1], m3
-    mova           [Q2], m5
-    mova           [Q3], m7
-%else
 ; The 8-byte row loads leave the high halves zero, so the odd registers
 ; of the byte butterflies above are zero and TRANSPOSE4x4W already
 ; produced all eight columns, packed two per register. Spill each
@@ -751,7 +734,6 @@ SECTION .text
     mova           [Q2], m6
     psrldq           m6, 8
     mova           [Q3], m6
-%endif
 %endif ; %2
 %endif ; x86-32/64
 %endif ; %1 == h
@@ -1193,7 +1175,15 @@ SECTION .text
     mova                    m5, [Q5]
     mova                    m7, [Q7]
     DEFINE_REAL_P7_TO_Q7 8
+%ifdef m8
+    TRANSPOSE8x8B 0, 1, 2, 3, 4, 5, 6, 7, [rsp+224], a, m8, [Q0], [Q1]
+%else
+; No m8 on x86-32: keep the stack slot, as at base. It is in
+; frame for %2 == 16 (the only path reaching this 32-bit-only
+; branch), unlike the h,8 case the scratch register was
+; introduced for.
     TRANSPOSE8x8B 0, 1, 2, 3, 4, 5, 6, 7, [rsp+224], a, [rsp+%3+%4], [Q0], [Q1]
+%endif
     movh   [P7],  m0
     movh   [P5],  m1
     movh   [P3],  m2
@@ -1307,11 +1297,9 @@ SECTION .text
     SBUTTERFLY  wd,  5,  7, 2
     SBUTTERFLY  dq,  0,  4, 2
     SBUTTERFLY  dq,  1,  5, 2
-%if mmsize == 16
+%if %2 != 8 && %2 != 4
     movh      [Q0], m1
     movhps    [Q1], m1
-%else
-    mova      [P3], m1
 %endif
     mova        m2, [rsp+mmsize*6]
     SBUTTERFLY  dq,  2,  6, 1
@@ -1319,7 +1307,6 @@ SECTION .text
 %endif
     SWAP         3, 6
     SWAP         1, 4
-%if mmsize == 16
     movh      [P7], m0
     movhps    [P6], m0
     movh      [P5], m1
@@ -1328,6 +1315,10 @@ SECTION .text
     movhps    [P2], m2
     movh      [P1], m3
     movhps    [P0], m3
+; The transposed block holds a second 8-row half (Q0..Q7) that only
+; the 16-row (16-wide/mix2) filters own: the 4/8-row filters must not
+; store it, or they corrupt the next block and write past the frame.
+%if %2 != 8 && %2 != 4
 %ifdef m8
     movh      [Q0], m4
     movhps    [Q1], m4
@@ -1338,20 +1329,12 @@ SECTION .text
     movhps    [Q5], m6
     movh      [Q6], m7
     movhps    [Q7], m7
-%else
-    mova      [P7], m0
-    mova      [P6], m1
-    mova      [P5], m2
-    mova      [P4], m3
-    mova      [P2], m5
-    mova      [P1], m6
-    mova      [P0], m7
 %endif
 %endif
 %endif
 %endmacro
 
-%macro LOOPFILTER 5 ; %1=v/h %2=size1 %3+%4=stack, %5=mmx/32bit stack only
+%macro LOOPFILTER 5 ; %1=v/h %2=size1 %3+%4=stack, %5=32-bit 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
@@ -1363,7 +1346,7 @@ SECTION .text
 %define LPF_NPX 16
 %endif
 %assign %%ext 0
-%if ARCH_X86_32 || mmsize == 8
+%if ARCH_X86_32
 %assign %%ext %5
 %endif
     LPF_CONFIG %1, %2
@@ -1389,11 +1372,7 @@ cglobal vp9_loop_filter_%1_%2_ %+ LPF_NPX, 2, 6, 16, %3 
+ %4 + %%ext, dst, strid
 
 %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
@@ -1402,7 +1381,6 @@ cglobal vp9_loop_filter_%1_%2_ %+ LPF_NPX, 2, 6, 16, %3 + 
%4 + %%ext, dst, strid
 %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
@@ -1440,5 +1418,4 @@ INIT_XMM sse2
 LOOPFILTER v, 4,   0,  0, 0
 LOOPFILTER v, 8, 256,  0, 16
 LOOPFILTER h, 4,   0, 128, 0
-INIT_MMX mmxext
-LOOPFILTER h, 8, 128, 64, 8
+LOOPFILTER h, 8, 256, 128, 16
diff --git a/tests/checkasm/vp9dsp.c b/tests/checkasm/vp9dsp.c
index 986c5802a39..690d5ed2497 100644
--- a/tests/checkasm/vp9dsp.c
+++ b/tests/checkasm/vp9dsp.c
@@ -458,7 +458,7 @@ static void check_loopfilter(void)
     static const char *const dir_name[2] = { "h", "v" };
     static const int E[2] = { 20, 28 }, I[2] = { 10, 16 };
     static const int H[2] = { 7, 11 }, F[2] = { 1, 1 };
-    declare_func_emms(AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT, void, uint8_t 
*dst, ptrdiff_t stride, int E, int I, int H);
+    declare_func(void, uint8_t *dst, ptrdiff_t stride, int E, int I, int H);
 
     for (bit_depth = 8; bit_depth <= 12; bit_depth += 2) {
         ff_vp9dsp_init(&dsp, bit_depth, 0);
-- 
2.54.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to