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

James Almer jamrial at gmail.com
Thu Sep 5 20:17:27 EEST 2019


On 9/5/2019 1:57 PM, Paul B Mahol wrote:
> On 9/5/19, James Almer <jamrial at gmail.com> wrote:
>> On 9/5/2019 5:52 AM, Paul B Mahol wrote:
>>> diff --git a/libavfilter/x86/vf_v360.asm b/libavfilter/x86/vf_v360.asm
>>> new file mode 100644
>>> index 0000000000..e1efe2e3a3
>>> --- /dev/null
>>> +++ b/libavfilter/x86/vf_v360.asm
>>> @@ -0,0 +1,98 @@
>>> +;*****************************************************************************
>>> +;* 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
>>
>> No need for 32 byte alignment. And if there was, you should do
>> SECTION_RODATA 32 instead.
> Removed.
> 
>>
>>> +
>>> +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
>>> +
>>> +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, 6, 7, 7, dst, width, src, in_linesize, u, v, x
>>> +    movsxdifnidn widthq, widthd
>>> +    xor             xq, xq
>>> +    movd           xm0, in_linesized
>>> +    pcmpeqw         m4, m4
>>> +    VBROADCASTI128  m6, [pb_mask]
>>> +    vpbroadcastd    m0, xm0
>>> +
>>> +    .loop:
>>> +        pmovsxwd   m1, [vq + xq * 2]
>>> +        pmovsxwd   m2, [uq + xq * 2]
>>> +
>>> +        pmulld           m1, m0
>>
>> Use pmaddwd as Hendrik suggested.
> 
> I can not use it, linesize is int.

It is int, but as long as its value fits on a signed word, it should
work with no extra changes.

> 
>>
>>> +        paddd            m1, m2
>>> +        mova             m2, m4
>>> +        vpgatherdd       m5, [srcq + m1], m2
>>> +        pshufb           m1, m5, m6
>>> +        vextracti128    xm2, m1, 1
>>> +        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, 8, 8, dst, width, src, in_linesize, u, v,
>>> ker, x
>>> +    movsxdifnidn widthq, widthd
>>> +    xor             xq, xq
>>> +    movd           xm0, in_linesized
>>
>>     movd           xm0, in_linesized
>> %if ARCH_X86_32
>> DEFINE_ARGS dst, width, src, x, u, v, ker
>> %endif
>>     xor             xq, xq
>>
>> That way this function will work on x86_32. Otherwise, seeing you didn't
>> add a preprocessor wrapper, assembly will fail on those targets.
> 
> I will left x32 to ones that cares.

I literally just gave you the code to copy and paste. And if you don't
want to paste those lines, you'll need to wrap this entire file in a
x86_64 check to prevent breaking x86_32 builds.

> 
>>
>>> +    pcmpeqw         m7, m7
>>> +    vpbroadcastd    m0, xm0
>>> +    movd           xm6, [pd_255]
>>> +    vpbroadcastd    m6, xm6
>>> +    VBROADCASTI128  m5, [pb_mask]
>>> +
>>> +    .loop:
>>> +        pmovsxwd   m1, [kerq + xq * 8]
>>> +        pmovsxwd   m2, [vq + xq * 8]
>>> +        pmovsxwd   m3, [uq + xq * 8]
>>> +
>>> +        pmulld          m4, m2, m0
>>> +        paddd           m4, m3
>>> +        mova            m3, m7
>>> +        vpgatherdd      m2, [srcq + m4], m3
>>> +        pand            m2, m6
>>> +        pmulld          m2, m1
>>
>> Same for these two pmulld.
> 
> Already explained.
> 
>>
>>> +        phaddd          m2, m2
>>> +        phaddd          m1, m2, m2
>>> +        psrld           m1, m1, 0xd
>>> +        pshufb          m1, m1, m5
>>> +        vextracti128   xm2, m1, 1
>>> +
>>> +        pextrb    [dstq+xq], xm1, 0
>>> +        pextrb  [dstq+xq+1], xm2, 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..b781fb13d1
>>> --- /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
>>
>> You can remove this after adding the above.
> 
> Already explained.
> 
>>
>>> +    int cpu_flags = av_get_cpu_flags();
>>> +
>>> +    if (EXTERNAL_AVX2_FAST(cpu_flags) && s->interp == NEAREST && depth <=
>>> 8)
>>> +        s->remap_line = ff_remap1_8bit_line_avx2;
>>> +
>>> +    if (EXTERNAL_AVX2_FAST(cpu_flags) && s->interp == BILINEAR && depth
>>> <= 8)
>>> +        s->remap_line = ff_remap2_8bit_line_avx2;
>>> +#endif
>>> +}
>>
> 
> I plan to apply this soon.
> 
>>
>> _______________________________________________
>> 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".
> _______________________________________________
> 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