[FFmpeg-devel] [PATCH] sws/aarch64: add ff_hscale_8_to_15_neon

Clément Bœsch u at pkh.me
Wed Mar 23 16:25:46 CET 2016


From: Clément Bœsch <clement at stupeflix.com>

./ffmpeg -nostats -f lavfi -i testsrc2=4k:d=2 -vf bench=start,scale=1024x1024,bench=stop -f null -

    before: t:0.489726 avg:0.489883 max:0.491852 min:0.489482
    after:  t:0.259438 avg:0.257707 max:0.260125 min:0.255893
---
I don't really like the roundtrip back to the general purpose register before
writing, but well...
---
 libswscale/aarch64/Makefile   |  6 +++--
 libswscale/aarch64/hscale.S   | 60 +++++++++++++++++++++++++++++++++++++++++++
 libswscale/aarch64/swscale.c  | 37 ++++++++++++++++++++++++++
 libswscale/swscale.c          |  2 ++
 libswscale/swscale_internal.h |  1 +
 libswscale/utils.c            |  4 ++-
 6 files changed, 107 insertions(+), 3 deletions(-)
 create mode 100644 libswscale/aarch64/hscale.S
 create mode 100644 libswscale/aarch64/swscale.c

diff --git a/libswscale/aarch64/Makefile b/libswscale/aarch64/Makefile
index 823806e..51bff08 100644
--- a/libswscale/aarch64/Makefile
+++ b/libswscale/aarch64/Makefile
@@ -1,3 +1,5 @@
-OBJS        += aarch64/swscale_unscaled.o
+OBJS        += aarch64/swscale.o                \
+               aarch64/swscale_unscaled.o       \
 
-NEON-OBJS   += aarch64/yuv2rgb_neon.o
+NEON-OBJS   += aarch64/hscale.o                 \
+               aarch64/yuv2rgb_neon.o           \
diff --git a/libswscale/aarch64/hscale.S b/libswscale/aarch64/hscale.S
new file mode 100644
index 0000000..89c3fce
--- /dev/null
+++ b/libswscale/aarch64/hscale.S
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2016 Clément Bœsch <clement stupeflix.com>
+ *
+ * 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"
+
+function ff_hscale_8_to_15_neon, export=1
+        add                 x10, x4, w6, UXTW #1        // filter2 = filter + filterSize*2 (x2 because int16)
+1:      ldr                 w8, [x5], #4                // filterPos[0]
+        ldr                 w9, [x5], #4                // filterPos[1]
+        movi                v4.4S, #0                   // val sum part 1 (for dst[0])
+        movi                v5.4S, #0                   // val sum part 2 (for dst[1])
+        mov                 w7, w6                      // filterSize counter
+        mov                 x13, x3                     // srcp = src
+2:      add                 x11, x13, w8, UXTW          // srcp + filterPos[0]
+        add                 x12, x13, w9, UXTW          // srcp + filterPos[1]
+        ld1                 {v0.8B}, [x11]              // srcp[filterPos[0] + {0..7}]
+        ld1                 {v1.8B}, [x12]              // srcp[filterPos[1] + {0..7}]
+        uxtl                v0.8H, v0.8B                // unpack part 1 to 16-bit
+        uxtl                v1.8H, v1.8B                // unpack part 2 to 16-bit
+        ld1                 {v2.8H}, [x4],  #16         // load 8x16-bit filter values, part 1
+        ld1                 {v3.8H}, [x10], #16         // ditto at filter+filterSize for part 2
+        smull               v6.4S, v0.4H, v2.4H         // v6.i32{0..3} = part 1 of: srcp[filterPos[0] + {0..7}] * filter[{0..7}]
+        smull               v8.4S, v1.4H, v3.4H         // v8.i32{0..3} = part 1 of: srcp[filterPos[1] + {0..7}] * filter[{0..7}]
+        smull2              v7.4S, v0.8H, v2.8H         // v7.i32{0..3} = part 2 of: srcp[filterPos[0] + {0..7}] * filter[{0..7}]
+        smull2              v9.4S, v1.8H, v3.8H         // v9.i32{0..3} = part 2 of: srcp[filterPos[1] + {0..7}] * filter[{0..7}]
+        addp                v6.4S, v6.4S, v7.4S         // horizontal pair adding of the 8x32-bit multiplied values for part 1 into 4x32-bit
+        addp                v8.4S, v8.4S, v9.4S         // horizontal pair adding of the 8x32-bit multiplied values for part 2 into 4x32-bit
+        add                 v4.4S, v4.4S, v6.4S         // update val accumulator for part 1
+        add                 v5.4S, v5.4S, v8.4S         // update val accumulator for part 2
+        add                 x13, x13, #8                // srcp += 8
+        subs                w7, w7, #8                  // processed 8/filterSize
+        b.gt                2b                          // inner loop if filterSize not consumed completely
+        mov                 x4, x10                     // filter = filter2
+        add                 x10, x10, w6, UXTW #1       // filter2 += filterSize*2
+        addp                v4.4S, v4.4S, v5.4S         // horizontal pair adding of the 8x32-bit sums into 4x32-bit
+        addp                v4.4S, v4.4S, v4.4S         // horizontal pair adding of the 4x32-bit sums into 2x32-bit
+        sqrshrun            v4.4H, v4.4S, #7            // shift and clip the 2x16-bit final values
+        umov                w14, v4.S[0]                // get the values into a general purpose register
+        str                 w14, [x1], #4               // write to destination
+        subs                w2, w2, #2                  // dstW -= 2
+        b.gt                1b                          // loop until end of line
+        ret
+endfunc
diff --git a/libswscale/aarch64/swscale.c b/libswscale/aarch64/swscale.c
new file mode 100644
index 0000000..6b9b046
--- /dev/null
+++ b/libswscale/aarch64/swscale.c
@@ -0,0 +1,37 @@
+/*
+ * 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 "config.h"
+#include "libswscale/swscale.h"
+#include "libswscale/swscale_internal.h"
+#include "libavutil/aarch64/cpu.h"
+
+void ff_hscale_8_to_15_neon(SwsContext *c, int16_t *dst, int dstW,
+                            const uint8_t *src, const int16_t *filter,
+                            const int32_t *filterPos, int filterSize);
+
+av_cold void ff_sws_init_swscale_aarch64(SwsContext *c)
+{
+    int cpu_flags = av_get_cpu_flags();
+
+    if (have_neon(cpu_flags)) {
+        if (c->srcBpc == 8 && c->dstBpc <= 14) {
+            c->hyScale = c->hcScale = ff_hscale_8_to_15_neon;
+        }
+    }
+}
diff --git a/libswscale/swscale.c b/libswscale/swscale.c
index 8d20577..2de5ad4 100644
--- a/libswscale/swscale.c
+++ b/libswscale/swscale.c
@@ -889,6 +889,8 @@ SwsFunc ff_getSwsFunc(SwsContext *c)
         ff_sws_init_swscale_ppc(c);
     if (ARCH_X86)
         ff_sws_init_swscale_x86(c);
+    if (ARCH_AARCH64)
+        ff_sws_init_swscale_aarch64(c);
 
     return swscale;
 }
diff --git a/libswscale/swscale_internal.h b/libswscale/swscale_internal.h
index f0bab78..99b5e7d 100644
--- a/libswscale/swscale_internal.h
+++ b/libswscale/swscale_internal.h
@@ -896,6 +896,7 @@ void ff_sws_init_output_funcs(SwsContext *c,
                               yuv2anyX_fn *yuv2anyX);
 void ff_sws_init_swscale_ppc(SwsContext *c);
 void ff_sws_init_swscale_x86(SwsContext *c);
+void ff_sws_init_swscale_aarch64(SwsContext *c);
 
 void ff_hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
                        const uint8_t *src, int srcW, int xInc);
diff --git a/libswscale/utils.c b/libswscale/utils.c
index ba409d6..24ddd13 100644
--- a/libswscale/utils.c
+++ b/libswscale/utils.c
@@ -49,6 +49,7 @@
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/aarch64/cpu.h"
 #include "libavutil/ppc/cpu.h"
 #include "libavutil/x86/asm.h"
 #include "libavutil/x86/cpu.h"
@@ -1614,7 +1615,8 @@ av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
 #endif /* HAVE_MMXEXT_INLINE */
         {
             const int filterAlign = X86_MMX(cpu_flags)     ? 4 :
-                                    PPC_ALTIVEC(cpu_flags) ? 8 : 1;
+                                    PPC_ALTIVEC(cpu_flags) ? 8 :
+                                    have_neon(cpu_flags)   ? 4 : 1;
 
             if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
                            &c->hLumFilterSize, c->lumXInc,
-- 
2.7.4



More information about the ffmpeg-devel mailing list