[FFmpeg-devel] [PATCH] swscale/arm: add ff_nv{12, 21}_to_{argb, rgba, abgr, bgra}_neon

Clément Bœsch u at pkh.me
Thu Nov 19 11:48:53 CET 2015


From: Matthieu Bouron <matthieu.bouron at stupeflix.com>

Signed-off-by: Matthieu Bouron <matthieu.bouron at stupeflix.com>
Signed-off-by: Clément Bœsch <clement at stupeflix.com>

---
The function takes about 29ms with a 1080p source (testsrc2) on a
cortex-a8. Though, 16ms (more than half the time!) is spend in the vst2
call. Any suggestion on how to speed up this?

Also, the reference code seems to cause some kind of ringing, while our
ASM doesn't:
  http://b.pkh.me/nv12-rgba-ref.png
  http://b.pkh.me/nv12-rgba-neon.png

Last, we noticed that the y_offset is scaled to 1<<9 for some reason we
couldn't figure out. Hopefully we're doing it correctly here.
---
 libswscale/arm/Makefile           |   3 +-
 libswscale/arm/swscale_unscaled.c |  62 +++++++++++++++
 libswscale/arm/yuv2rgb_neon.S     | 162 ++++++++++++++++++++++++++++++++++++++
 libswscale/swscale_unscaled.c     |   4 +-
 4 files changed, 228 insertions(+), 3 deletions(-)
 create mode 100644 libswscale/arm/yuv2rgb_neon.S

diff --git a/libswscale/arm/Makefile b/libswscale/arm/Makefile
index 8b5a97b..97b3561 100644
--- a/libswscale/arm/Makefile
+++ b/libswscale/arm/Makefile
@@ -1,4 +1,5 @@
-# OBJS        += arm/swscale_unscaled.o
+OBJS        += arm/swscale_unscaled.o
 
 # NEON-OBJS   += arm/rgb2yuv_neon_32.o
 # NEON-OBJS   += arm/rgb2yuv_neon_16.o
+NEON-OBJS   += arm/yuv2rgb_neon.o
diff --git a/libswscale/arm/swscale_unscaled.c b/libswscale/arm/swscale_unscaled.c
index 04be762..79e7129 100644
--- a/libswscale/arm/swscale_unscaled.c
+++ b/libswscale/arm/swscale_unscaled.c
@@ -23,6 +23,7 @@
 #include "libswscale/swscale_internal.h"
 #include "libavutil/arm/cpu.h"
 
+#if 0
 extern void rgbx_to_nv12_neon_32(const uint8_t *src, uint8_t *y, uint8_t *chroma,
                 int width, int height,
                 int y_stride, int c_stride, int src_stride,
@@ -60,8 +61,65 @@ static int rgbx_to_nv12_neon_16_wrapper(SwsContext *context, const uint8_t *src[
 
     return 0;
 }
+#endif
+
+#define DECLARE_FF_NVX_TO_RGBX_FUNCS(ifmt, ofmt)                                            \
+int ff_##ifmt##_to_##ofmt##_neon(int w, int h,                                              \
+                                 uint8_t *dst, int linesize,                                \
+                                 const uint8_t *srcY, int linesizeY,                        \
+                                 const uint8_t *srcC, int linesizeC,                        \
+                                 const int16_t *table,                                      \
+                                 int y_offset,                                              \
+                                 int y_coeff);                                              \
+                                                                                            \
+static int ifmt##_to_##ofmt##_neon_wrapper(SwsContext *c, const uint8_t *src[],             \
+                                           int srcStride[], int srcSliceY, int srcSliceH,   \
+                                           uint8_t *dst[], int dstStride[]) {               \
+    const int16_t yuv2rgb_table[] = {                                                       \
+        c->yuv2rgb_v2r_coeff,                                                               \
+        c->yuv2rgb_u2g_coeff,                                                               \
+        c->yuv2rgb_v2g_coeff,                                                               \
+        c->yuv2rgb_u2b_coeff,                                                               \
+    };                                                                                      \
+                                                                                            \
+    ff_##ifmt##_to_##ofmt##_neon(c->srcW, srcSliceH,                                        \
+                                 dst[0] + srcSliceY * dstStride[0], dstStride[0],           \
+                                 src[0] + srcSliceY * srcStride[0], srcStride[0],           \
+                                 src[1] + (srcSliceY / 2) * srcStride[1],                   \
+                                 srcStride[1],                                              \
+                                 yuv2rgb_table,                                             \
+                                 c->yuv2rgb_y_offset >> 9,                                  \
+                                 c->yuv2rgb_y_coeff);                                       \
+                                                                                            \
+    return 0;                                                                               \
+}
+
+#define DECLARE_FF_NVX_TO_ALL_RGBX_FUNCS(nvx)                                               \
+DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, argb)                                                     \
+DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, rgba)                                                     \
+DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, abgr)                                                     \
+DECLARE_FF_NVX_TO_RGBX_FUNCS(nvx, bgra)                                                     \
+
+DECLARE_FF_NVX_TO_ALL_RGBX_FUNCS(nv12)
+DECLARE_FF_NVX_TO_ALL_RGBX_FUNCS(nv21)
+
+#define SET_FF_NVX_TO_RGBX_FUNC(ifmt, IFMT, ofmt, OFMT) do {                                \
+    if (c->srcFormat == AV_PIX_FMT_##IFMT                                                   \
+        && c->dstFormat == AV_PIX_FMT_##OFMT                                                \
+        && !(c->srcH & 1)) {                                                                \
+        c->swscale = ifmt##_to_##ofmt##_neon_wrapper;                                       \
+    }                                                                                       \
+} while (0)
+
+#define SET_FF_NVX_TO_ALL_RGBX_FUNC(nvx, NVX) do {                                          \
+    SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, argb, ARGB);                                          \
+    SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, rgba, RGBA);                                          \
+    SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, abgr, ABGR);                                          \
+    SET_FF_NVX_TO_RGBX_FUNC(nvx, NVX, bgra, BGRA);                                          \
+} while (0)
 
 static void get_unscaled_swscale_neon(SwsContext *c) {
+#if 0
     int accurate_rnd = c->flags & SWS_ACCURATE_RND;
     if (c->srcFormat == AV_PIX_FMT_RGBA
             && c->dstFormat == AV_PIX_FMT_NV12
@@ -69,6 +127,10 @@ static void get_unscaled_swscale_neon(SwsContext *c) {
         c->swscale = accurate_rnd ? rgbx_to_nv12_neon_32_wrapper
                         : rgbx_to_nv12_neon_16_wrapper;
     }
+#endif
+
+    SET_FF_NVX_TO_ALL_RGBX_FUNC(nv12, NV12);
+    SET_FF_NVX_TO_ALL_RGBX_FUNC(nv21, NV21);
 }
 
 void ff_get_unscaled_swscale_arm(SwsContext *c)
diff --git a/libswscale/arm/yuv2rgb_neon.S b/libswscale/arm/yuv2rgb_neon.S
new file mode 100644
index 0000000..79073d7
--- /dev/null
+++ b/libswscale/arm/yuv2rgb_neon.S
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2015 Matthieu Bouron <matthieu.bouron stupeflix.com>
+ * Copyright (c) 2015 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/arm/asm.S"
+
+.macro compute_premult half_u half_v
+    vmov                d2, \half_u                                    @ copy left q14 to left q1
+    vmov                d3, \half_u                                    @ copy left q14 to right q1
+    vmov                d4, \half_v                                    @ copy left q15 to left q2
+    vmov                d5, \half_v                                    @ copy left q15 to right q2
+
+    vzip.16             d2, d3                                         @ U1U1U2U2U3U3U4U4
+    vzip.16             d4, d5                                         @ V1V1V2V2V3V3V4V4
+
+    vmull.s16           q8,  d4, d1[0]                                 @  V * v2r             (left,  red)
+    vmull.s16           q9,  d5, d1[0]                                 @  V * v2r             (right, red)
+    vmull.s16           q10, d2, d1[1]                                 @  U * u2g
+    vmull.s16           q11, d3, d1[1]                                 @  U * u2g
+    vmlal.s16           q10, d4, d1[2]                                 @  U * u2g + V * v2g   (left,  green)
+    vmlal.s16           q11, d5, d1[2]                                 @  U * u2g + V * v2g   (right, green)
+    vmull.s16           q12, d2, d1[3]                                 @  U * u2b             (left,  blue)
+    vmull.s16           q13, d3, d1[3]                                 @  U * u2b             (right, blue)
+.endm
+
+.macro compute_color dst_comp pre1 pre2
+    vadd.s32            q3, q1, \pre1
+    vadd.s32            q4, q2, \pre2
+    vqrshrun.s32        d10, q3, #13
+    vqrshrun.s32        d11, q4, #13                                   @ q5 = ({q3,q4} + (1<<12)) >> 13
+    vqmovn.u16          \dst_comp, q5                                  @ saturate 16bit -> 8bit
+.endm
+
+.macro compute_rgba r g b a
+    compute_color       \r, q8,  q9
+    compute_color       \g, q10, q11
+    compute_color       \b, q12, q13
+    vmov.u8             \a, #255
+.endm
+
+.macro compute_half_line dst half_y ofmt
+    vmovl.u8            q7, \half_y                                    @ 8px of Y
+    vdup.16             q5, r9
+    vsub.s16            q7, q5
+    vmull.s16           q1, d14, d0                                    @ q1 = (srcY - y_offset) * y_coeff (left)
+    vmull.s16           q2, d15, d0                                    @ q2 = (srcY - y_offset) * y_coeff (right)
+
+.ifc \ofmt,argb
+    compute_rgba        d13, d14, d15, d12
+.endif
+
+.ifc \ofmt,rgba
+    compute_rgba        d12, d13, d14, d15
+.endif
+
+.ifc \ofmt,abgr
+    compute_rgba        d15, d14, d13, d12
+.endif
+
+.ifc \ofmt,bgra
+    compute_rgba        d14, d13, d12, d15
+.endif
+
+    vst2.32             {q6, q7}, [\dst,:128]!
+.endm
+
+.macro declare_func ifmt ofmt
+function ff_\ifmt\()_to_\ofmt\()_neon, export=1
+    push                {r4-r12, lr}
+    vpush               {q4-q7}
+    ldr                 r4, [sp, #104]                                 @ r4  = srcY
+    ldr                 r5, [sp, #108]                                 @ r5  = linesizeY
+    ldr                 r6, [sp, #112]                                 @ r6  = srcC
+    ldr                 r7, [sp, #116]                                 @ r7  = linesizeC
+    ldr                 r8, [sp, #120]                                 @ r8  = table
+    ldr                 r9, [sp, #124]                                 @ r9  = y_offset
+    ldr                 r10,[sp, #128]                                 @ r10 = y_coeff
+    vdup.16             d0, r10                                        @ d0  = y_coeff
+    vld1.16             {d1}, [r8]                                     @ d1  = *table
+    add                 r11, r2, r3                                    @ r11 = dst + linesize (dst2)
+    add                 r12, r4, r5                                    @ r12 = srcY + linesizeY (srcY2)
+    lsl                 r3, r3, #1
+    lsl                 r5, r5, #1
+    lsl                 r8, r0, #2
+    sub                 r3, r3, r8                                     @ r3 = linesize  * 2 - width * 4 (padding)
+    sub                 r5, r5, r0                                     @ r5 = linesizeY * 2 - width     (paddingY)
+    sub                 r7, r7, r0                                     @ r7 = linesizeC * 2 - width     (paddingC)
+1:
+    mov                 r8, r0                                         @ r8 = width
+2:
+    pld [r6, #64*3]
+    pld [r4, #64*3]
+    pld [r12, #64*3]
+
+    vld2.8              {d2, d3}, [r6]!                                @ q1: interleaved chroma line
+    vmov.i8             d10, #128
+.ifc \ifmt,nv12
+    vsubl.u8            q14, d2, d10                                   @ q14 = U - 128
+    vsubl.u8            q15, d3, d10                                   @ q15 = V - 128
+.else
+    vsubl.u8            q14, d3, d10                                   @ q14 = U - 128
+    vsubl.u8            q15, d2, d10                                   @ q15 = V - 128
+.endif
+
+    compute_premult     d28, d30
+
+    vld1.8              {q7}, [r4]!                                    @ first line of luma
+    vmov                d28, d15                                       @ save right of the first line of luma for later use
+    compute_half_line   r2, d14, \ofmt
+
+    vld1.8              {q7}, [r12]!                                   @ second line of luma
+    vmov                d30, d15                                       @ save right of the second line of luma for later use
+    compute_half_line   r11, d14, \ofmt
+
+    compute_premult     d29, d31
+    compute_half_line   r2,  d28, \ofmt
+    compute_half_line   r11, d30, \ofmt
+
+    subs                r8, r8, #16                                    @ width -= 16
+    bgt                 2b
+
+    add                 r2, r2, r3                                     @ dst   += padding
+    add                 r11, r11, r3                                   @ dst2  += padding
+    add                 r4, r4, r5                                     @ srcY  += paddingY
+    add                 r6, r6, r7                                     @ srcC  += paddingC
+    add                 r12, r12, r5                                   @ srcY2 += paddingY
+
+    subs                r1, r1, #2                                     @ height -= 2
+    bgt                 1b
+
+    vpop                {q4-q7}
+    pop                 {r4-r12, lr}
+    mov                 pc, lr
+endfunc
+.endm
+
+.macro declare_rgb_funcs ifmt
+    declare_func \ifmt, argb
+    declare_func \ifmt, rgba
+    declare_func \ifmt, abgr
+    declare_func \ifmt, bgra
+.endm
+
+declare_rgb_funcs nv12
+declare_rgb_funcs nv21
diff --git a/libswscale/swscale_unscaled.c b/libswscale/swscale_unscaled.c
index f387f52..89cb90d 100644
--- a/libswscale/swscale_unscaled.c
+++ b/libswscale/swscale_unscaled.c
@@ -1762,8 +1762,8 @@ void ff_get_unscaled_swscale(SwsContext *c)
 
     if (ARCH_PPC)
         ff_get_unscaled_swscale_ppc(c);
-//     if (ARCH_ARM)
-//         ff_get_unscaled_swscale_arm(c);
+     if (ARCH_ARM)
+         ff_get_unscaled_swscale_arm(c);
 }
 
 /* Convert the palette to the same packed 32-bit format as the palette */
-- 
2.6.2



More information about the ffmpeg-devel mailing list