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

aarch64 has no NEON code for the VC-1 bicubic (mspel) motion compensation, so 
`put_vc1_mspel_pixels_tab` and `avg_vc1_mspel_pixels_tab` run the C functions 
there. 32-bit ARM has had NEON versions for a long time. These functions are 
used for the luma of interlaced pictures (field and frame MBs with 4 MVs, field 
MVs) and for the backward half of the interpolated (bidirectional) prediction 
in B pictures, so they are hot in interlaced and B-heavy content. In a 
single-thread profile of interlaced Blu-ray content on an Apple M3 Max they 
were one of the largest remaining costs.

This adds NEON versions of the 15 fractional-pel modes (mc01 to mc33) for put 
and avg, 8x8 and 16x16, and of the full-pel avg (mc00). Full-pel put is a plain 
copy; in checkasm a NEON version was slower than C for 8x8 and only 1.2x-1.6x 
faster for 16x16 (Shield and M3 Max), so the AI left it alone.

- put and avg come from the same macros. The avg version loads the destination 
and averages with `urhadd` before the store, which is `op_avg` in `vc1dsp.c`.
- The one-dimensional modes accumulate the 4-tap filter in 16 bits with 
`umlal`/`umlsl` on the tap magnitudes.
- The two-dimensional modes do the vertical pass first, keep the shifted 
intermediate in 16 bits, and then do the horizontal pass in 16-bit arithmetic. 
The horizontal sum can be negative or above 32767, so it is computed modulo 
2^16 with a bias of 72 * 128 that keeps it inside [0, 65535] for every mode, 
rounding value and input (the range is [-9180, 41820] before the bias). A 
logical shift and a subtraction of 72 then give the exact signed result. The 
comment in the code explains this.
- 16x16 is not four 8x8 calls: the one-dimensional modes run over all 16 
columns in one pass, and the two-dimensional ones run two 8-column passes 
inside one function, so each function is entered once per block.

The assembly was written with AI assistance, in the style of the existing 
aarch64 code, and tested as described below.

### Why

These changes come out of testing the VC-1 frame-threading PR 
([#26273](https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26273)) on a low-power 
ARM TV. Even with 3 or 4 frame threads that TV can't decode some Blu-ray VC-1 
content in real time (1080i interlaced reached about 0.83-0.91x real time with 
4 threads). Threading alone is not enough when a single core can't keep up, so 
the per-thread cost of the decoder has to come down too. This change doesn't 
depend on #26273 and applies to master on its own; all numbers below were 
measured on master. The test builds (both master and patched) also had the VC-1 
parser fix from the separate parser PR applied, because without it some of the 
interlaced clips don't decode correctly on master; it doesn't touch motion 
compensation.

The single-thread profiling that found this was done on an Apple M3 Max, and 
the change was then tested on real hardware.

The original effort targeted 32-bit Android on a Cortex-A53 TV (Sony X940E). 
That TV can't run aarch64 code, so there are no numbers from it for this 
change, and arm32 already has mspel NEON. While working on it, though, the same 
interlaced content turned out not to keep up on a single core on an NVIDIA 
Shield TV (2019, Tegra X1+, 4x Cortex-A57 @ 2.0 GHz, 2 MB L2, Android 11, 
arm64-v8a) either: at one thread, master decodes the two 1080i clips tested 
there (Diana, PlanetEarth) at about 0.81x and 0.82x real time. Improving 
single-thread performance on aarch64 therefore seems worthwhile too, and the 
Shield is where this patch was measured on real hardware.

### checkasm

`checkasm --test=vc1dsp --bench`, C against NEON, one run. The Shield 
(Cortex-A57, NDK clang, arm64-v8a) timings are in nsecs per call; the M3 Max 
uses the `cntvct` timer, so its numbers are in arbitrary units and only the 
ratio means anything. 8x8 / 16x16 in each column.

| mode | Shield put | Shield avg | M3 Max put | M3 Max avg |
|---|---|---|---|---|
| mc00 | - | 2.4x / 2.6x | - | 1.4x / 2.9x |
| mc01 | 7.8x / 4.3x | 8.9x / 3.7x | 14.3x / 6.0x | 15.8x / 5.7x |
| mc02 | 9.1x / 9.8x | 10.1x / 3.2x | 13.1x / 11.1x | 13.6x / 3.9x |
| mc03 | 8.7x / 4.3x | 10.3x / 4.0x | 15.6x / 6.4x | 14.6x / 5.8x |
| mc10 | 6.5x / 3.3x | 8.0x / 3.1x | 9.7x / 5.2x | 11.3x / 4.9x |
| mc20 | 6.2x / 6.7x | 6.8x / 2.5x | 7.7x / 9.3x | 9.0x / 3.7x |
| mc30 | 5.8x / 3.1x | 7.5x / 2.9x | 9.3x / 5.2x | 10.9x / 4.9x |
| mc11 | 1.6x / 1.5x | 2.3x / 1.5x | 3.0x / 2.2x | 3.2x / 2.1x |
| mc12 | 1.5x / 1.3x | 2.1x / 1.2x | 2.5x / 1.9x | 2.6x / 1.8x |
| mc13 | 1.6x / 1.6x | 2.3x / 1.5x | 2.9x / 2.4x | 2.8x / 2.2x |
| mc21 | 1.5x / 1.5x | 2.2x / 1.5x | 2.8x / 2.1x | 3.3x / 2.0x |
| mc22 | 1.4x / 1.2x | 2.1x / 1.2x | 2.4x / 1.7x | 2.6x / 1.7x |
| mc23 | 1.5x / 1.5x | 2.2x / 1.5x | 2.9x / 2.2x | 3.0x / 2.1x |
| mc31 | 1.6x / 1.6x | 2.3x / 1.5x | 2.9x / 2.3x | 3.0x / 2.1x |
| mc32 | 1.5x / 1.2x | 2.2x / 1.2x | 2.4x / 2.0x | 2.5x / 1.7x |
| mc33 | 1.6x / 1.6x | 2.3x / 1.5x | 2.8x / 2.2x | 2.7x / 2.3x |

(mcXY: X horizontal, Y vertical quarter-pel mode.) The one-dimensional modes 
gain 3x-16x. The two-dimensional modes, which are the majority of the 15, gain 
much less: 1.2x-1.6x on the A57 for put and 1.7x-3.3x on the M3 Max. Looking at 
the disassembly of the arm64 NDK clang build, the AI found that the compiled C 
code for the two-dimensional functions already contains some vector 
instructions (auto-vectorization), while the one-dimensional 8x8 functions are 
scalar; this may be part of why the two-dimensional gain is smaller, but the AI 
did not investigate it further. The AI thinks the two-dimensional functions can 
still be made faster, but it kept this patch to a straightforward version.

### Decode impact

Decode-only (`-f null`, `-threads 1`, whole clips: 2156-2696 frames), VC-1 
Blu-ray clips, master against patched.

Apple M3 Max, CPU cycles and instructions from `/usr/bin/time -l`, best of 3 
interleaved runs. Speedup is base cycles / patched cycles; instructions are 
relative to master. Cycles drift about ±2-4% between sessions on this machine, 
so only the interlaced results are meaningful here.

| clip | speedup | instructions |
|---|---:|---:|
| progressive 1080p (DarkKnight) | 1.018x | 0.973 |
| progressive 1080p (Contact) | 1.011x | 0.980 |
| progressive 1080p (HP1) | 1.008x | 0.998 |
| progressive 1080p (HumanPlanet) | 1.011x | 1.000 |
| interlaced 1080i (Diana) | 1.131x | 0.770 |
| interlaced 1080i (NIN peak) | 1.156x | 0.794 |
| interlaced 1080i (NIN mid) | 1.183x | 0.750 |
| interlaced 1080i (PlanetEarth peak) | 1.095x | 0.832 |
| interlaced 1080i (PlanetEarth mid) | 1.114x | 0.823 |

Interlaced content gains +9.5% to +18%; progressive film moves by 1-2%, which 
is within the session noise on this machine (the instruction count does drop by 
2-3% on two of the clips).

NVIDIA Shield TV (Cortex-A57), median of 3 runs, 3 of the clips. CPU time is 
user+sys. The Shield was left asleep as it is (no settings changed), and its 
CPU clock dipped to 1.1 GHz at some point during the runs, but run-to-run 
spread was under 1.5%.

| clip | master fps | patched fps | fps | CPU time |
|---|---:|---:|---:|---:|
| progressive 1080p (DarkKnight) | 37.24 | 37.58 | +0.9% | -1.1% |
| interlaced 1080i (Diana) | 24.15 | 27.17 | +12.5% | -10.5% |
| interlaced 1080i (PlanetEarth peak) | 20.54 | 22.36 | +8.9% | -7.6% |

Differences below ±1% are noise. The interlaced gains on the Shield are about 
the same as on the Mac (a bit smaller on PlanetEarth), and the progressive clip 
is unchanged. Diana at one thread goes from 0.81x to 0.91x real time on that 
device, which is the kind of change that matters for the case that motivated 
this.

### Testing

- `make fate-vc1 fate-wmv3-drm fate-mss2 fate-checkasm-vc1dsp` with a full 
macOS (aarch64) build: all pass.
- `checkasm --test=vc1dsp` on macOS (M3 Max), seeds 1-10, 777 and 31337, and on 
the Shield (Cortex-A57) with `--repeat=30` on seeds 1-8, 12345 and 99999: all 
pass. The test covers all 15 fractional modes and mc00 for put and avg, 8x8 and 
16x16, both `rnd` values, random strides (including negative ones) larger than 
the block, and random source misalignment.
- To check the arithmetic limits of the 16-bit path, which random data doesn't 
reach, the AI also ran checkasm on macOS with a temporary extra loop feeding 
all-0, all-255 and several alternating 0/255 patterns (columns, rows, 
checkerboard, pairs) as source, for 8 seeds: all pass. That change is not part 
of this patch.
- Full `checkasm` on the Shield: all 8182 tests pass.
- Decoded the 9 benchmark clips on macOS with `-threads 1 -f framemd5`: 
bit-exact against reference decodes from an unpatched decoder, and also with 
`-cpuflags 0` (C code only).
- Decoded the 3 clips available on the Shield (full length) with the arm64 
build of this branch with `-f framemd5`: bit-exact against the same reference.
- Builds for macOS (aarch64), the Android NDK for arm64-v8a, and armv7-a (where 
the new code isn't built).
- `git diff --check` is clean.



From 2a33e998020c75be5c4131660bc658cd1d13066b Mon Sep 17 00:00:00 2001
From: Shaya Potter <[email protected]>
Date: Thu, 26 Nov 2026 18:08:14 +0200
Subject: [PATCH] avcodec/aarch64/vc1dsp: add NEON mspel put/avg MC

aarch64 had no NEON for the VC-1 bicubic motion compensation, so
put_vc1_mspel_pixels_tab and avg_vc1_mspel_pixels_tab used the C
functions. These are used for interlaced pictures and for the
interpolated (bidirectional) prediction of B pictures.

Add NEON versions of the 15 fractional-pel modes for put and avg, 8x8
and 16x16, and of the full-pel (mc00) avg, which is also used for the
interpolated prediction. Full-pel put is a plain copy that the C code
does as fast, so it stays as it is. The put and avg functions are
generated from the same macros, the avg ones loading the destination
and averaging with urhadd before the store, as op_avg does in vc1dsp.c.

The one-dimensional filters accumulate in 16 bits with umlal and umlsl
on the magnitudes of the taps. The two-dimensional modes keep the
intermediate of the vertical pass in 16 bits, and the horizontal pass
wraps around modulo 2^16 using a bias that keeps the sum in the
unsigned range, which is explained in the comment in the code.

16x16 is done in one pass over 16 columns for the one-dimensional modes,
and as two passes over 8 columns for the two-dimensional ones, rather
than as four 8x8 calls.

Tested with checkasm for all modes, put and avg, both block sizes and
both rounding values.
---
 libavcodec/aarch64/Makefile              |   3 +-
 libavcodec/aarch64/vc1dsp_init_aarch64.c |  57 ++++
 libavcodec/aarch64/vc1dsp_mspel_neon.S   | 353 +++++++++++++++++++++++
 3 files changed, 412 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/aarch64/vc1dsp_mspel_neon.S

diff --git a/libavcodec/aarch64/Makefile b/libavcodec/aarch64/Makefile
index e560a8c3129..2383491eb4b 100644
--- a/libavcodec/aarch64/Makefile
+++ b/libavcodec/aarch64/Makefile
@@ -69,7 +69,8 @@ NEON-OBJS-$(CONFIG_PIXBLOCKDSP)         += 
aarch64/pixblockdsp_neon.o
 NEON-OBJS-$(CONFIG_APNG_DECODER)        += aarch64/pngdsp_neon.o
 NEON-OBJS-$(CONFIG_LSCR_DECODER)        += aarch64/pngdsp_neon.o
 NEON-OBJS-$(CONFIG_PNG_DECODER)         += aarch64/pngdsp_neon.o
-NEON-OBJS-$(CONFIG_VC1DSP)              += aarch64/vc1dsp_neon.o
+NEON-OBJS-$(CONFIG_VC1DSP)              += aarch64/vc1dsp_neon.o \
+                                           aarch64/vc1dsp_mspel_neon.o
 NEON-OBJS-$(CONFIG_VP8DSP)              += aarch64/vp8dsp_neon.o
 
 # decoders/encoders
diff --git a/libavcodec/aarch64/vc1dsp_init_aarch64.c 
b/libavcodec/aarch64/vc1dsp_init_aarch64.c
index 3bc0bd17ee5..7b40753407c 100644
--- a/libavcodec/aarch64/vc1dsp_init_aarch64.c
+++ b/libavcodec/aarch64/vc1dsp_init_aarch64.c
@@ -110,6 +110,46 @@ static int vc1_unescape_buffer_neon(const uint8_t *src, 
int size, uint8_t *dst)
     return dsize;
 }
 
+#define DECL_MSPEL(op, X, Y)                                                  \
+void ff_ ## op ## _vc1_mspel_mc ## X ## Y ## _neon(uint8_t *dst,              \
+                                                   const uint8_t *src,        \
+                                                   ptrdiff_t stride, int rnd);\
+void ff_ ## op ## _vc1_mspel_mc ## X ## Y ## _16_neon(uint8_t *dst,           \
+                                                      const uint8_t *src,     \
+                                                      ptrdiff_t stride,       \
+                                                      int rnd)
+
+#define DECL_MSPEL_OPS(X, Y)                                                  \
+    DECL_MSPEL(put, X, Y);                                                    \
+    DECL_MSPEL(avg, X, Y)
+
+DECL_MSPEL(avg, 0, 0);
+DECL_MSPEL_OPS(1, 0);
+DECL_MSPEL_OPS(2, 0);
+DECL_MSPEL_OPS(3, 0);
+DECL_MSPEL_OPS(0, 1);
+DECL_MSPEL_OPS(1, 1);
+DECL_MSPEL_OPS(2, 1);
+DECL_MSPEL_OPS(3, 1);
+DECL_MSPEL_OPS(0, 2);
+DECL_MSPEL_OPS(1, 2);
+DECL_MSPEL_OPS(2, 2);
+DECL_MSPEL_OPS(3, 2);
+DECL_MSPEL_OPS(0, 3);
+DECL_MSPEL_OPS(1, 3);
+DECL_MSPEL_OPS(2, 3);
+DECL_MSPEL_OPS(3, 3);
+
+#define SET_MSPEL(op, X, Y)                                                   \
+    dsp->op ## _vc1_mspel_pixels_tab[1][X + 4 * Y] =                          \
+        ff_ ## op ## _vc1_mspel_mc ## X ## Y ## _neon;                        \
+    dsp->op ## _vc1_mspel_pixels_tab[0][X + 4 * Y] =                          \
+        ff_ ## op ## _vc1_mspel_mc ## X ## Y ## _16_neon
+
+#define SET_MSPEL_OPS(X, Y)                                                   \
+    SET_MSPEL(put, X, Y);                                                     \
+    SET_MSPEL(avg, X, Y)
+
 av_cold void ff_vc1dsp_init_aarch64(VC1DSPContext *dsp)
 {
     int cpu_flags = av_get_cpu_flags();
@@ -137,5 +177,22 @@ av_cold void ff_vc1dsp_init_aarch64(VC1DSPContext *dsp)
         dsp->avg_no_rnd_vc1_chroma_pixels_tab[1] = ff_avg_vc1_chroma_mc4_neon;
 
         dsp->vc1_unescape_buffer = vc1_unescape_buffer_neon;
+
+        SET_MSPEL(avg, 0, 0);
+        SET_MSPEL_OPS(1, 0);
+        SET_MSPEL_OPS(2, 0);
+        SET_MSPEL_OPS(3, 0);
+        SET_MSPEL_OPS(0, 1);
+        SET_MSPEL_OPS(1, 1);
+        SET_MSPEL_OPS(2, 1);
+        SET_MSPEL_OPS(3, 1);
+        SET_MSPEL_OPS(0, 2);
+        SET_MSPEL_OPS(1, 2);
+        SET_MSPEL_OPS(2, 2);
+        SET_MSPEL_OPS(3, 2);
+        SET_MSPEL_OPS(0, 3);
+        SET_MSPEL_OPS(1, 3);
+        SET_MSPEL_OPS(2, 3);
+        SET_MSPEL_OPS(3, 3);
     }
 }
diff --git a/libavcodec/aarch64/vc1dsp_mspel_neon.S 
b/libavcodec/aarch64/vc1dsp_mspel_neon.S
new file mode 100644
index 00000000000..a53a31c9071
--- /dev/null
+++ b/libavcodec/aarch64/vc1dsp_mspel_neon.S
@@ -0,0 +1,353 @@
+/*
+ * VC-1 bicubic (mspel) motion compensation, AArch64 NEON
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/aarch64/asm.S"
+
+// All functions have the prototype
+//   void ff_{put,avg}_vc1_mspel_mc<h><v>[_16]_neon(uint8_t *dst,
+//                                                  const uint8_t *src,
+//                                                  ptrdiff_t stride, int rnd)
+// with x0 = dst, x1 = src, x2 = stride, w3 = rnd. <h> and <v> are the
+// horizontal and vertical quarter-pel modes (0 to 3), and the _16 versions
+// do a 16x16 block instead of 8x8.
+//
+// The filters are { -4, 53, 18, -3 } (mode 1), { -1, 9, 9, -1 } (mode 2) and
+// { -3, 18, 53, -4 } (mode 3). Only their magnitudes are kept in registers,
+// the signs are applied by choosing between umlal and umlsl.
+
+// Load the magnitudes of the filter taps for \mode into v28-v31 as bytes.
+.macro taps8 mode
+.if \mode == 1
+        movi            v28.16b, #4
+        movi            v29.16b, #53
+        movi            v30.16b, #18
+        movi            v31.16b, #3
+.elseif \mode == 2
+        movi            v28.16b, #1
+        movi            v29.16b, #9
+        movi            v30.16b, #9
+        movi            v31.16b, #1
+.else
+        movi            v28.16b, #3
+        movi            v29.16b, #18
+        movi            v30.16b, #53
+        movi            v31.16b, #4
+.endif
+.endm
+
+// The same as 16-bit values in v20-v23.
+.macro taps16 mode
+.if \mode == 1
+        movi            v20.8h, #4
+        movi            v21.8h, #53
+        movi            v22.8h, #18
+        movi            v23.8h, #3
+.elseif \mode == 2
+        movi            v20.8h, #1
+        movi            v21.8h, #9
+        movi            v22.8h, #9
+        movi            v23.8h, #1
+.else
+        movi            v20.8h, #3
+        movi            v21.8h, #18
+        movi            v22.8h, #53
+        movi            v23.8h, #4
+.endif
+.endm
+
+// \acc.8h = v27 - s0 * v28 + s1 * v29 + s2 * v30 - s3 * v31, on the low
+// (filt8) or high (filt8_hi) 8 bytes of the sources
+.macro filt8 acc, s0, s1, s2, s3
+        mov             \acc\().16b, v27.16b
+        umlsl           \acc\().8h, \s0\().8b,  v28.8b
+        umlal           \acc\().8h, \s1\().8b,  v29.8b
+        umlal           \acc\().8h, \s2\().8b,  v30.8b
+        umlsl           \acc\().8h, \s3\().8b,  v31.8b
+.endm
+
+.macro filt8_hi acc, s0, s1, s2, s3
+        mov             \acc\().16b, v27.16b
+        umlsl2          \acc\().8h, \s0\().16b, v28.16b
+        umlal2          \acc\().8h, \s1\().16b, v29.16b
+        umlal2          \acc\().8h, \s2\().16b, v30.16b
+        umlsl2          \acc\().8h, \s3\().16b, v31.16b
+.endm
+
+// Store the result in \r to dst, averaging it with what is there for avg
+.macro store8 op, r
+.ifc \op, avg
+        ld1             {v18.8b}, [x0]
+        urhadd          \r\().8b, \r\().8b, v18.8b
+.endif
+        st1             {\r\().8b}, [x0], x2
+.endm
+
+.macro store16 op, r
+.ifc \op, avg
+        ld1             {v18.16b}, [x0]
+        urhadd          \r\().16b, \r\().16b, v18.16b
+.endif
+        st1             {\r\().16b}, [x0], x2
+.endm
+
+// Full-pel average. Put is a plain copy, which the C version does as fast.
+.macro mspel_mc00 op, size, name
+function ff_\op\()_vc1_mspel_\name\()_neon, export=1
+.if \size == 8
+        mov             w5, #8
+1:      ld1             {v0.8b}, [x1], x2
+        store8          \op, v0
+.else
+        mov             w5, #16
+1:      ld1             {v0.16b}, [x1], x2
+        store16         \op, v0
+.endif
+        subs            w5, w5, #1
+        b.ne            1b
+        ret
+endfunc
+.endm
+
+// Horizontal filter only. The rounding constant is add - rnd, where add is
+// 32 for the quarter-pel modes and 8 for the half-pel one.
+.macro mspel_h op, mode, add, shift
+function ff_\op\()_vc1_mspel_mc\mode\()0_neon, export=1
+        mov             w4, #\add
+        sub             w4, w4, w3
+        dup             v27.8h, w4
+        taps8           \mode
+        sub             x1, x1, #1
+        mov             w5, #8
+1:      ld1             {v0.16b}, [x1], x2
+        ext             v1.16b, v0.16b, v0.16b, #1
+        ext             v2.16b, v0.16b, v0.16b, #2
+        ext             v3.16b, v0.16b, v0.16b, #3
+        filt8           v16, v0, v1, v2, v3
+        sqshrun         v16.8b, v16.8h, #\shift
+        store8          \op, v16
+        subs            w5, w5, #1
+        b.ne            1b
+        ret
+endfunc
+
+function ff_\op\()_vc1_mspel_mc\mode\()0_16_neon, export=1
+        mov             w4, #\add
+        sub             w4, w4, w3
+        dup             v27.8h, w4
+        taps8           \mode
+        sub             x1, x1, #1
+        mov             w5, #16
+1:      ldr             s1, [x1, #16]
+        ld1             {v0.16b}, [x1], x2
+        ext             v2.16b, v0.16b, v1.16b, #1
+        ext             v3.16b, v0.16b, v1.16b, #2
+        ext             v4.16b, v0.16b, v1.16b, #3
+        filt8           v16, v0, v2, v3, v4
+        filt8_hi        v17, v0, v2, v3, v4
+        sqshrun         v16.8b, v16.8h, #\shift
+        sqshrun2        v16.16b, v17.8h, #\shift
+        store16         \op, v16
+        subs            w5, w5, #1
+        b.ne            1b
+        ret
+endfunc
+.endm
+
+// One output row of the vertical filter: load the next source row into \ld
+// and filter it together with the three before it, \s0-\s2.
+.macro vrow8 op, shift, ld, s0, s1, s2
+        ld1             {\ld\().8b}, [x1], x2
+        filt8           v16, \s0, \s1, \s2, \ld
+        sqshrun         v16.8b, v16.8h, #\shift
+        store8          \op, v16
+.endm
+
+.macro vrow16 op, shift, ld, s0, s1, s2
+        ld1             {\ld\().16b}, [x1], x2
+        filt8           v16, \s0, \s1, \s2, \ld
+        filt8_hi        v17, \s0, \s1, \s2, \ld
+        sqshrun         v16.8b, v16.8h, #\shift
+        sqshrun2        v16.16b, v17.8h, #\shift
+        store16         \op, v16
+.endm
+
+// Vertical filter only. The rounding constant is add - 1 + rnd. The four
+// rows the filter needs rotate through v0-v3, so every output row costs one
+// load and no register moves; four rows are done per iteration.
+.macro mspel_v op, mode, add, shift
+function ff_\op\()_vc1_mspel_mc0\mode\()_neon, export=1
+        mov             w4, #(\add - 1)
+        add             w4, w4, w3
+        dup             v27.8h, w4
+        taps8           \mode
+        sub             x1, x1, x2
+        ld1             {v0.8b}, [x1], x2
+        ld1             {v1.8b}, [x1], x2
+        ld1             {v2.8b}, [x1], x2
+        mov             w5, #8
+1:
+        vrow8           \op, \shift, v3, v0, v1, v2
+        vrow8           \op, \shift, v0, v1, v2, v3
+        vrow8           \op, \shift, v1, v2, v3, v0
+        vrow8           \op, \shift, v2, v3, v0, v1
+        subs            w5, w5, #4
+        b.ne            1b
+        ret
+endfunc
+
+function ff_\op\()_vc1_mspel_mc0\mode\()_16_neon, export=1
+        mov             w4, #(\add - 1)
+        add             w4, w4, w3
+        dup             v27.8h, w4
+        taps8           \mode
+        sub             x1, x1, x2
+        ld1             {v0.16b}, [x1], x2
+        ld1             {v1.16b}, [x1], x2
+        ld1             {v2.16b}, [x1], x2
+        mov             w5, #16
+1:
+        vrow16          \op, \shift, v3, v0, v1, v2
+        vrow16          \op, \shift, v0, v1, v2, v3
+        vrow16          \op, \shift, v1, v2, v3, v0
+        vrow16          \op, \shift, v2, v3, v0, v1
+        subs            w5, w5, #4
+        b.ne            1b
+        ret
+endfunc
+.endm
+
+// Vertical filter followed by horizontal filter. The vertical pass is shifted
+// right by \shift to give a 16-bit intermediate, and the horizontal pass then
+// uses 16-bit arithmetic with a final shift by 7.
+//
+// The result of the horizontal pass lies in [-9180, 41820] for every mode,
+// rounding and input. Adding a bias of 72 * 128 keeps it in [0, 65535], so
+// the 16-bit wraparound does not matter and a logical shift followed by
+// subtracting 72 gives the correct, signed, value, which sqxtun then clips.
+//
+// A column of 8 pixels is done per pass, with the rows of the vertical filter
+// sliding through v0-v3. On entry to hv_column x1 points at the row above the
+// first one, one pixel to the left, and w5 is the number of rows.
+//   v27     rounding constant of the vertical pass: (1 << (shift - 1)) - 1 + 
rnd
+//   v28-v31 vertical taps, as bytes
+//   v19     rounding constant of the horizontal pass: 64 - rnd, plus the bias
+//   v20-v23 horizontal taps, as 16-bit values
+//   v26     the value to subtract again after the shift
+.macro hv_column op
+        ld1             {v0.16b}, [x1], x2
+        ld1             {v1.16b}, [x1], x2
+        ld1             {v2.16b}, [x1], x2
+1:      ld1             {v3.16b}, [x1], x2
+        filt8           v16, v0, v1, v2, v3
+        filt8_hi        v17, v0, v1, v2, v3
+        sshr            v16.8h, v16.8h, #hv_shift       // tmp[-1..6]
+        sshr            v17.8h, v17.8h, #hv_shift       // tmp[7..14]
+        ext             v4.16b, v16.16b, v17.16b, #2    // tmp[0..7]
+        ext             v5.16b, v16.16b, v17.16b, #4    // tmp[1..8]
+        ext             v6.16b, v16.16b, v17.16b, #6    // tmp[2..9]
+        mov             v24.16b, v19.16b
+        mul             v25.8h, v5.8h, v22.8h
+        mls             v24.8h, v16.8h, v20.8h
+        mls             v25.8h, v6.8h, v23.8h
+        mla             v24.8h, v4.8h, v21.8h
+        add             v24.8h, v24.8h, v25.8h
+        ushr            v24.8h, v24.8h, #7
+        sub             v24.8h, v24.8h, v26.8h
+        sqxtun          v24.8b, v24.8h
+        store8          \op, v24
+        mov             v0.16b, v1.16b
+        mov             v1.16b, v2.16b
+        mov             v2.16b, v3.16b
+        subs            w5, w5, #1
+        b.ne            1b
+.endm
+
+.macro hv_setup hmode, vmode
+        mov             w4, #((1 << (hv_shift - 1)) - 1)
+        add             w4, w4, w3
+        dup             v27.8h, w4
+        mov             w4, #(64 + 72 * 128)
+        sub             w4, w4, w3
+        dup             v19.8h, w4
+        movi            v26.8h, #72
+        taps8           \vmode
+        taps16          \hmode
+        sub             x1, x1, x2
+        sub             x1, x1, #1
+.endm
+
+.macro mspel_hv op, hmode, vmode
+// The shifts of the one-dimensional filters are 5 for the quarter-pel modes
+// and 1 for the half-pel one, and the shift of the first pass is their mean.
+.if \hmode == 2
+        .set hv_shift_h, 1
+.else
+        .set hv_shift_h, 5
+.endif
+.if \vmode == 2
+        .set hv_shift_v, 1
+.else
+        .set hv_shift_v, 5
+.endif
+        .set hv_shift, (hv_shift_h + hv_shift_v) >> 1
+
+function ff_\op\()_vc1_mspel_mc\hmode\vmode\()_neon, export=1
+        hv_setup        \hmode, \vmode
+        mov             w5, #8
+        hv_column       \op
+        ret
+endfunc
+
+function ff_\op\()_vc1_mspel_mc\hmode\vmode\()_16_neon, export=1
+        hv_setup        \hmode, \vmode
+        mov             x6, x0
+        mov             x7, x1
+        mov             w5, #16
+        hv_column       \op
+        add             x0, x6, #8
+        add             x1, x7, #8
+        mov             w5, #16
+        hv_column       \op
+        ret
+endfunc
+.endm
+
+.macro mspel_all op
+mspel_h         \op, 1, 32, 6
+mspel_h         \op, 2, 8,  4
+mspel_h         \op, 3, 32, 6
+mspel_v         \op, 1, 32, 6
+mspel_v         \op, 2, 8,  4
+mspel_v         \op, 3, 32, 6
+mspel_hv        \op, 1, 1
+mspel_hv        \op, 1, 2
+mspel_hv        \op, 1, 3
+mspel_hv        \op, 2, 1
+mspel_hv        \op, 2, 2
+mspel_hv        \op, 2, 3
+mspel_hv        \op, 3, 1
+mspel_hv        \op, 3, 2
+mspel_hv        \op, 3, 3
+.endm
+
+mspel_all       put
+mspel_all       avg
+mspel_mc00      avg, 8,  mc00
+mspel_mc00      avg, 16, mc00_16
-- 
2.54.1

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

Reply via email to