Add a NEON rgb24tobgr24 using ld3/st3 to swap R and B channels in
packed 24bpp RGB buffers. Handles all input sizes with a 16-pixel
NEON fast path, 8-pixel NEON cleanup, and scalar tail.

checkasm --bench on Apple M3 Max (1920*3 = 5760 bytes):
  rgb24tobgr24_c:    872.3 ( 1.00x)
  rgb24tobgr24_neon:  62.4 (13.98x)

Signed-off-by: David Christle <[email protected]>
---
 libswscale/aarch64/rgb2rgb.c      |  3 +++
 libswscale/aarch64/rgb2rgb_neon.S | 43 +++++++++++++++++++++++++++++++
 tests/checkasm/sw_rgb.c           | 31 ++++++++++++++++++++++
 3 files changed, 77 insertions(+)

diff --git a/libswscale/aarch64/rgb2rgb.c b/libswscale/aarch64/rgb2rgb.c
index f474228298..5873439db5 100644
--- a/libswscale/aarch64/rgb2rgb.c
+++ b/libswscale/aarch64/rgb2rgb.c
@@ -51,6 +51,8 @@ static void rgb24toyv12(const uint8_t *src, uint8_t *ydst, 
uint8_t *udst,
     }
 }
 
+void ff_rgb24tobgr24_neon(const uint8_t *src, uint8_t *dst, int src_size);
+
 void ff_interleave_bytes_neon(const uint8_t *src1, const uint8_t *src2,
                               uint8_t *dest, int width, int height,
                               int src1Stride, int src2Stride, int dstStride);
@@ -85,6 +87,7 @@ av_cold void rgb2rgb_init_aarch64(void)
 
     if (have_neon(cpu_flags)) {
         ff_rgb24toyv12  = rgb24toyv12;
+        rgb24tobgr24    = ff_rgb24tobgr24_neon;
         interleaveBytes = ff_interleave_bytes_neon;
         deinterleaveBytes = ff_deinterleave_bytes_neon;
         shuffle_bytes_0321 = ff_shuffle_bytes_0321_neon;
diff --git a/libswscale/aarch64/rgb2rgb_neon.S 
b/libswscale/aarch64/rgb2rgb_neon.S
index f6d625f11f..25e6c73f42 100644
--- a/libswscale/aarch64/rgb2rgb_neon.S
+++ b/libswscale/aarch64/rgb2rgb_neon.S
@@ -241,6 +241,49 @@ function ff_rgb24toyv12_neon, export=1
         ret
 endfunc
 
+// void ff_rgb24tobgr24_neon(const uint8_t *src, uint8_t *dst, int src_size);
+function ff_rgb24tobgr24_neon, export=1
+        // x0 = src, x1 = dst, w2 = src_size (bytes)
+
+        // Fast path: 48 bytes (16 pixels) per iteration
+        subs            w2, w2, #48
+        b.lt            2f
+1:
+        ld3             {v0.16b, v1.16b, v2.16b}, [x0], #48
+        mov             v3.16b, v0.16b
+        mov             v0.16b, v2.16b
+        mov             v2.16b, v3.16b
+        st3             {v0.16b, v1.16b, v2.16b}, [x1], #48
+        subs            w2, w2, #48
+        b.ge            1b
+2:
+        add             w2, w2, #48
+        // Medium path: 24 bytes (8 pixels)
+        cmp             w2, #24
+        b.lt            3f
+        ld3             {v0.8b, v1.8b, v2.8b}, [x0], #24
+        mov             v3.8b, v0.8b
+        mov             v0.8b, v2.8b
+        mov             v2.8b, v3.8b
+        sub             w2, w2, #24
+        st3             {v0.8b, v1.8b, v2.8b}, [x1], #24
+3:
+        // Scalar tail: 3 bytes (1 pixel) at a time
+        cmp             w2, #3
+        b.lt            4f
+5:
+        ldrb            w3, [x0], #1
+        ldrb            w4, [x0], #1
+        ldrb            w5, [x0], #1
+        subs            w2, w2, #3
+        strb            w5, [x1], #1
+        strb            w4, [x1], #1
+        strb            w3, [x1], #1
+        b.gt            5b
+4:
+        ret
+endfunc
+
 // void ff_interleave_bytes_neon(const uint8_t *src1, const uint8_t *src2,
 //                               uint8_t *dest, int width, int height,
 //                               int src1Stride, int src2Stride, int 
dstStride);
diff --git a/tests/checkasm/sw_rgb.c b/tests/checkasm/sw_rgb.c
index 6edfc93b0b..baeb34b465 100644
--- a/tests/checkasm/sw_rgb.c
+++ b/tests/checkasm/sw_rgb.c
@@ -834,6 +834,37 @@ void checkasm_check_sw_rgb(void)
     check_shuffle_bytes(shuffle_bytes_2130, "shuffle_bytes_2130");
     report("shuffle_bytes_2130");
 
+    {
+        /* rgb24tobgr24 operates on 3-byte pixels, so test widths must be
+         * multiples of 3 to avoid reading past the source buffer. */
+        static const int rgb24_width[] = {3, 12, 24, 36, 48, 126, 1920 * 3};
+        int i;
+#define RGB24_BENCH_WIDTH (1920 * 3)
+        LOCAL_ALIGNED_32(uint8_t, src0, [RGB24_BENCH_WIDTH]);
+        LOCAL_ALIGNED_32(uint8_t, src1, [RGB24_BENCH_WIDTH]);
+        LOCAL_ALIGNED_32(uint8_t, dst0, [RGB24_BENCH_WIDTH]);
+        LOCAL_ALIGNED_32(uint8_t, dst1, [RGB24_BENCH_WIDTH]);
+
+        declare_func(void, const uint8_t *src, uint8_t *dst, int src_size);
+
+        memset(dst0, 0, RGB24_BENCH_WIDTH);
+        memset(dst1, 0, RGB24_BENCH_WIDTH);
+        randomize_buffers(src0, RGB24_BENCH_WIDTH);
+        memcpy(src1, src0, RGB24_BENCH_WIDTH);
+
+        if (check_func(rgb24tobgr24, "rgb24tobgr24")) {
+            for (i = 0; i < FF_ARRAY_ELEMS(rgb24_width); i++) {
+                call_ref(src0, dst0, rgb24_width[i]);
+                call_new(src1, dst1, rgb24_width[i]);
+                if (memcmp(dst0, dst1, rgb24_width[i]))
+                    fail();
+            }
+            bench_new(src0, dst0, RGB24_BENCH_WIDTH);
+        }
+#undef RGB24_BENCH_WIDTH
+    }
+    report("rgb24tobgr24");
+
     check_uyvy_to_422p();
     report("uyvytoyuv422");
 
-- 
2.52.0


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

Reply via email to