[FFmpeg-devel] [PATCH 1/2] swscale/aarch64: Add bgr24 to yuv

Zhao Zhili quinkblack at foxmail.com
Sat Jun 15 12:57:17 EEST 2024


From: Zhao Zhili <zhilizhao at tencent.com>

Test on Apple M1 with kperf

bgr24_to_uv_8_c: 41.5
bgr24_to_uv_8_neon: 41.8
bgr24_to_uv_128_c: 133.5
bgr24_to_uv_128_neon: 94.3
bgr24_to_uv_1080_c: 960.5
bgr24_to_uv_1080_neon: 751.0
bgr24_to_uv_1920_c: 1695.3
bgr24_to_uv_1920_neon: 1357.3
bgr24_to_uv_half_8_c: 45.0
bgr24_to_uv_half_8_neon: 11.0
bgr24_to_uv_half_128_c: 130.5
bgr24_to_uv_half_128_neon: 51.8
bgr24_to_uv_half_1080_c: 877.3
bgr24_to_uv_half_1080_neon: 414.0
bgr24_to_uv_half_1920_c: 1540.0
bgr24_to_uv_half_1920_neon: 695.0
bgr24_to_y_8_c: 24.3
bgr24_to_y_8_neon: 12.8
bgr24_to_y_128_c: 94.3
bgr24_to_y_128_neon: 47.5
bgr24_to_y_1080_c: 611.5
bgr24_to_y_1080_neon: 437.5
bgr24_to_y_1920_c: 1077.3
bgr24_to_y_1920_neon: 765.3
---
 libswscale/aarch64/input.S   | 79 ++++++++++++++++++++++++++++--------
 libswscale/aarch64/swscale.c | 32 +++++++++------
 2 files changed, 80 insertions(+), 31 deletions(-)

diff --git a/libswscale/aarch64/input.S b/libswscale/aarch64/input.S
index 33afa34111..2b956fe5c2 100644
--- a/libswscale/aarch64/input.S
+++ b/libswscale/aarch64/input.S
@@ -20,7 +20,7 @@
 
 #include "libavutil/aarch64/asm.S"
 
-.macro rgb24_to_yuv_load_rgb, src
+.macro rgb_to_yuv_load_rgb src
         ld3             { v16.16b, v17.16b, v18.16b }, [\src]
         uxtl            v19.8h, v16.8b             // v19: r
         uxtl            v20.8h, v17.8b             // v20: g
@@ -30,7 +30,7 @@
         uxtl2           v24.8h, v18.16b            // v24: b
 .endm
 
-.macro rgb24_to_yuv_product, r, g, b, dst1, dst2, dst, coef0, coef1, coef2, right_shift
+.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
         smlal           \dst1\().4s, \coef0\().4h, \r\().4h     // dst1 += rx * r
@@ -43,12 +43,20 @@
         sqshrn2         \dst\().8h, \dst2\().4s, \right_shift   // dst_higher_half = dst2 >> right_shift
 .endm
 
-function ff_rgb24ToY_neon, export=1
+.macro rgbToY bgr
         cmp             w4, #0                  // check width > 0
+    .if \bgr
+        ldr             w12, [x5]               // w12: ry
+        ldr             w11, [x5, #4]           // w11: gy
+        ldr             w10, [x5, #8]           // w10: by
+    .else
         ldp             w10, w11, [x5]          // w10: ry, w11: gy
         ldr             w12, [x5, #8]           // w12: by
+    .endif
         b.le            3f
 
+        // The following comments assume RGB order. The logic for RGB and BGR is the same.
+
         mov             w9, #256                // w9 = 1 << (RGB2YUV_SHIFT - 7)
         movk            w9, #8, lsl #16         // w9 += 32 << (RGB2YUV_SHIFT - 1)
         dup             v6.4s, w9               // w9: const_offset
@@ -59,9 +67,9 @@ function ff_rgb24ToY_neon, export=1
         dup             v2.8h, w12
         b.lt            2f
 1:
-        rgb24_to_yuv_load_rgb x1
-        rgb24_to_yuv_product v19, v20, v21, v25, v26, v16, v0, v1, v2, #9
-        rgb24_to_yuv_product v22, v23, v24, v27, v28, v17, v0, v1, v2, #9
+        rgb_to_yuv_load_rgb x1
+        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
         add             x1, x1, #48             // src += 48
         cmp             w4, #16                 // width >= 16 ?
@@ -83,12 +91,29 @@ function ff_rgb24ToY_neon, export=1
         cbnz            w4, 2b
 3:
         ret
+.endm
+
+function ff_rgb24ToY_neon, export=1
+        rgbToY          bgr=0
+endfunc
+
+function ff_bgr24ToY_neon, export=1
+        rgbToY          bgr=1
 endfunc
 
-.macro rgb24_load_uv_coeff half
+.macro rgb_load_uv_coeff half, bgr
+    .if \bgr
+        ldr             w12, [x6, #12]
+        ldr             w11, [x6, #16]
+        ldr             w10, [x6, #20]
+        ldr             w15, [x6, #24]
+        ldr             w14, [x6, #28]
+        ldr             w13, [x6, #32]
+    .else
         ldp             w10, w11, [x6, #12]     // w10: ru, w11: gu
         ldp             w12, w13, [x6, #20]     // w12: bu, w13: rv
         ldp             w14, w15, [x6, #28]     // w14: gv, w15: bv
+    .endif
     .if \half
         mov             w9, #512
         movk            w9, #128, lsl #16       // w9: const_offset
@@ -105,21 +130,22 @@ endfunc
         dup             v6.4s, w9
 .endm
 
-function ff_rgb24ToUV_half_neon, export=1
+.macro rgbToUV_half bgr
         cmp             w5, #0          // check width > 0
         b.le            3f
 
         cmp             w5, #8
-        rgb24_load_uv_coeff half=1
+        rgb_load_uv_coeff half=1, bgr=\bgr
         b.lt            2f
+        // The following comments assume RGB order. The logic for RGB and BGR is the same.
 1:
         ld3             { v16.16b, v17.16b, v18.16b }, [x3]
         uaddlp          v19.8h, v16.16b         // v19: r
         uaddlp          v20.8h, v17.16b         // v20: g
         uaddlp          v21.8h, v18.16b         // v21: b
 
-        rgb24_to_yuv_product v19, v20, v21, v22, v23, v16, v0, v1, v2, #10
-        rgb24_to_yuv_product v19, v20, v21, v24, v25, v17, v3, v4, v5, #10
+        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
         sub             w5, w5, #8              // width -= 8
         add             x3, x3, #48             // src += 48
         cmp             w5, #8                  // width >= 8 ?
@@ -156,21 +182,30 @@ function ff_rgb24ToUV_half_neon, export=1
         cbnz            w5, 2b
 3:
         ret
+.endm
+
+function ff_rgb24ToUV_half_neon, export=1
+        rgbToUV_half    bgr=0
 endfunc
 
-function ff_rgb24ToUV_neon, export=1
+function ff_bgr24ToUV_half_neon, export=1
+        rgbToUV_half    bgr=1
+endfunc
+
+.macro rgbToUV bgr
         cmp             w5, #0                  // check width > 0
         b.le            3f
 
         cmp             w5, #16
-        rgb24_load_uv_coeff half=0
+        rgb_load_uv_coeff half=0, bgr=\bgr
         b.lt            2f
+        // The following comments assume RGB order. The logic for RGB and BGR is the same.
 1:
-        rgb24_to_yuv_load_rgb x3
-        rgb24_to_yuv_product v19, v20, v21, v25, v26, v16, v0, v1, v2, #9
-        rgb24_to_yuv_product v22, v23, v24, v27, v28, v17, v0, v1, v2, #9
-        rgb24_to_yuv_product v19, v20, v21, v25, v26, v18, v3, v4, v5, #9
-        rgb24_to_yuv_product v22, v23, v24, v27, v28, v19, v3, v4, v5, #9
+        rgb_to_yuv_load_rgb x3
+        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
+        rgb_to_yuv_product v22, v23, v24, v27, v28, v19, v3, v4, v5, #9
         sub             w5, w5, #16
         add             x3, x3, #48             // src += 48
         cmp             w5, #16
@@ -199,4 +234,12 @@ function ff_rgb24ToUV_neon, export=1
         cbnz            w5, 2b
 3:
         ret
+.endm
+
+function ff_rgb24ToUV_neon, export=1
+        rgbToUV         bgr=0
+endfunc
+
+function ff_bgr24ToUV_neon, export=1
+        rgbToUV         bgr=1
 endfunc
diff --git a/libswscale/aarch64/swscale.c b/libswscale/aarch64/swscale.c
index 4c4ea39dc1..ce70dbedcc 100644
--- a/libswscale/aarch64/swscale.c
+++ b/libswscale/aarch64/swscale.c
@@ -201,19 +201,18 @@ void ff_yuv2plane1_8_neon(
     default: break;                                                     \
     }
 
-void ff_rgb24ToY_neon(uint8_t *_dst, const uint8_t *src, const uint8_t *unused1,
-                      const uint8_t *unused2, int width,
-                      uint32_t *rgb2yuv, void *opq);
-
-void ff_rgb24ToUV_neon(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0,
-                       const uint8_t *src1,
-                       const uint8_t *src2, int width, uint32_t *rgb2yuv,
-                       void *opq);
-
-void ff_rgb24ToUV_half_neon(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0,
-                       const uint8_t *src1,
-                       const uint8_t *src2, int width, uint32_t *rgb2yuv,
-                       void *opq);
+#define NEON_INPUT(name) \
+void ff_##name##ToY_neon(uint8_t *dst, const uint8_t *src, const uint8_t *, \
+                        const uint8_t *, int w, uint32_t *coeffs, void *); \
+void ff_##name##ToUV_neon(uint8_t *, uint8_t *, const uint8_t *, \
+                         const uint8_t *, const uint8_t *, int w, \
+                         uint32_t *coeffs, void *); \
+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(bgr24);
+NEON_INPUT(rgb24);
 
 av_cold void ff_sws_init_swscale_aarch64(SwsContext *c)
 {
@@ -227,6 +226,13 @@ av_cold void ff_sws_init_swscale_aarch64(SwsContext *c)
             c->yuv2planeX = ff_yuv2planeX_8_neon;
         }
         switch (c->srcFormat) {
+        case AV_PIX_FMT_BGR24:
+            c->lumToYV12 = ff_bgr24ToY_neon;
+            if (c->chrSrcHSubSample)
+                c->chrToYV12 = ff_bgr24ToUV_half_neon;
+            else
+                c->chrToYV12 = ff_bgr24ToUV_neon;
+            break;
         case AV_PIX_FMT_RGB24:
             c->lumToYV12 = ff_rgb24ToY_neon;
             if (c->chrSrcHSubSample)
-- 
2.42.0



More information about the ffmpeg-devel mailing list