Commit 70a5114d7c for ffmpeg

commit 70a5114d7cbcf59e3a73e607928922eda94f1d6c
Author: Zuxy Meng <zuxy.meng@gmail.com>
Date:   Mon Nov 9 19:18:37 2026 -0800

    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. CPUs previously served by MMXEXT alone fall back to C.
    The p/q operand loads share a single LPF_LOAD setting, configured
    only in LPF_CONFIG.

    Benchmarks (checkasm --bench --duration=20000, Zen 4, cycles, median
    of 3, pinned to one core):
      vp9_loop_filter_v_4_8_8bpp: x86-64 mmxext 40.2 -> sse2 39.5; i686 mmxext 40.0 -> sse2 58.7.

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

    The narrow specialization is SSE2-only; MMX keeps the legacy
    full-width sequences.

    Signed-off-by: Zuxy Meng <zuxy.meng@gmail.com>

diff --git a/libavcodec/x86/vp9dsp_init.c b/libavcodec/x86/vp9dsp_init.c
index 3e8e1fc8ca..7d3a7e6130 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 cc4076a397..16a41e8983 100644
--- a/libavcodec/x86/vp9lpf.asm
+++ b/libavcodec/x86/vp9lpf.asm
@@ -49,6 +49,17 @@ 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 eight bytes from frame rows.
+;     These operands need only be eight-byte aligned, so use movq.
+;     Legacy SSE ALU memory operands require 16-byte alignment, and
+;     a 16-byte unaligned load would read beyond the eight pixels.
+
 %macro SCRATCH 3
 %ifdef m8
     SWAP                %1, %2
@@ -66,15 +77,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_LOAD            %1, %3
+    LPF_LOAD            %4, %2
+    mova                %5, %1
+    psubusb             %1, %4
+    psubusb             %4, %5
 %endif
     por                 %1, %4
 %endmacro
@@ -97,7 +118,7 @@ SECTION .text
 %elifidn %2, ""
     %xdefine LPF_SRC %1
 %else
-    mova            %2, %1
+    LPF_LOAD            %2, %1
     %xdefine LPF_SRC %2
 %endif
 %endmacro
@@ -152,7 +173,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_LOAD            %1, %3
     psubusb             %1, %2                          ; %3 - min(..)
     %4                  %5, %1
 %endmacro
@@ -181,11 +202,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_LOAD            %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_LOAD            %2, %3
     punpck%1bw          %2, %4
 %endif
 %endmacro
@@ -212,10 +245,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 +267,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
+; %13 is an unaligned frame address: keep it as the old-data source of
+; MASK_APPLY_MEM, which loads it with LPF_LOAD. 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
+%elif LPF_MFOLD
     MASK_APPLY          %1, %5, %12, %2
+%else
+    MASK_APPLY_MEM      %1, %5, %12, %2
 %endif
-    mova                %5, %1
+    LPF_STORE           %5, %1
 %endmacro

 %macro SRSHIFT3B_2X 3 ; reg1, reg2, tmp
@@ -252,9 +307,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_LOAD            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_LOAD            m%8, %4
+    pxor                m%8, %7
+%endif
 %if avx_enabled
     psubsb              m%2, m%8, m%6
 %else
@@ -437,6 +505,42 @@ SECTION .text
 %endif
 %endmacro

+; All four single-block filters have eight live byte lanes. The two
+; vertical ones read frame rows directly. The two horizontal ones
+; transpose eight rows into aligned stack slots.
+;
+;   LPF_LOAD/LPF_STORE  access width for p/q operands. Narrow vertical
+;                       filters access eight frame bytes. Horizontal
+;                       filters use full-width stack accesses, with only
+;                       the low eight lanes live for single-block widths.
+;   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
+%assign LPF_MFOLD 1
+%assign LPF_NARROW 0
+; MMX registers hold exactly the eight live bytes with no dead high
+; half, so the packed narrow sequences below do not apply to them;
+; MMX keeps the legacy full-width code throughout the series.
+%if (%2 == 4 || %2 == 8)
+%if cpuflag(sse2)
+%assign LPF_NARROW 1
+%ifidn %1, v
+%define LPF_LOAD movq
+%define LPF_STORE movh
+%assign LPF_MFOLD 0
+%endif
+%endif
+%endif
+%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.
@@ -597,7 +701,7 @@ SECTION .text
 %endif ; %1 == h
 %endmacro

-; Compute the three masks that select which filter each pixel gets:
+; Compute the 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)
@@ -609,7 +713,7 @@ SECTION .text
 ; 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
@@ -629,14 +733,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.
@@ -665,16 +769,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
@@ -747,6 +851,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)
@@ -787,11 +894,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_LOAD            m5, rp1                         ; p1 - 128
+    pxor                m5, m4
+    LPF_LOAD            m7, rq1                         ; q1 - 128
+    pxor                m7, m4
+    LPF_LOAD            m6, rq0                         ; q0 - 128
+    pxor                m6, m4
+    psubsb              m5, m7                          ; clip_i8(p1 - q1)
+    LPF_LOAD            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)
@@ -811,8 +930,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
@@ -822,8 +941,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()
@@ -841,15 +960,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 +1304,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
@@ -1253,8 +1373,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