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

Zhao Zhili quinkblack at foxmail.com
Wed Jun 19 20:10:23 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
---
v2: Use LDP to load BGR coefficients

 libswscale/aarch64/input.S   | 71 +++++++++++++++++++++++++++---------
 libswscale/aarch64/swscale.c | 32 +++++++++-------
 2 files changed, 72 insertions(+), 31 deletions(-)

diff --git a/libswscale/aarch64/input.S b/libswscale/aarch64/input.S
index 33afa34111..cf8d062942 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,10 +43,15 @@
         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
+        ldp             w12, w11, [x5]          // w12: ry, 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
 
         mov             w9, #256                // w9 = 1 << (RGB2YUV_SHIFT - 7)
@@ -59,9 +64,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 +88,26 @@ function ff_rgb24ToY_neon, export=1
         cbnz            w4, 2b
 3:
         ret
+.endm
+
+function ff_rgb24ToY_neon, export=1
+        rgbToY          bgr=0
 endfunc
 
-.macro rgb24_load_uv_coeff half
+function ff_bgr24ToY_neon, export=1
+        rgbToY          bgr=1
+endfunc
+
+.macro rgb_load_uv_coeff half, bgr
+    .if \bgr
+        ldp             w12, w11, [x6, #12]
+        ldp             w10, w15, [x6, #20]
+        ldp             w14, w13, [x6, #28]
+    .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,12 +124,12 @@ 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
 1:
         ld3             { v16.16b, v17.16b, v18.16b }, [x3]
@@ -118,8 +137,8 @@ function ff_rgb24ToUV_half_neon, export=1
         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 +175,29 @@ 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
 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 +226,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 e4ea3309ba..c6594944c3 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);
 
 void ff_lumRangeFromJpeg_neon(int16_t *dst, int width);
 void ff_chrRangeFromJpeg_neon(int16_t *dstU, int16_t *dstV, int width);
@@ -247,6 +246,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