[FFmpeg-devel] [PATCH 4/4] avfilter/vf_v360: x86 SIMD for interpolations

James Almer jamrial at gmail.com
Wed Sep 4 23:01:31 EEST 2019


On 9/4/2019 4:28 PM, Paul B Mahol wrote:
> diff --git a/libavfilter/x86/vf_v360.asm b/libavfilter/x86/vf_v360.asm
> new file mode 100644
> index 0000000000..46142a3bad
> --- /dev/null
> +++ b/libavfilter/x86/vf_v360.asm
> @@ -0,0 +1,104 @@
> +;*****************************************************************************
> +;* x86-optimized functions for v360 filter
> +;*
> +;* 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/x86/x86util.asm"
> +
> +SECTION_RODATA
> +
> +ALIGN 32
> +
> +pb_mask: db 0,4,8,12,5,5,5,5,5,5,5,5,5,5,5,5
> +pd_255: times 4 dd 255
> +pb_255: times 16 db 255

No need for this one. See below.

> +
> +SECTION .text
> +
> +; void ff_remap2_8bit_line_avx2(uint8_t *dst, int width, const uint8_t *src, ptrdiff_t in_linesize,
> +;                               const uint16_t *u, const uint16_t *v, const int16_t *ker);
> +
> +%if HAVE_AVX2_EXTERNAL
> +INIT_YMM avx2
> +cglobal remap1_8bit_line, 7, 7, 7, dst, width, src, in_linesize, u, v, x

You're loading seven regs when you only have six. x doesn't need to be
loaded.

> +    movsxdifnidn widthq, widthd
> +    movsxdifnidn in_linesizeq, in_linesized

Unneeded since in_linesize is ptrdiff_t.

> +    xor             xq, xq
> +    movd           xm0, in_linesized
> +    VBROADCASTI128  m4, [pb_255]

pcmpeqw m4, m4

> +    VBROADCASTI128  m6, [pb_mask]
> +    vpbroadcastd    m0, xm0
> +
> +    .loop:
> +        vpmovsxwd   m1, [vq + xq * 2]
> +        vpmovsxwd   m2, [uq + xq * 2]

No need to use the v prefix in all these pre-avx instructions. INIT_YMM
takes care of it.

> +
> +        vpmulld          m3, m1, m0
> +        vpaddd           m1, m3, m2

pmulld m1, m0
paddd  m1, m2

> +        mova             m2, m4

Pointless mova. Just use m4 in the vpgatherdd below.

> +        vpgatherdd       m5, [srcq + m1], m2
> +        vextracti128    xm3, m5, 1
> +        vpshufb          m1, m5, m6
> +        vpshufb          m2, m3, m6

You could make these two pshufb use xmm regs, since you don't care
what's in the upper 128 bits.

> +        movd      [dstq+xq], xm1
> +        movd    [dstq+xq+4], xm2
> +
> +        add   xq, mmsize / 4
> +        cmp   xq, widthq
> +        jl .loop
> +    RET
> +
> +INIT_YMM avx2
> +cglobal remap2_8bit_line, 7, 9, 9, dst, width, src, in_linesize, u, v, ker, x, temp
> +    movsxdifnidn widthq, widthd
> +    movsxdifnidn in_linesizeq, in_linesized

Same as above.

> +    xor             xq, xq
> +    movd           xm0, in_linesized
> +    VBROADCASTI128  m7, [pb_255]

Also pcmpeqw m4, m4

> +    vpbroadcastd    m0, xm0
> +    movd           xm6, [pd_255]
> +    vpbroadcastd    m6, xm6

VBROADCASTI128 m6, [pd_255]

> +
> +    .loop:
> +        vpmovsxwd  m1, [kerq + xq * 8]
> +        vpmovsxwd  m2, [vq + xq * 8]
> +        vpmovsxwd  m3, [uq + xq * 8]
> +
> +        vpmulld         m4, m2, m0
> +        vpaddd          m4, m3
> +        mova            m3, m7

Also pointless mova. Use m7 below.

> +        vpgatherdd      m5, [srcq + m4], m3
> +        vpand           m5, m6
> +        vpmulld         m5, m1
> +        vphaddd         m2, m5, m5
> +        vphaddd         m5, m2, m2
> +        vpsrld          m5, m5, 0xd
> +        vextracti128   xm3, m5, 1
> +        vpshufb         m5, m5, [pb_mask]
> +        vpshufb         m3, m3, [pb_mask]

Why aren't you loading the mask at the beginning into some register?

> +
> +        movd          tempd, xm5
> +        mov       [dstq+xq], tempb
> +        movd          tempd, xm3
> +        mov     [dstq+xq+1], tempb

did you try pextrb [mem], xm, 0?

> +
> +        add   xq, mmsize / 16
> +        cmp   xq, widthq
> +        jl .loop
> +    RET
> +%endif
> diff --git a/libavfilter/x86/vf_v360_init.c b/libavfilter/x86/vf_v360_init.c
> new file mode 100644
> index 0000000000..e48ee307b3
> --- /dev/null
> +++ b/libavfilter/x86/vf_v360_init.c
> @@ -0,0 +1,43 @@
> +/*
> + * 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 "config.h"
> +
> +#include "libavutil/attributes.h"
> +#include "libavutil/cpu.h"
> +#include "libavutil/x86/cpu.h"
> +#include "libavfilter/v360.h"
> +
> +void ff_remap1_8bit_line_avx2(uint8_t *dst, int width, const uint8_t *src, ptrdiff_t in_linesize,
> +                              const uint16_t *u, const uint16_t *v, const int16_t *ker);
> +
> +void ff_remap2_8bit_line_avx2(uint8_t *dst, int width, const uint8_t *src, ptrdiff_t in_linesize,
> +                              const uint16_t *u, const uint16_t *v, const int16_t *ker);
> +
> +av_cold void ff_v360_init_x86(V360Context *s, int depth)
> +{
> +#if ARCH_X86_64
> +    int cpu_flags = av_get_cpu_flags();
> +
> +    if (EXTERNAL_AVX2(cpu_flags) && s->interp == NEAREST && depth <= 8)
> +        s->remap_line = ff_remap1_8bit_line_avx2;
> +
> +    if (EXTERNAL_AVX2(cpu_flags) && s->interp == BILINEAR && depth <= 8)
> +        s->remap_line = ff_remap2_8bit_line_avx2;

EXTERNAL_AVX2_FAST() for both.

> +#endif
> +}



More information about the ffmpeg-devel mailing list