[FFmpeg-devel] [PATCH v3 3/3] swscale/aarch64: Add argb/abgr to yuv

Zhao Zhili quinkblack at foxmail.com
Mon Jun 24 14:37:01 EEST 2024


From: Zhao Zhili <zhilizhao at tencent.com>

Test on Apple M1 with kperf:
				: -O3		: -O3 -fno-vectorize
abgr_to_uv_8_c			: 19.4		: 26.1
abgr_to_uv_8_neon		: 29.9		: 51.1
abgr_to_uv_128_c		: 146.4		: 558.9
abgr_to_uv_128_neon		: 85.1		: 83.4
abgr_to_uv_1080_c		: 1162.6	: 4786.4
abgr_to_uv_1080_neon		: 819.6		: 826.6
abgr_to_uv_1920_c		: 2063.6	: 8492.1
abgr_to_uv_1920_neon		: 1435.1	: 1447.1
abgr_to_uv_half_8_c		: 16.4		: 11.4
abgr_to_uv_half_8_neon		: 35.6		: 20.4
abgr_to_uv_half_128_c		: 108.6		: 359.4
abgr_to_uv_half_128_neon	: 75.4		: 42.6
abgr_to_uv_half_1080_c		: 883.4		: 2885.6
abgr_to_uv_half_1080_neon	: 460.6		: 481.1
abgr_to_uv_half_1920_c		: 1553.6	: 5106.9
abgr_to_uv_half_1920_neon	: 817.6		: 820.4
abgr_to_y_8_c			: 6.1		: 26.4
abgr_to_y_8_neon		: 40.6		: 6.4
abgr_to_y_128_c			: 99.9		: 390.1
abgr_to_y_128_neon		: 67.4		: 55.9
abgr_to_y_1080_c		: 735.9		: 3170.4
abgr_to_y_1080_neon		: 534.6		: 536.6
abgr_to_y_1920_c		: 1279.4	: 6016.4
abgr_to_y_1920_neon		: 932.6		: 927.6
---
 libswscale/aarch64/input.S   | 114 ++++++++++++++++++++++++++++-------
 libswscale/aarch64/swscale.c |  17 ++++++
 2 files changed, 110 insertions(+), 21 deletions(-)

diff --git a/libswscale/aarch64/input.S b/libswscale/aarch64/input.S
index 6d2c6034bb..f4d587fed0 100644
--- a/libswscale/aarch64/input.S
+++ b/libswscale/aarch64/input.S
@@ -34,6 +34,16 @@
         uxtl2           v24.8h, v18.16b            // v24: b
 .endm
 
+.macro argb_to_yuv_load_rgb src
+        ld4             { v16.16b, v17.16b, v18.16b, v19.16b }, [\src]
+        uxtl            v21.8h, v19.8b             // v21: b
+        uxtl2           v24.8h, v19.16b            // v24: b
+        uxtl            v19.8h, v17.8b             // v19: r
+        uxtl            v20.8h, v18.8b             // v20: g
+        uxtl2           v22.8h, v17.16b            // v22: r
+        uxtl2           v23.8h, v18.16b            // v23: g
+.endm
+
 .macro rgb_to_yuv_product r, g, b, dst1, dst2, dst, coef0, coef1, coef2, right_shift
         mov             \dst1\().16b, v6.16b                    // dst1 = const_offset
         mov             \dst2\().16b, v6.16b                    // dst2 = const_offset
@@ -55,7 +65,7 @@ function ff_bgr24ToY_neon, export=1
         ret
 endfunc
 
-.macro rgbToY_neon fmt, element
+.macro rgbToY_neon fmt, element, alpha_first=0
 function ff_\fmt\()ToY_neon, export=1
         cmp             w4, #0                  // check width > 0
         ldp             w10, w11, [x5]          // w10: ry, w11: gy
@@ -72,7 +82,11 @@ function ff_\fmt\()ToY_neon, export=1
         dup             v2.8h, w12
         b.lt            2f
 1:
+    .if \alpha_first
+        argb_to_yuv_load_rgb x1
+    .else
         rgb_to_yuv_load_rgb x1, \element
+    .endif
         rgb_to_yuv_product v19, v20, v21, v25, v26, v16, v0, v1, v2, #9
         rgb_to_yuv_product v22, v23, v24, v27, v28, v17, v0, v1, v2, #9
         sub             w4, w4, #16             // width -= 16
@@ -82,9 +96,15 @@ function ff_\fmt\()ToY_neon, export=1
         b.ge            1b
         cbz             x4, 3f
 2:
+    .if \alpha_first
+        ldrb            w13, [x1, #1]           // w13: r
+        ldrb            w14, [x1, #2]           // w14: g
+        ldrb            w15, [x1, #3]           // w15: b
+    .else
         ldrb            w13, [x1]               // w13: r
         ldrb            w14, [x1, #1]           // w14: g
         ldrb            w15, [x1, #2]           // w15: b
+    .endif
 
         smaddl          x13, w13, w10, x9       // x13 = ry * r + const_offset
         smaddl          x13, w14, w11, x13      // x13 += gy * g
@@ -101,6 +121,16 @@ endfunc
 
 rgbToY_neon fmt=rgb24, element=3
 
+function ff_abgr32ToY_neon, export=1
+        cmp             w4, #0                  // check width > 0
+        ldp             w12, w11, [x5]          // w12: ry, w11: gy
+        ldr             w10, [x5, #8]           // w10: by
+        b.gt            4f
+        ret
+endfunc
+
+rgbToY_neon fmt=argb32, element=4, alpha_first=1
+
 function ff_bgra32ToY_neon, export=1
         cmp             w4, #0                  // check width > 0
         ldp             w12, w11, [x5]          // w12: ry, w11: gy
@@ -138,7 +168,21 @@ function ff_bgr24ToUV_half_neon, export=1
         b               4f
 endfunc
 
-.macro rgbToUV_half_neon fmt, element
+.macro rgb_load_add_half off_r1, off_r2, off_g1, off_g2, off_b1, off_b2
+        ldrb            w2, [x3, #\off_r1]     // w2: r1
+        ldrb            w4, [x3, #\off_r2]     // w4: r2
+        add             w2, w2, w4             // w2 = r1 + r2
+
+        ldrb            w4, [x3, #\off_g1]     // w4: g1
+        ldrb            w7, [x3, #\off_g2]     // w7: g2
+        add             w4, w4, w7             // w4 = g1 + g2
+
+        ldrb            w7, [x3, #\off_b1]     // w7: b1
+        ldrb            w8, [x3, #\off_b2]     // w8: b2
+        add             w7, w7, w8             // w7 = b1 + b2
+.endm
+
+.macro rgbToUV_half_neon fmt, element, alpha_first=0
 function ff_\fmt\()ToUV_half_neon, export=1
         cmp             w5, #0          // check width > 0
         b.le            3f
@@ -156,9 +200,15 @@ function ff_\fmt\()ToUV_half_neon, export=1
     .else
         ld4             { v16.16b, v17.16b, v18.16b, v19.16b }, [x3]
     .endif
+    .if \alpha_first
+        uaddlp          v21.8h, v19.16b
+        uaddlp          v20.8h, v18.16b
+        uaddlp          v19.8h, v17.16b
+    .else
         uaddlp          v19.8h, v16.16b         // v19: r
         uaddlp          v20.8h, v17.16b         // v20: g
         uaddlp          v21.8h, v18.16b         // v21: b
+    .endif
 
         rgb_to_yuv_product v19, v20, v21, v22, v23, v16, v0, v1, v2, #10
         rgb_to_yuv_product v19, v20, v21, v24, v25, v17, v3, v4, v5, #10
@@ -170,27 +220,15 @@ function ff_\fmt\()ToUV_half_neon, export=1
         b.ge            1b
         cbz             w5, 3f
 2:
-        ldrb            w2, [x3]                // w2: r1
-        ldrb            w4, [x3, #\element]     // w4: r2
-        add             w2, w2, w4              // w2 = r1 + r2
-
+.if \alpha_first
+        rgb_load_add_half 1, 5, 2, 6, 3, 7
+.else
     .if \element == 3
-        ldrb            w4, [x3, #1]            // w4: g1
-        ldrb            w7, [x3, #4]            // w7: g2
-        add             w4, w4, w7              // w4 = g1 + g2
-
-        ldrb            w7, [x3, #2]            // w7: b1
-        ldrb            w8, [x3, #5]            // w8: b2
-        add             w7, w7, w8              // w7 = b1 + b2
+        rgb_load_add_half 0, 3, 1, 4, 2, 5
     .else
-        ldrb            w4, [x3, #1]            // w4: g1
-        ldrb            w7, [x3, #5]            // w7: g2
-        add             w4, w4, w7              // w4 = g1 + g2
-
-        ldrb            w7, [x3, #2]            // w7: b1
-        ldrb            w8, [x3, #6]            // w8: b2
-        add             w7, w7, w8              // w7 = b1 + b2
+        rgb_load_add_half 0, 4, 1, 5, 2, 6
     .endif
+.endif
 
         smaddl          x8, w2, w10, x9         // dst_u = ru * r + const_offset
         smaddl          x8, w4, w11, x8         // dst_u += gu * g
@@ -214,6 +252,18 @@ endfunc
 
 rgbToUV_half_neon fmt=rgb24, element=3
 
+function ff_abgr32ToUV_half_neon, export=1
+        cmp             w5, #0          // check width > 0
+        b.le            3f
+
+        ldp             w12, w11, [x6, #12]
+        ldp             w10, w15, [x6, #20]
+        ldp             w14, w13, [x6, #28]
+        b               4f
+endfunc
+
+rgbToUV_half_neon fmt=argb32, element=4, alpha_first=1
+
 function ff_bgra32ToUV_half_neon, export=1
         cmp             w5, #0          // check width > 0
         b.le            3f
@@ -236,7 +286,7 @@ function ff_bgr24ToUV_neon, export=1
         b               4f
 endfunc
 
-.macro rgbToUV_neon fmt, element
+.macro rgbToUV_neon fmt, element, alpha_first=0
 function ff_\fmt\()ToUV_neon, export=1
         cmp             w5, #0                  // check width > 0
         b.le            3f
@@ -249,7 +299,11 @@ function ff_\fmt\()ToUV_neon, export=1
         rgb_set_uv_coeff half=0
         b.lt            2f
 1:
+    .if \alpha_first
+        argb_to_yuv_load_rgb x3
+    .else
         rgb_to_yuv_load_rgb x3, \element
+    .endif
         rgb_to_yuv_product v19, v20, v21, v25, v26, v16, v0, v1, v2, #9
         rgb_to_yuv_product v22, v23, v24, v27, v28, v17, v0, v1, v2, #9
         rgb_to_yuv_product v19, v20, v21, v25, v26, v18, v3, v4, v5, #9
@@ -262,9 +316,15 @@ function ff_\fmt\()ToUV_neon, export=1
         b.ge            1b
         cbz             w5, 3f
 2:
+    .if \alpha_first
+        ldrb            w16, [x3, #1]           // w16: r
+        ldrb            w17, [x3, #2]           // w17: g
+        ldrb            w4, [x3, #3]            // w4: b
+    .else
         ldrb            w16, [x3]               // w16: r
         ldrb            w17, [x3, #1]           // w17: g
         ldrb            w4, [x3, #2]            // w4: b
+    .endif
 
         smaddl          x8, w16, w10, x9        // x8 = ru * r + const_offset
         smaddl          x8, w17, w11, x8        // x8 += gu * g
@@ -287,6 +347,18 @@ endfunc
 
 rgbToUV_neon fmt=rgb24, element=3
 
+function ff_abgr32ToUV_neon, export=1
+        cmp             w5, #0                  // check width > 0
+        b.le            3f
+
+        ldp             w12, w11, [x6, #12]
+        ldp             w10, w15, [x6, #20]
+        ldp             w14, w13, [x6, #28]
+        b               4f
+endfunc
+
+rgbToUV_neon fmt=argb32, element=4, alpha_first=1
+
 function ff_bgra32ToUV_neon, export=1
         cmp             w5, #0                  // check width > 0
         b.le            3f
diff --git a/libswscale/aarch64/swscale.c b/libswscale/aarch64/swscale.c
index 92af662014..eb907284e7 100644
--- a/libswscale/aarch64/swscale.c
+++ b/libswscale/aarch64/swscale.c
@@ -211,6 +211,8 @@ void ff_##name##ToUV_half_neon(uint8_t *, uint8_t *, const uint8_t *, \
                               const uint8_t *, const uint8_t *, int w, \
                               uint32_t *coeffs, void *)
 
+NEON_INPUT(abgr32);
+NEON_INPUT(argb32);
 NEON_INPUT(bgr24);
 NEON_INPUT(bgra32);
 NEON_INPUT(rgb24);
@@ -248,6 +250,21 @@ av_cold void ff_sws_init_swscale_aarch64(SwsContext *c)
             c->yuv2planeX = ff_yuv2planeX_8_neon;
         }
         switch (c->srcFormat) {
+        case AV_PIX_FMT_ABGR:
+            c->lumToYV12 = ff_abgr32ToY_neon;
+            if (c->chrSrcHSubSample)
+                c->chrToYV12 = ff_abgr32ToUV_half_neon;
+            else
+                c->chrToYV12 = ff_abgr32ToUV_neon;
+            break;
+
+        case AV_PIX_FMT_ARGB:
+            c->lumToYV12 = ff_argb32ToY_neon;
+            if (c->chrSrcHSubSample)
+                c->chrToYV12 = ff_argb32ToUV_half_neon;
+            else
+                c->chrToYV12 = ff_argb32ToUV_neon;
+            break;
         case AV_PIX_FMT_BGR24:
             c->lumToYV12 = ff_bgr24ToY_neon;
             if (c->chrSrcHSubSample)
-- 
2.42.0



More information about the ffmpeg-devel mailing list