[FFmpeg-devel] [PATCH] avcodec/v210enc: add new function for avx2 avx512 avx512icl

James Darnley jdarnley at obe.tv
Fri Oct 28 21:55:08 EEST 2022


Negligible speed difference for avx2 on Zen 2 (Ryzen 5700X) and
Broadwell (Xeon E5-2620 v4):
    1690±4.3 decicycles vs. 1693±78.4
    1439±31.1 decicycles vs 1429±16.7

Moderate speedup with avx512 on Skylake-X (Xeon D-2123IT):
1.22x faster (793±0.8 vs. 649±5.5 decicycles) compared with avx2

Better speedup with avx512icl on Ice Lake (Xeon Silver 4316):
1.77x faster (784±1.8 vs. 442±11.6 decicycles) compared with avx2

Co-authors:
    Henrik Gramner <henrik at gramner.com>
    Kieran Kunhya <kierank at obe.tv>
---
 libavcodec/x86/v210enc.asm    | 80 ++++++++++++++++++++++++++++++++++-
 libavcodec/x86/v210enc_init.c | 14 ++++++
 2 files changed, 92 insertions(+), 2 deletions(-)

diff --git a/libavcodec/x86/v210enc.asm b/libavcodec/x86/v210enc.asm
index 965f2bea3c..afac238ede 100644
--- a/libavcodec/x86/v210enc.asm
+++ b/libavcodec/x86/v210enc.asm
@@ -21,7 +21,7 @@
 
 %include "libavutil/x86/x86util.asm"
 
-SECTION_RODATA 32
+SECTION_RODATA 64
 
 cextern pw_4
 %define v210_enc_min_10 pw_4
@@ -46,6 +46,16 @@ v210_enc_chroma_shuf2_8: times 2 db 3,-1,4,-1,5,-1,7,-1,11,-1,12,-1,13,-1,15,-1
 
 v210_enc_chroma_mult_8: times 2 dw 4,16,64,0,64,4,16,0
 
+v210enc_8_permb: db 32, 0,48,-1 ,  1,33, 2,-1 , 49, 3,34,-1 ,  4,50, 5,-1
+                 db 35, 6,51,-1 ,  7,36, 8,-1 , 52, 9,37,-1 , 10,53,11,-1
+                 db 38,12,54,-1 , 13,39,14,-1 , 55,15,40,-1 , 16,56,17,-1
+                 db 41,18,57,-1 , 19,42,20,-1 , 58,21,43,-1 , 22,59,23,-1
+v210enc_8_shufb: db  0, 8, 1,-1 ,  9, 2,10,-1 ,  3,11, 4,-1 , 12, 5,13,-1
+                 db  2,10, 3,-1 , 11, 4,12,-1 ,  5,13, 6,-1 , 14, 7,15,-1
+v210enc_8_permd: dd 0,1,4,5, 1,2,5,6
+v210enc_8_mult: db 4, 0, 64, 0
+v210enc_8_mask: dd 255<<12
+
 SECTION .text
 
 %macro v210_planar_pack_10 0
@@ -178,7 +188,73 @@ INIT_XMM avx
 v210_planar_pack_8
 %endif
 
+%macro v210_planar_pack_8_new 0
+
+cglobal v210_planar_pack_8, 5, 5, 7+notcpuflag(avx512icl), y, u, v, dst, width
+    add     yq, widthq
+    shr     widthq, 1
+    add     uq, widthq
+    add     vq, widthq
+    neg     widthq
+
+    %if cpuflag(avx512icl)
+        mova m2, [v210enc_8_permb]
+    %else
+        mova m2, [v210enc_8_permd]
+    %endif
+    vpbroadcastd   m3, [v210enc_8_mult]
+    VBROADCASTI128 m4, [v210_enc_min_8] ; only ymm sized
+    VBROADCASTI128 m5, [v210_enc_max_8] ; only ymm sized
+    vpbroadcastd   m6, [v210enc_8_mask]
+    %if notcpuflag(avx512icl)
+        movu m7, [v210enc_8_shufb]
+    %endif
+
+    .loop:
+        %if cpuflag(avx512icl)
+            movu         ym1, [yq + 2*widthq]
+            vinserti32x4  m1, [uq + 1*widthq], 2
+            vinserti32x4  m1, [vq + 1*widthq], 3
+            vpermb        m1, m2, m1                 ; uyv0 yuy0 vyu0 yvy0
+        %else
+            movq         xm0, [uq + 1*widthq]        ; uuuu uuxx
+            movq         xm1, [vq + 1*widthq]        ; vvvv vvxx
+            punpcklbw    xm1, xm0, xm1               ; uvuv uvuv uvuv xxxx
+            vinserti128   m1, m1, [yq + 2*widthq], 1 ; uvuv uvuv uvuv xxxx yyyy yyyy yyyy xxxx
+            vpermd        m1, m2, m1                 ; uvuv uvxx yyyy yyxx xxuv uvuv xxyy yyyy
+            pshufb        m1, m7                     ; uyv0 yuy0 vyu0 yvy0
+        %endif
+        CLIPUB       m1, m4, m5
+
+        pmaddubsw  m0, m1, m3
+        pslld      m1,  4
+        %if cpuflag(avx512)
+            vpternlogd m0, m1, m6, 0xd8 ; C?B:A
+        %else
+            pand       m1, m6, m1
+            pandn      m0, m6, m0
+            por        m0, m0, m1
+        %endif
+
+        movu  [dstq], m0
+        add     dstq, mmsize
+        add   widthq, (mmsize*3)/16
+    jl .loop
+RET
+
+%endmacro
+
 %if HAVE_AVX2_EXTERNAL
 INIT_YMM avx2
-v210_planar_pack_8
+v210_planar_pack_8_new
+%endif
+
+%if HAVE_AVX512_EXTERNAL
+INIT_YMM avx512
+v210_planar_pack_8_new
+%endif
+
+%if HAVE_AVX512ICL_EXTERNAL
+INIT_ZMM avx512icl
+v210_planar_pack_8_new
 %endif
diff --git a/libavcodec/x86/v210enc_init.c b/libavcodec/x86/v210enc_init.c
index 13a351dd1d..6e9f8c6e61 100644
--- a/libavcodec/x86/v210enc_init.c
+++ b/libavcodec/x86/v210enc_init.c
@@ -27,6 +27,10 @@ void ff_v210_planar_pack_8_avx(const uint8_t *y, const uint8_t *u,
                                const uint8_t *v, uint8_t *dst, ptrdiff_t width);
 void ff_v210_planar_pack_8_avx2(const uint8_t *y, const uint8_t *u,
                                 const uint8_t *v, uint8_t *dst, ptrdiff_t width);
+void ff_v210_planar_pack_8_avx512(const uint8_t *y, const uint8_t *u,
+                                const uint8_t *v, uint8_t *dst, ptrdiff_t width);
+void ff_v210_planar_pack_8_avx512icl(const uint8_t *y, const uint8_t *u,
+                                const uint8_t *v, uint8_t *dst, ptrdiff_t width);
 void ff_v210_planar_pack_10_ssse3(const uint16_t *y, const uint16_t *u,
                                   const uint16_t *v, uint8_t *dst,
                                   ptrdiff_t width);
@@ -52,4 +56,14 @@ av_cold void ff_v210enc_init_x86(V210EncContext *s)
         s->sample_factor_10 = 2;
         s->pack_line_10     = ff_v210_planar_pack_10_avx2;
     }
+
+    if (EXTERNAL_AVX512(cpu_flags)) {
+        s->sample_factor_8  = 2;
+        s->pack_line_8      = ff_v210_planar_pack_8_avx512;
+    }
+
+    if (EXTERNAL_AVX512ICL(cpu_flags)) {
+        s->sample_factor_8  = 4;
+        s->pack_line_8      = ff_v210_planar_pack_8_avx512icl;
+    }
 }
-- 
2.38.0



More information about the ffmpeg-devel mailing list