[FFmpeg-devel] [PATCH v3] libavfilter/x86/vf_convolution: add sobel filter optimization and unit test with intel AVX512 VNNI

Henrik Gramner henrik at gramner.com
Tue Sep 6 16:01:57 EEST 2022


On Tue, Aug 23, 2022 at 10:43 AM <bin.wang-at-intel.com at ffmpeg.org> wrote:
> +.loop1:
> +    pxor m4, m4
> +    pxor m5, m5
> +
> +    ;Gx
> +    SOBEL_MUL_16 0, data_n1, 4
> +    SOBEL_MUL_16 1, data_n2, 4
> +    SOBEL_MUL_16 2, data_n1, 4
> +    SOBEL_ADD_16 6, 4
> +    SOBEL_MUL_16 7, data_p2, 4
> +    SOBEL_ADD_16 8, 4
> +
> [...]
> +
> +    ;Gy
> +    SOBEL_MUL_16 0, data_n1, 5
> +    SOBEL_ADD_16 2, 5
> +    SOBEL_MUL_16 3, data_n2, 5
> +    SOBEL_MUL_16 5, data_p2, 5
> +    SOBEL_MUL_16 6, data_n1, 5
> +    SOBEL_ADD_16 8, 5

This could probably be improved by doing something along the lines of
(untested, but you get the idea):

ALIGN 64
sobel_perm: db  0, 16, 32, 48,  1, 17, 33, 49,  2, 18, 34, 50,  3, 19, 35, 51
            db  4, 20, 36, 52,  5, 21, 37, 53,  6, 22, 38, 54,  7, 23, 39, 55
            db  8, 24, 40, 56,  9, 25, 41, 57, 10, 26, 42, 58, 11, 27, 43, 59
            db 12, 28, 44, 60, 13, 29, 45, 61, 14, 30, 46, 62, 15, 31, 47, 63
sobel_mulA: db -1,  1, -2,  2
sobel_mulB: db  1, -1,  2, -2

[...]

    mova          m6, [sobel_perm]
.loop1:
    movu         xm3, [c2q + xq]
    pmovzxbd      m5, [c0q + xq]
    vinserti32x4 ym3, [c6q + xq], 1
    pmovzxbd      m4, [c8q + xq]
    vinserti32x4  m2, m3, [c1q + xq], 2
    vinserti32x4  m3, [c5q + xq], 2
    vinserti32x4  m2, [c7q + xq], 3
    vinserti32x4  m3, [c3q + xq], 3
    vpermb        m2, m6, m2
    psubd         m4, m5
    vpermb        m3, m6, m3
    mova          m5, m4
    vpdpbusd      m4, m2, [sobel_mulA] {1to16}
    vpdpbusd      m5, m3, [sobel_mulB] {1to16}

> +    mulps     m4, m0       ; sum *= scale
> +    addps     m4, m1       ; sum += delta

    fmaddps m4, m4, m0, m1

> +    vpmovusdb xmm4, m4
> +    movdqu    [dstq + xq], xmm4

    vpmovusdb [dstq + xq], m4

> +    mulss     xmm5, xmm5    ; b1 * b1
> +    addss     xmm4, xmm5

    fmaddss xmm4, xmm5, xmm5, xmm4

> +    mulss     xmm4, xmm0     ; sum *= rdiv
> +    addss     xmm4, xmm1     ; sum += bias

    fmaddss xmm4, xmm4, xmm0, xmm1



> +
> +    cvtdq2ps  m4, m4
> +    mulps     m4, m4
> +
> +    ;Gy
> +    SOBEL_MUL_16 0, data_n1, 5
> +    SOBEL_ADD_16 2, 5
> +    SOBEL_MUL_16 3, data_n2, 5
> +    SOBEL_MUL_16 5, data_p2, 5
> +    SOBEL_MUL_16 6, data_n1, 5
> +    SOBEL_ADD_16 8, 5
> +
> +    cvtdq2ps    m5, m5
> +    VFMADD231PS m4, m5, m5
> +
> +    sqrtps    m4, m4
> +    mulps     m4, m0       ; sum *= scale
> +    addps     m4, m1       ; sum += delta
> +    cvttps2dq m4, m4
> +    vpmovusdb xmm4, m4
> +    movdqu    [dstq + xq], xmm4
> +
> +    add xq, mmsize/4
> +    cmp xq, widthq
> +    jl .loop1
> +
> +    add widthq, rq
> +    cmp xq, widthq
> +    jge .end
> +
> +.loop2:
> +    xor  rd, rd
> +    pxor m4, m4
> +
> +    ;Gx
> +    SOBEL_MUL 0, data_n1
> +    SOBEL_MUL 1, data_n2
> +    SOBEL_MUL 2, data_n1
> +    SOBEL_ADD 6
> +    SOBEL_MUL 7, data_p2
> +    SOBEL_ADD 8
> +
> +    cvtsi2ss xmm4, rd
> +    mulss    xmm4, xmm4
> +
> +    xor rd, rd
> +    ;Gy
> +    SOBEL_MUL 0, data_n1
> +    SOBEL_ADD 2
> +    SOBEL_MUL 3, data_n2
> +    SOBEL_MUL 5, data_p2
> +    SOBEL_MUL 6, data_n1
> +    SOBEL_ADD 8
> +
> +    cvtsi2ss  xmm5, rd
> +    mulss     xmm5, xmm5    ; b1 * b1
> +    addss     xmm4, xmm5
> +
> +    sqrtps    xmm4, xmm4
> +    mulss     xmm4, xmm0     ; sum *= rdiv
> +    addss     xmm4, xmm1     ; sum += bias
> +    cvttps2dq xmm4, xmm4     ; trunc to integer
> +    packssdw  xmm4, xmm4
> +    packuswb  xmm4, xmm4
> +    movd      rd, xmm4
> +    mov       [dstq + xq], rb
> +
> +    add xq, 1
> +    cmp xq, widthq
> +    jl .loop2
> +.end:
> +    RET
> +%endmacro
> +
> +%if ARCH_X86_64
> +%if HAVE_AVX512ICL_EXTERNAL
> +INIT_ZMM avx512icl
> +FILTER_SOBEL
> +%endif
> +%endif
> diff --git a/libavfilter/x86/vf_convolution_init.c b/libavfilter/x86/vf_convolution_init.c
> index b78a47d02b..52a3d28991 100644
> --- a/libavfilter/x86/vf_convolution_init.c
> +++ b/libavfilter/x86/vf_convolution_init.c
> @@ -29,6 +29,11 @@ void ff_filter_3x3_sse4(uint8_t *dst, int width,
>                          const uint8_t *c[], int peak, int radius,
>                          int dstride, int stride, int size);
>
> +void ff_filter_sobel_avx512icl(uint8_t *dst, int width,
> +                         float scale, float delta, const int *const matrix,
> +                         const uint8_t *c[], int peak, int radius,
> +                         int dstride, int stride, int size);
> +
>  av_cold void ff_convolution_init_x86(ConvolutionContext *s)
>  {
>  #if ARCH_X86_64
> @@ -44,3 +49,16 @@ av_cold void ff_convolution_init_x86(ConvolutionContext *s)
>      }
>  #endif
>  }
> +
> +av_cold void ff_sobel_init_x86(ConvolutionContext *s)
> +{
> +#if ARCH_X86_64
> +    int cpu_flags = av_get_cpu_flags();
> +    for (int i = 0; i < s->nb_planes; i++) {
> +        if (s->depth == 8) {
> +            if (EXTERNAL_AVX512ICL(cpu_flags))
> +                s->filter[i] = ff_filter_sobel_avx512icl;
> +        }
> +    }
> +#endif
> +}
> diff --git a/tests/checkasm/Makefile b/tests/checkasm/Makefile
> index 1ac170491b..4e91547fde 100644
> --- a/tests/checkasm/Makefile
> +++ b/tests/checkasm/Makefile
> @@ -44,6 +44,7 @@ AVFILTEROBJS-$(CONFIG_GBLUR_FILTER)      += vf_gblur.o
>  AVFILTEROBJS-$(CONFIG_HFLIP_FILTER)      += vf_hflip.o
>  AVFILTEROBJS-$(CONFIG_THRESHOLD_FILTER)  += vf_threshold.o
>  AVFILTEROBJS-$(CONFIG_NLMEANS_FILTER)    += vf_nlmeans.o
> +AVFILTEROBJS-$(CONFIG_CONVOLUTION_FILTER)    += vf_convolution.o
>
>  CHECKASMOBJS-$(CONFIG_AVFILTER) += $(AVFILTEROBJS-yes)
>
> diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c
> index e56fd3850e..ae5e9d1143 100644
> --- a/tests/checkasm/checkasm.c
> +++ b/tests/checkasm/checkasm.c
> @@ -191,6 +191,9 @@ static const struct {
>      #if CONFIG_THRESHOLD_FILTER
>          { "vf_threshold", checkasm_check_vf_threshold },
>      #endif
> +    #if CONFIG_CONVOLUTION_FILTER
> +        { "vf_convolution", checkasm_check_vf_convolution },
> +    #endif
>  #endif
>  #if CONFIG_SWSCALE
>      { "sw_gbrp", checkasm_check_sw_gbrp },
> diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
> index d7645d3730..71c722cf77 100644
> --- a/tests/checkasm/checkasm.h
> +++ b/tests/checkasm/checkasm.h
> @@ -85,6 +85,7 @@ void checkasm_check_vf_eq(void);
>  void checkasm_check_vf_gblur(void);
>  void checkasm_check_vf_hflip(void);
>  void checkasm_check_vf_threshold(void);
> +void checkasm_check_vf_convolution(void);
>  void checkasm_check_vp8dsp(void);
>  void checkasm_check_vp9dsp(void);
>  void checkasm_check_videodsp(void);
> diff --git a/tests/checkasm/vf_convolution.c b/tests/checkasm/vf_convolution.c
> new file mode 100644
> index 0000000000..a10da8b45e
> --- /dev/null
> +++ b/tests/checkasm/vf_convolution.c
> @@ -0,0 +1,103 @@
> +/*
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 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 General Public License for more details.
> + *
> + * You should have received a copy of the GNU 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 <string.h>
> +#include "checkasm.h"
> +#include "libavfilter/avfilter.h"
> +#include "libavfilter/convolution.h"
> +#include "libavutil/intreadwrite.h"
> +#include "libavutil/mem_internal.h"
> +
> +#define WIDTH 512
> +#define HEIGHT 512
> +#define SRC_STRIDE 512
> +#define PIXELS (WIDTH * HEIGHT)
> +
> +#define randomize_buffers(buf, size)      \
> +    do {                                  \
> +        int j;                            \
> +        uint8_t *tmp_buf = (uint8_t *)buf;\
> +        for (j = 0; j< size; j++)         \
> +            tmp_buf[j] = rnd() & 0xFF;    \
> +    } while (0)
> +
> +static void check_sobel(const char * report_name)
> +{
> +    LOCAL_ALIGNED_32(uint8_t, src,     [PIXELS]);
> +    LOCAL_ALIGNED_32(uint8_t, dst_ref, [PIXELS]);
> +    LOCAL_ALIGNED_32(uint8_t, dst_new, [PIXELS]);
> +    const int height = WIDTH;
> +    const int width  = HEIGHT;
> +    const int stride = SRC_STRIDE;
> +    const int dstride = SRC_STRIDE;
> +    int mode = 0;
> +    const uint8_t *c[49];
> +    const int radius = 1;
> +    const int bpc = 1;
> +    const int step = mode == MATRIX_COLUMN ? 16 : 1;
> +    const int slice_start = 0;
> +    const int slice_end = height;
> +    int y;
> +    const int sizew = mode == MATRIX_COLUMN ? height : width;
> +    float scale = 2;
> +    float delta = 10;
> +
> +    ConvolutionContext s;
> +
> +    s.scale = scale;
> +    s.delta = delta;
> +    s.depth = 8;
> +    s.nb_planes = 3;
> +    ff_convolution_init(&s, "sobel");
> +
> +    declare_func(void, uint8_t *dst, int width, float scale, float delta, const int *const matrix,
> +                 const uint8_t *c[], int peak, int radius, int dstride, int stride, int size);
> +
> +    memset(dst_ref, 0, PIXELS);
> +    memset(dst_new, 0, PIXELS);
> +    randomize_buffers(src, PIXELS);
> +
> +    if (check_func(s.filter[0], "%s", report_name)) {
> +        for (y = slice_start; y < slice_end; y += step) {
> +            const int xoff = mode == MATRIX_COLUMN ? (y - slice_start) * bpc : radius * bpc;
> +            const int yoff = mode == MATRIX_COLUMN ? radius * dstride : 0;
> +
> +            s.setup[0](radius, c, src, stride, radius, width, y, height, bpc);
> +            call_ref(dst_ref + yoff + xoff, sizew - 2 * radius,
> +                     scale, delta, NULL, c, 0, radius,
> +                     dstride, stride, slice_end - step);
> +            call_new(dst_new + yoff + xoff, sizew - 2 * radius,
> +                     scale, delta, NULL, c, 0, radius,
> +                     dstride, stride, slice_end - step);
> +            if (memcmp(dst_ref + yoff + xoff, dst_new + yoff + xoff, slice_end - step))
> +                fail();
> +            bench_new(dst_new + yoff + xoff, sizew - 2 * radius,
> +                      scale, delta, NULL, c, 0, radius,
> +                      dstride, stride, slice_end - step);
> +            if (mode != MATRIX_COLUMN)
> +                dst_ref += dstride;
> +        }
> +    }
> +
> +}
> +
> +void checkasm_check_vf_convolution(void)
> +{
> +    check_sobel("sobel");
> +    report("convolution:sobel");
> +}
> --
> 2.27.0
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".


More information about the ffmpeg-devel mailing list