[FFmpeg-devel] [PATCH] avfilter/vf_maskedmerge: add SIMD for maskedmerge with 8 bit depth input

Paul B Mahol onemda at gmail.com
Thu Oct 1 20:42:12 CEST 2015


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavfilter/maskedmerge.h             | 40 ++++++++++++++++++++
 libavfilter/vf_maskedmerge.c          | 59 ++++++++++++------------------
 libavfilter/x86/Makefile              |  2 +
 libavfilter/x86/vf_maskedmerge.asm    | 69 +++++++++++++++++++++++++++++++++++
 libavfilter/x86/vf_maskedmerge_init.c | 40 ++++++++++++++++++++
 5 files changed, 175 insertions(+), 35 deletions(-)
 create mode 100644 libavfilter/maskedmerge.h
 create mode 100644 libavfilter/x86/vf_maskedmerge.asm
 create mode 100644 libavfilter/x86/vf_maskedmerge_init.c

diff --git a/libavfilter/maskedmerge.h b/libavfilter/maskedmerge.h
new file mode 100644
index 0000000..b47a816
--- /dev/null
+++ b/libavfilter/maskedmerge.h
@@ -0,0 +1,40 @@
+/*
+ * 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 "avfilter.h"
+#include "framesync.h"
+
+typedef struct MaskedMergeContext {
+    const AVClass *class;
+    int width[4], height[4];
+    int nb_planes;
+    int planes;
+    int half, depth;
+    FFFrameSync fs;
+
+    void (*maskedmerge)(const uint8_t *bsrc, const uint8_t *osrc,
+                        const uint8_t *msrc, uint8_t *dst,
+                        ptrdiff_t blinesize, ptrdiff_t olinesize,
+                        ptrdiff_t mlinesize, ptrdiff_t dlinesize,
+                        int w, int h,
+                        int half, int shift);
+} MaskedMergeContext;
+
+void ff_maskedmerge_init_x86(MaskedMergeContext *s);
diff --git a/libavfilter/vf_maskedmerge.c b/libavfilter/vf_maskedmerge.c
index 41bd039..aa11d31 100644
--- a/libavfilter/vf_maskedmerge.c
+++ b/libavfilter/vf_maskedmerge.c
@@ -23,24 +23,9 @@
 #include "libavutil/opt.h"
 #include "avfilter.h"
 #include "formats.h"
-#include "framesync.h"
 #include "internal.h"
 #include "video.h"
-
-typedef struct MaskedMergeContext {
-    const AVClass *class;
-    int width[4], height[4];
-    int nb_planes;
-    int planes;
-    int max, half, depth;
-    FFFrameSync fs;
-
-    void (*maskedmerge)(const uint8_t *bsrc, int blinesize,
-                        const uint8_t *osrc, int olinesize,
-                        const uint8_t *msrc, int mlinesize,
-                        uint8_t *dst, int dlinesize, int w, int h,
-                        int max, int half, int shift);
-} MaskedMergeContext;
+#include "maskedmerge.h"
 
 #define OFFSET(x) offsetof(MaskedMergeContext, x)
 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
@@ -109,12 +94,12 @@ static int process_frame(FFFrameSync *fs)
                 continue;
             }
 
-            s->maskedmerge(base->data[p], base->linesize[p],
-                           overlay->data[p], overlay->linesize[p],
-                           mask->data[p], mask->linesize[p],
-                           out->data[p], out->linesize[p],
+            s->maskedmerge(base->data[p], overlay->data[p],
+                           mask->data[p], out->data[p],
+                           base->linesize[p], overlay->linesize[p],
+                           mask->linesize[p], out->linesize[p],
                            s->width[p], s->height[p],
-                           s->max, s->half, s->depth);
+                           s->half, s->depth);
         }
     }
     out->pts = av_rescale_q(base->pts, s->fs.time_base, outlink->time_base);
@@ -122,17 +107,18 @@ static int process_frame(FFFrameSync *fs)
     return ff_filter_frame(outlink, out);
 }
 
-static void maskedmerge8(const uint8_t *bsrc, int blinesize,
-                         const uint8_t *osrc, int olinesize,
-                         const uint8_t *msrc, int mlinesize,
-                         uint8_t *dst, int dlinesize, int w, int h,
-                         int max, int half, int shift)
+static void maskedmerge8(const uint8_t *bsrc, const uint8_t *osrc,
+                         const uint8_t *msrc, uint8_t *dst,
+                         ptrdiff_t blinesize, ptrdiff_t olinesize,
+                         ptrdiff_t mlinesize, ptrdiff_t dlinesize,
+                         int w, int h,
+                         int half, int shift)
 {
     int x, y;
 
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
-            dst[x] = ((256 - msrc[x]) * bsrc[x] + msrc[x] * osrc[x] + 128) >> 8;
+            dst[x] = bsrc[x] + ((msrc[x] * (osrc[x] - bsrc[x]) + 128) >> 8);
         }
 
         dst  += dlinesize;
@@ -142,11 +128,12 @@ static void maskedmerge8(const uint8_t *bsrc, int blinesize,
     }
 }
 
-static void maskedmerge16(const uint8_t *bbsrc, int blinesize,
-                          const uint8_t *oosrc, int olinesize,
-                          const uint8_t *mmsrc, int mlinesize,
-                          uint8_t *ddst, int dlinesize, int w, int h,
-                          int max, int half, int shift)
+static void maskedmerge16(const uint8_t *bbsrc, const uint8_t *oosrc,
+                          const uint8_t *mmsrc, uint8_t *ddst,
+                          ptrdiff_t blinesize, ptrdiff_t olinesize,
+                          ptrdiff_t mlinesize, ptrdiff_t dlinesize,
+                          int w, int h,
+                          int half, int shift)
 {
     const uint16_t *bsrc = (const uint16_t *)bbsrc;
     const uint16_t *osrc = (const uint16_t *)oosrc;
@@ -156,7 +143,7 @@ static void maskedmerge16(const uint8_t *bbsrc, int blinesize,
 
     for (y = 0; y < h; y++) {
         for (x = 0; x < w; x++) {
-            dst[x] = ((max - msrc[x]) * bsrc[x] + msrc[x] * osrc[x] + half) >> shift;
+            dst[x] = bsrc[x] + ((msrc[x] * (osrc[x] - bsrc[x]) + half) >> shift);
         }
 
         dst  += dlinesize / 2;
@@ -183,14 +170,16 @@ static int config_input(AVFilterLink *inlink)
     s->width[0]  = s->width[3]  = inlink->w;
 
     s->depth = desc->comp[0].depth;
-    s->max = 1 << s->depth;
-    s->half = s->max / 2;
+    s->half = (1 << s->depth) / 2;
 
     if (desc->comp[0].depth == 8)
         s->maskedmerge = maskedmerge8;
     else
         s->maskedmerge = maskedmerge16;
 
+    if (ARCH_X86)
+        ff_maskedmerge_init_x86(s);
+
     return 0;
 }
 
diff --git a/libavfilter/x86/Makefile b/libavfilter/x86/Makefile
index 5382027..db072cd 100644
--- a/libavfilter/x86/Makefile
+++ b/libavfilter/x86/Makefile
@@ -4,6 +4,7 @@ OBJS-$(CONFIG_GRADFUN_FILTER)                += x86/vf_gradfun_init.o
 OBJS-$(CONFIG_HQDN3D_FILTER)                 += x86/vf_hqdn3d_init.o
 OBJS-$(CONFIG_IDET_FILTER)                   += x86/vf_idet_init.o
 OBJS-$(CONFIG_INTERLACE_FILTER)              += x86/vf_interlace_init.o
+OBJS-$(CONFIG_MASKEDMERGE_FILTER)            += x86/vf_maskedmerge_init.o
 OBJS-$(CONFIG_NOISE_FILTER)                  += x86/vf_noise.o
 OBJS-$(CONFIG_PP7_FILTER)                    += x86/vf_pp7_init.o
 OBJS-$(CONFIG_PSNR_FILTER)                   += x86/vf_psnr_init.o
@@ -20,6 +21,7 @@ YASM-OBJS-$(CONFIG_GRADFUN_FILTER)           += x86/vf_gradfun.o
 YASM-OBJS-$(CONFIG_HQDN3D_FILTER)            += x86/vf_hqdn3d.o
 YASM-OBJS-$(CONFIG_IDET_FILTER)              += x86/vf_idet.o
 YASM-OBJS-$(CONFIG_INTERLACE_FILTER)         += x86/vf_interlace.o
+YASM-OBJS-$(CONFIG_MASKEDMERGE_FILTER)       += x86/vf_maskedmerge.o
 YASM-OBJS-$(CONFIG_PP7_FILTER)               += x86/vf_pp7.o
 YASM-OBJS-$(CONFIG_PSNR_FILTER)              += x86/vf_psnr.o
 YASM-OBJS-$(CONFIG_PULLUP_FILTER)            += x86/vf_pullup.o
diff --git a/libavfilter/x86/vf_maskedmerge.asm b/libavfilter/x86/vf_maskedmerge.asm
new file mode 100644
index 0000000..dac3746
--- /dev/null
+++ b/libavfilter/x86/vf_maskedmerge.asm
@@ -0,0 +1,69 @@
+;*****************************************************************************
+;* x86-optimized functions for maskedmerge filter
+;*
+;* 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"
+
+%if ARCH_X86_64
+SECTION_RODATA
+
+pw_128: times 8 dw 128
+pw_256: times 8 dw 256
+
+SECTION .text
+
+INIT_XMM sse2
+cglobal maskedmerge8, 10, 11, 7, 0, bsrc, osrc, msrc, dst, blinesize, olinesize, mlinesize, dlinesize, w, h
+    mova m5, [pw_128]
+    mova m2, [pw_256]
+    pxor m6, m6
+.nextrow:
+    mov r10q, 0
+    %define x r10q
+
+    .loop:
+        movh m0, [bsrcq + x]
+        movh m1, [osrcq + x]
+        movh m3, [msrcq + x]
+        mova m4, m2
+        punpcklbw m0, m6
+        punpcklbw m1, m6
+        punpcklbw m3, m6
+        psubw m4, m3
+        pmullw m4, m0
+        pmullw m1, m3
+        paddw m1, m4
+        paddw m1, m5
+        psrlw m1, 8
+        packuswb m1, m1
+        movh [dstq + x], m1
+        add r10q, mmsize / 2
+        cmp r10q, wq
+    jl .loop
+
+    lea bsrcq, [bsrcq+blinesizeq]
+    lea osrcq, [osrcq+olinesizeq]
+    lea msrcq, [msrcq+mlinesizeq]
+    lea dstq, [dstq+dlinesizeq]
+    sub hd, 1
+    jg .nextrow
+REP_RET
+%endif
diff --git a/libavfilter/x86/vf_maskedmerge_init.c b/libavfilter/x86/vf_maskedmerge_init.c
new file mode 100644
index 0000000..4435700
--- /dev/null
+++ b/libavfilter/x86/vf_maskedmerge_init.c
@@ -0,0 +1,40 @@
+/*
+ * 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/attributes.h"
+#include "libavutil/cpu.h"
+#include "libavutil/x86/cpu.h"
+#include "libavfilter/maskedmerge.h"
+
+void ff_maskedmerge8_sse2(const uint8_t *bsrc, const uint8_t *osrc,
+                          const uint8_t *msrc, uint8_t *dst,
+                          ptrdiff_t blinesize, ptrdiff_t olinesize,
+                          ptrdiff_t mlinesize, ptrdiff_t dlinesize,
+                          int w, int h,
+                          int half, int shift);
+
+av_cold void ff_maskedmerge_init_x86(MaskedMergeContext *s)
+{
+    int cpu_flags = av_get_cpu_flags();
+
+    if (ARCH_X86_64 && EXTERNAL_SSE2(cpu_flags) && s->depth == 8) {
+        s->maskedmerge = ff_maskedmerge8_sse2;
+    }
+}
-- 
1.9.1



More information about the ffmpeg-devel mailing list