From patchwork Sat Jan 20 00:43:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marton Balint X-Patchwork-Id: 7351 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.156.27 with SMTP id q27csp154328jak; Fri, 19 Jan 2018 16:43:39 -0800 (PST) X-Google-Smtp-Source: AH8x226VyT5tnO63tlznfYQOGYlO3pCenSP7u8EBpYvB9lD1uPkqKx8GynhIz3KisK4GmDFx+6th X-Received: by 10.223.138.178 with SMTP id y47mr182018wry.257.1516409019751; Fri, 19 Jan 2018 16:43:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516409019; cv=none; d=google.com; s=arc-20160816; b=fI39b5AWlaeHSXsT2QliMi8x9M5pti7FI06CWEtTIcsiLdTlehjwcW+7K1hhGl8Age I376dGktKHzJUyC+I+doSfGC0hN+85gUW6RpJx6iKCagEr8rPfKbqTNuvIvcLUa59xod hqiBgkdPnEWsfAnB6NpTDz4oitb8vN7e4xoxW6q2g3JX/uRLQiyHsJFNKb5lAaAhg4xB zmOv2JZaarw8FxpSsMQ27zT9ccT948pxjNNJ3u6PU2BWSVPmPS/6innREnNd0tjfJQgD 9NEkQQ3TgUZ+l2iq0BVORR895CsZMtOMm7aXTUcr6g756g/dZ+yhGknjbB3fKJHo/8ZH Am4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:delivered-to:arc-authentication-results; bh=pFwDX+8ykm/UBDDkflaSpJ3VDj38xLDQxeA16C0WVhc=; b=pdoSEMHpIHP7uM0PsL24gNYDoaP6IeKh6lUf6zOcFxemcJyhShaf9b+Karb/odGeP3 88OhPjYTJM2BfG6Y1nwJFTC9+WXjCv0Gi4aNkRf77YcCEZL9svC2+hTp6XvX6uz2i9g3 NxRNFqH7ETCwwdZU1wBKT3X4tZMttSoB1QVH2d5PJZKQ9tRfKtGEyLLevaW+ZK2aeQYI +Gu1gGqxUEj060Vv8gVv+t4g28EXcgSueeE0VF4kZLzbaJdqqhtco/ZJOpBwHN77+02X tMaRqR2ITKRPeFpgEiSm5qx2ofT8t+NhzAgo0C35bk7rrDQrupOBsReIiL6pRpq1VCPW vp/Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id h90si8946269wrh.537.2018.01.19.16.43.39; Fri, 19 Jan 2018 16:43:39 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4E31468990D; Sat, 20 Jan 2018 02:43:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from iq.passwd.hu (iq.passwd.hu [217.27.212.140]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 259F7680B0E for ; Sat, 20 Jan 2018 02:43:31 +0200 (EET) Received: from localhost (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id D2BD2E156C; Sat, 20 Jan 2018 01:43:31 +0100 (CET) X-Virus-Scanned: amavisd-new at passwd.hu Received: from iq.passwd.hu ([127.0.0.1]) by localhost (iq.passwd.hu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZEd0u-EUmuf7; Sat, 20 Jan 2018 01:43:30 +0100 (CET) Received: from bluegene.passwd.hu (localhost [127.0.0.1]) by iq.passwd.hu (Postfix) with ESMTP id 5CD7BE0E11; Sat, 20 Jan 2018 01:43:30 +0100 (CET) From: Marton Balint To: ffmpeg-devel@ffmpeg.org Date: Sat, 20 Jan 2018 01:43:26 +0100 Message-Id: <20180120004326.25121-1-cus@passwd.hu> X-Mailer: git-send-email 2.13.6 In-Reply-To: References: Subject: [FFmpeg-devel] [PATCHv3 4/4] avfilter/vf_framerate: add SIMD functions for frame blending X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marton Balint MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Blend function speedups on x86_64 Core i5 4460: ffmpeg -f lavfi -i allyuv -vf framerate=60:threads=1 -f null none C: 447548411 decicycles in Blend, 2048 runs, 0 skips SSSE3: 130020087 decicycles in Blend, 2048 runs, 0 skips AVX2: 128508221 decicycles in Blend, 2048 runs, 0 skips ffmpeg -f lavfi -i allyuv -vf format=yuv420p12,framerate=60:threads=1 -f null none C: 228932745 decicycles in Blend, 2048 runs, 0 skips SSE4: 123357781 decicycles in Blend, 2048 runs, 0 skips AVX2: 121215353 decicycles in Blend, 2048 runs, 0 skips Signed-off-by: Marton Balint --- libavfilter/framerate.h | 74 ++++++++++++++++++++ libavfilter/vf_framerate.c | 70 +++++-------------- libavfilter/x86/Makefile | 2 + libavfilter/x86/vf_framerate.asm | 134 ++++++++++++++++++++++++++++++++++++ libavfilter/x86/vf_framerate_init.c | 43 ++++++++++++ 5 files changed, 269 insertions(+), 54 deletions(-) create mode 100644 libavfilter/framerate.h create mode 100644 libavfilter/x86/vf_framerate.asm create mode 100644 libavfilter/x86/vf_framerate_init.c diff --git a/libavfilter/framerate.h b/libavfilter/framerate.h new file mode 100644 index 0000000000..d6b6f5f649 --- /dev/null +++ b/libavfilter/framerate.h @@ -0,0 +1,74 @@ +/* + * 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 + */ + +#ifndef AVFILTER_FRAMERATE_H +#define AVFILTER_FRAMERATE_H + +#include "libavutil/pixelutils.h" +#include "avfilter.h" + +#define BLEND_FUNC_PARAMS const uint8_t *src1, ptrdiff_t src1_linesize, \ + const uint8_t *src2, ptrdiff_t src2_linesize, \ + uint8_t *dst, ptrdiff_t dst_linesize, \ + ptrdiff_t width, ptrdiff_t height, \ + int factor1, int factor2, int half + +#define BLEND_FACTOR_DEPTH8 7 +#define BLEND_FACTOR_DEPTH16 15 + +typedef void (*blend_func)(BLEND_FUNC_PARAMS); + +typedef struct FrameRateContext { + const AVClass *class; + // parameters + AVRational dest_frame_rate; ///< output frames per second + int flags; ///< flags affecting frame rate conversion algorithm + double scene_score; ///< score that denotes a scene change has happened + int interp_start; ///< start of range to apply linear interpolation + int interp_end; ///< end of range to apply linear interpolation + + int line_size[4]; ///< bytes of pixel data per line for each plane + int vsub; + + AVRational srce_time_base; ///< timebase of source + AVRational dest_time_base; ///< timebase of destination + + av_pixelutils_sad_fn sad; ///< Sum of the absolute difference function (scene detect only) + double prev_mafd; ///< previous MAFD (scene detect only) + + int blend_factor_max; + int bitdepth; + AVFrame *work; + + AVFrame *f0; ///< last frame + AVFrame *f1; ///< current frame + int64_t pts0; ///< last frame pts in dest_time_base + int64_t pts1; ///< current frame pts in dest_time_base + int64_t delta; ///< pts1 to pts0 delta + double score; ///< scene change score (f0 to f1) + int flush; ///< 1 if the filter is being flushed + int64_t start_pts; ///< pts of the first output frame + int64_t n; ///< output frame counter + + blend_func blend; +} FrameRateContext; + +void ff_framerate_init(FrameRateContext *s); +void ff_framerate_init_x86(FrameRateContext *s); + +#endif /* AVFILTER_BLEND_H */ diff --git a/libavfilter/vf_framerate.c b/libavfilter/vf_framerate.c index d315ef5d09..a1684e1b15 100644 --- a/libavfilter/vf_framerate.c +++ b/libavfilter/vf_framerate.c @@ -38,52 +38,7 @@ #include "avfilter.h" #include "internal.h" #include "video.h" - -#define BLEND_FUNC_PARAMS const uint8_t *src1, ptrdiff_t src1_linesize, \ - const uint8_t *src2, ptrdiff_t src2_linesize, \ - uint8_t *dst, ptrdiff_t dst_linesize, \ - ptrdiff_t width, ptrdiff_t height, \ - int factor1, int factor2, int half - -#define BLEND_FACTOR_DEPTH8 7 -#define BLEND_FACTOR_DEPTH16 15 - -typedef void (*blend_func)(BLEND_FUNC_PARAMS); - -typedef struct FrameRateContext { - const AVClass *class; - // parameters - AVRational dest_frame_rate; ///< output frames per second - int flags; ///< flags affecting frame rate conversion algorithm - double scene_score; ///< score that denotes a scene change has happened - int interp_start; ///< start of range to apply linear interpolation - int interp_end; ///< end of range to apply linear interpolation - - int line_size[4]; ///< bytes of pixel data per line for each plane - int vsub; - - AVRational srce_time_base; ///< timebase of source - AVRational dest_time_base; ///< timebase of destination - - av_pixelutils_sad_fn sad; ///< Sum of the absolute difference function (scene detect only) - double prev_mafd; ///< previous MAFD (scene detect only) - - int blend_factor_max; - int bitdepth; - AVFrame *work; - - AVFrame *f0; ///< last frame - AVFrame *f1; ///< current frame - int64_t pts0; ///< last frame pts in dest_time_base - int64_t pts1; ///< current frame pts in dest_time_base - int64_t delta; ///< pts1 to pts0 delta - double score; ///< scene change score (f0 to f1) - int flush; ///< 1 if the filter is being flushed - int64_t start_pts; ///< pts of the first output frame - int64_t n; ///< output frame counter - - blend_func blend; -} FrameRateContext; +#include "framerate.h" #define OFFSET(x) offsetof(FrameRateContext, x) #define V AV_OPT_FLAG_VIDEO_PARAM @@ -246,7 +201,7 @@ static int blend_frames(AVFilterContext *ctx, int interpolate) av_frame_copy_props(s->work, s->f0); ff_dlog(ctx, "blend_frames() INTERPOLATE to create work frame\n"); - ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx))); + ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(FFMAX(1, outlink->h >> 2), ff_filter_get_nb_threads(ctx))); return 1; } return 0; @@ -366,6 +321,19 @@ static void blend_frames16_c(BLEND_FUNC_PARAMS) } } +void ff_framerate_init(FrameRateContext *s) +{ + if (s->bitdepth == 8) { + s->blend_factor_max = 1 << BLEND_FACTOR_DEPTH8; + s->blend = blend_frames_c; + } else { + s->blend_factor_max = 1 << BLEND_FACTOR_DEPTH16; + s->blend = blend_frames16_c; + } + if (ARCH_X86) + ff_framerate_init_x86(s); +} + static int config_input(AVFilterLink *inlink) { AVFilterContext *ctx = inlink->dst; @@ -387,13 +355,7 @@ static int config_input(AVFilterLink *inlink) s->srce_time_base = inlink->time_base; - if (s->bitdepth == 8) { - s->blend_factor_max = 1 << BLEND_FACTOR_DEPTH8; - s->blend = blend_frames_c; - } else { - s->blend_factor_max = 1 << BLEND_FACTOR_DEPTH16; - s->blend = blend_frames16_c; - } + ff_framerate_init(s); return 0; } diff --git a/libavfilter/x86/Makefile b/libavfilter/x86/Makefile index 2fc5c62644..4d4c5e503a 100644 --- a/libavfilter/x86/Makefile +++ b/libavfilter/x86/Makefile @@ -5,6 +5,7 @@ OBJS-$(CONFIG_COLORSPACE_FILTER) += x86/colorspacedsp_init.o OBJS-$(CONFIG_EQ_FILTER) += x86/vf_eq.o OBJS-$(CONFIG_FSPP_FILTER) += x86/vf_fspp_init.o OBJS-$(CONFIG_GRADFUN_FILTER) += x86/vf_gradfun_init.o +OBJS-$(CONFIG_FRAMERATE_FILTER) += x86/vf_framerate_init.o OBJS-$(CONFIG_HFLIP_FILTER) += x86/vf_hflip_init.o OBJS-$(CONFIG_HQDN3D_FILTER) += x86/vf_hqdn3d_init.o OBJS-$(CONFIG_IDET_FILTER) += x86/vf_idet_init.o @@ -31,6 +32,7 @@ X86ASM-OBJS-$(CONFIG_AFIR_FILTER) += x86/af_afir.o X86ASM-OBJS-$(CONFIG_BLEND_FILTER) += x86/vf_blend.o X86ASM-OBJS-$(CONFIG_BWDIF_FILTER) += x86/vf_bwdif.o X86ASM-OBJS-$(CONFIG_COLORSPACE_FILTER) += x86/colorspacedsp.o +X86ASM-OBJS-$(CONFIG_FRAMERATE_FILTER) += x86/vf_framerate.o X86ASM-OBJS-$(CONFIG_FSPP_FILTER) += x86/vf_fspp.o X86ASM-OBJS-$(CONFIG_GRADFUN_FILTER) += x86/vf_gradfun.o X86ASM-OBJS-$(CONFIG_HFLIP_FILTER) += x86/vf_hflip.o diff --git a/libavfilter/x86/vf_framerate.asm b/libavfilter/x86/vf_framerate.asm new file mode 100644 index 0000000000..7a30c870bd --- /dev/null +++ b/libavfilter/x86/vf_framerate.asm @@ -0,0 +1,134 @@ +;***************************************************************************** +;* x86-optimized functions for framerate filter +;* +;* Copyright (C) 2018 Marton Balint +;* +;* Based on vf_blend.asm, Copyright (C) 2015 Paul B Mahol +;* +;* 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 .text + + +%macro XSPLAT 3 +%if cpuflag(avx2) + vpbroadcast%3 %1, %2 +%else + movd %1, %2 +%ifidn %3, d + SPLATD %1 +%else + SPLATW %1, %1 +%endif +%endif +%endmacro + + +%macro BLEND_INIT 0-1 +%if ARCH_X86_64 +cglobal blend_frames%1, 6, 9, 5, src1, src1_linesize, src2, src2_linesize, dst, dst_linesize, width, end, x + mov widthd, dword widthm +%else +cglobal blend_frames%1, 5, 7, 5, src1, src1_linesize, src2, src2_linesize, dst, end, x +%define dst_linesizeq r5mp +%define widthq r6mp +%endif + mov endd, dword r7m + add src1q, widthq + add src2q, widthq + add dstq, widthq + neg widthq +%endmacro + + +%macro BLEND_LOOP 4 +.nextrow: + mov xq, widthq + + .loop: + movu m0, [src1q + xq] + movu m1, [src2q + xq] + SBUTTERFLY %1%2, 0, 1, 4 ; aAbBcCdD + ; eEfFgGhH + pmadd%3 m0, m2 + pmadd%3 m1, m2 + + padd%2 m0, m3 + padd%2 m1, m3 + psrl%2 m0, %4 ; 0A0B0C0D + psrl%2 m1, %4 ; 0E0F0G0H + + packus%2%1 m0, m1 ; ABCDEFGH + movu [dstq + xq], m0 + add xq, mmsize + jl .loop + add src1q, src1_linesizeq + add src2q, src2_linesizeq + add dstq, dst_linesizeq + sub endd, 1 + jg .nextrow +REP_RET +%endmacro + + +%macro BLEND_FRAMES 0 + BLEND_INIT + + XSPLAT m2, r8m, w ; factor1 + XSPLAT m3, r9m, w ; factor2 + + psllw m3, 8 + por m2, m3 ; interleaved factors + + XSPLAT m3, r10m, w ; half + + BLEND_LOOP b, w, ubsw, 7 +%endmacro + + +%macro BLEND_FRAMES16 0 + BLEND_INIT 16 + + XSPLAT m2, r8m, d ; factor1 + XSPLAT m3, r9m, d ; factor2 + + pslld m3, 16 + por m2, m3 ; interleaved factors + + XSPLAT m3, r10m, d ; half + + BLEND_LOOP w, d, wd, 15 +%endmacro + + +INIT_XMM ssse3 +BLEND_FRAMES + +INIT_XMM sse4 +BLEND_FRAMES16 + + +%if HAVE_AVX2_EXTERNAL + +INIT_YMM avx2 +BLEND_FRAMES +BLEND_FRAMES16 + +%endif diff --git a/libavfilter/x86/vf_framerate_init.c b/libavfilter/x86/vf_framerate_init.c new file mode 100644 index 0000000000..72705e6a49 --- /dev/null +++ b/libavfilter/x86/vf_framerate_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 "libavutil/cpu.h" +#include "libavutil/x86/cpu.h" +#include "libavfilter/framerate.h" + +void ff_blend_frames_ssse3(BLEND_FUNC_PARAMS); +void ff_blend_frames_avx2(BLEND_FUNC_PARAMS); +void ff_blend_frames16_sse4(BLEND_FUNC_PARAMS); +void ff_blend_frames16_avx2(BLEND_FUNC_PARAMS); + +void ff_framerate_init_x86(FrameRateContext *s) +{ + int cpu_flags = av_get_cpu_flags(); + if (s->bitdepth == 8) { + if (EXTERNAL_AVX2_FAST(cpu_flags)) + s->blend = ff_blend_frames_avx2; + else if (EXTERNAL_SSSE3(cpu_flags)) + s->blend = ff_blend_frames_ssse3; + } else { + if (EXTERNAL_AVX2_FAST(cpu_flags)) + s->blend = ff_blend_frames16_avx2; + else if (EXTERNAL_SSE4(cpu_flags)) + s->blend = ff_blend_frames16_sse4; + } +} +