[FFmpeg-devel] [PATCH] avfilter/vf_stereo3d: add x86 SIMD for anaglyph outputs

Paul B Mahol onemda at gmail.com
Mon Oct 5 11:49:36 CEST 2015


Signed-off-by: Paul B Mahol <onemda at gmail.com>
---
 libavfilter/stereo3d.h             |  36 ++++++++
 libavfilter/vf_stereo3d.c          |  70 +++++++-------
 libavfilter/x86/Makefile           |   2 +
 libavfilter/x86/vf_stereo3d.asm    | 184 +++++++++++++++++++++++++++++++++++++
 libavfilter/x86/vf_stereo3d_init.c |  37 ++++++++
 5 files changed, 297 insertions(+), 32 deletions(-)
 create mode 100644 libavfilter/stereo3d.h
 create mode 100644 libavfilter/x86/vf_stereo3d.asm
 create mode 100644 libavfilter/x86/vf_stereo3d_init.c

diff --git a/libavfilter/stereo3d.h b/libavfilter/stereo3d.h
new file mode 100644
index 0000000..e7a8456
--- /dev/null
+++ b/libavfilter/stereo3d.h
@@ -0,0 +1,36 @@
+/*
+ * 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
+ */
+
+#ifndef LIBAVFILTER_STEREO3D_H
+#define LIBAVFILTER_STEREO3D_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct Stereo3DDSPContext {
+    void (*anaglyph)(uint8_t *dst, uint8_t *lsrc, uint8_t *rsrc,
+                     ptrdiff_t dst_linesize, ptrdiff_t l_linesize, ptrdiff_t r_linesize,
+                     int width, int height,
+                     const int *ana_matrix_r, const int *ana_matrix_g, const int *ana_matrix_b);
+} Stereo3DDSPContext;
+
+void ff_stereo3d_init_x86(Stereo3DDSPContext *dsp);
+
+#endif /* LIBAVFILTER_STEREO3D_H */
diff --git a/libavfilter/vf_stereo3d.c b/libavfilter/vf_stereo3d.c
index f9d344a..8e9cc83 100644
--- a/libavfilter/vf_stereo3d.c
+++ b/libavfilter/vf_stereo3d.c
@@ -30,6 +30,7 @@
 #include "formats.h"
 #include "internal.h"
 #include "video.h"
+#include "stereo3d.h"
 
 enum StereoCode {
     ANAGLYPH_RC_GRAY,   // anaglyph red/cyan gray
@@ -150,6 +151,7 @@ typedef struct Stereo3DContext {
     double ts_unit;
     int blanks;
     int in_off_left[4], in_off_right[4];
+    Stereo3DDSPContext dsp;
 } Stereo3DContext;
 
 #define OFFSET(x) offsetof(Stereo3DContext, x)
@@ -300,6 +302,37 @@ static int query_formats(AVFilterContext *ctx)
     return ff_set_common_formats(ctx, fmts_list);
 }
 
+static inline uint8_t ana_convert(const int *coeff, const uint8_t *left, const uint8_t *right)
+{
+    int sum;
+
+    sum  = coeff[0] * left[0] + coeff[3] * right[0]; //red in
+    sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
+    sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
+
+    return av_clip_uint8(sum >> 16);
+}
+
+static void anaglyph(uint8_t *dst, uint8_t *lsrc, uint8_t *rsrc,
+                     ptrdiff_t dst_linesize, ptrdiff_t l_linesize, ptrdiff_t r_linesize,
+                     int width, int height,
+                     const int *ana_matrix_r, const int *ana_matrix_g, const int *ana_matrix_b)
+{
+    int x, y, o;
+
+    for (y = 0; y < height; y++) {
+        for (o = 0, x = 0; x < width; x++, o+= 3) {
+            dst[o    ] = ana_convert(ana_matrix_r, lsrc + o, rsrc + o);
+            dst[o + 1] = ana_convert(ana_matrix_g, lsrc + o, rsrc + o);
+            dst[o + 2] = ana_convert(ana_matrix_b, lsrc + o, rsrc + o);
+        }
+
+        dst  += dst_linesize;
+        lsrc += l_linesize;
+        rsrc += r_linesize;
+    }
+}
+
 static int config_output(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
@@ -517,38 +550,11 @@ static int config_output(AVFilterLink *outlink)
     s->hsub = desc->log2_chroma_w;
     s->vsub = desc->log2_chroma_h;
 
-    return 0;
-}
-
-static inline uint8_t ana_convert(const int *coeff, const uint8_t *left, const uint8_t *right)
-{
-    int sum;
-
-    sum  = coeff[0] * left[0] + coeff[3] * right[0]; //red in
-    sum += coeff[1] * left[1] + coeff[4] * right[1]; //green in
-    sum += coeff[2] * left[2] + coeff[5] * right[2]; //blue in
-
-    return av_clip_uint8(sum >> 16);
-}
+    s->dsp.anaglyph = anaglyph;
+    if (ARCH_X86)
+        ff_stereo3d_init_x86(&s->dsp);
 
-static void anaglyph(uint8_t *dst, uint8_t *lsrc, uint8_t *rsrc,
-                     ptrdiff_t dst_linesize, ptrdiff_t l_linesize, ptrdiff_t r_linesize,
-                     int width, int height,
-                     const int *ana_matrix_r, const int *ana_matrix_g, const int *ana_matrix_b)
-{
-    int x, y, o;
-
-    for (y = 0; y < height; y++) {
-        for (o = 0, x = 0; x < width; x++, o+= 3) {
-            dst[o    ] = ana_convert(ana_matrix_r, lsrc + o, rsrc + o);
-            dst[o + 1] = ana_convert(ana_matrix_g, lsrc + o, rsrc + o);
-            dst[o + 2] = ana_convert(ana_matrix_b, lsrc + o, rsrc + o);
-        }
-
-        dst  += dst_linesize;
-        lsrc += l_linesize;
-        rsrc += r_linesize;
-    }
+    return 0;
 }
 
 typedef struct ThreadData {
@@ -568,7 +574,7 @@ static int filter_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
     int end   = (height * (jobnr+1)) / nb_jobs;
     const int **ana_matrix = s->ana_matrix;
 
-    anaglyph(out->data[0] + out->linesize[0] * start,
+    s->dsp.anaglyph(out->data[0] + out->linesize[0] * start,
              ileft ->data[0] + s->in_off_left [0]  + ileft->linesize[0] * start * s->in.row_step,
              iright->data[0] + s->in_off_right[0] + iright->linesize[0] * start * s->in.row_step,
              out->linesize[0],
diff --git a/libavfilter/x86/Makefile b/libavfilter/x86/Makefile
index 57b413f..c024204 100644
--- a/libavfilter/x86/Makefile
+++ b/libavfilter/x86/Makefile
@@ -13,6 +13,7 @@ OBJS-$(CONFIG_PULLUP_FILTER)                 += x86/vf_pullup_init.o
 OBJS-$(CONFIG_REMOVEGRAIN_FILTER)            += x86/vf_removegrain_init.o
 OBJS-$(CONFIG_SPP_FILTER)                    += x86/vf_spp.o
 OBJS-$(CONFIG_SSIM_FILTER)                   += x86/vf_ssim_init.o
+OBJS-$(CONFIG_STEREO3D_FILTER)               += x86/vf_stereo3d_init.o
 OBJS-$(CONFIG_TBLEND_FILTER)                 += x86/vf_blend_init.o
 OBJS-$(CONFIG_TINTERLACE_FILTER)             += x86/vf_tinterlace_init.o
 OBJS-$(CONFIG_VOLUME_FILTER)                 += x86/af_volume_init.o
@@ -32,6 +33,7 @@ ifdef CONFIG_GPL
 YASM-OBJS-$(CONFIG_REMOVEGRAIN_FILTER)       += x86/vf_removegrain.o
 endif
 YASM-OBJS-$(CONFIG_SSIM_FILTER)              += x86/vf_ssim.o
+YASM-OBJS-$(CONFIG_STEREO3D_FILTER)          += x86/vf_stereo3d.o
 YASM-OBJS-$(CONFIG_TBLEND_FILTER)            += x86/vf_blend.o
 YASM-OBJS-$(CONFIG_TINTERLACE_FILTER)        += x86/vf_interlace.o
 YASM-OBJS-$(CONFIG_VOLUME_FILTER)            += x86/af_volume.o
diff --git a/libavfilter/x86/vf_stereo3d.asm b/libavfilter/x86/vf_stereo3d.asm
new file mode 100644
index 0000000..269004b
--- /dev/null
+++ b/libavfilter/x86/vf_stereo3d.asm
@@ -0,0 +1,184 @@
+;*****************************************************************************
+;* x86-optimized functions for stereo3d 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
+
+; rgbrgbrgbrgb
+; rrrrggggbbbb
+
+shuf: db 0, 4, 8, 1,5, 9, 2, 6,10,3, 7,11,-1,-1,-1,-1
+ex_r: db 0,-1,-1,-1,3,-1,-1,-1,6,-1,-1,-1, 9,-1,-1,-1
+ex_g: db 1,-1,-1,-1,4,-1,-1,-1,7,-1,-1,-1,10,-1,-1,-1
+ex_b: db 2,-1,-1,-1,5,-1,-1,-1,8,-1,-1,-1,11,-1,-1,-1
+
+SECTION .text
+
+INIT_XMM sse4
+cglobal anaglyph, 11, 13, 16, 3*6*mmsize, dst, lsrc, rsrc, dst_linesize, l_linesize, r_linesize, width, height, ana_matrix_r, ana_matrix_g, ana_matrix_b
+    movd                 m10, [ana_matrix_rq+ 0]
+    movd                 m11, [ana_matrix_rq+ 4]
+    movd                 m12, [ana_matrix_rq+ 8]
+    movd                 m13, [ana_matrix_rq+12]
+    movd                 m14, [ana_matrix_rq+16]
+    movd                 m15, [ana_matrix_rq+20]
+    pshufd               m10, m10, q0000
+    pshufd               m11, m11, q0000
+    pshufd               m12, m12, q0000
+    pshufd               m13, m13, q0000
+    pshufd               m14, m14, q0000
+    pshufd               m15, m15, q0000
+    mova      [rsp+mmsize*0], m10
+    mova      [rsp+mmsize*1], m11
+    mova      [rsp+mmsize*2], m12
+    mova      [rsp+mmsize*3], m13
+    mova      [rsp+mmsize*4], m14
+    mova      [rsp+mmsize*5], m15
+
+    movd                 m10, [ana_matrix_gq+ 0]
+    movd                 m11, [ana_matrix_gq+ 4]
+    movd                 m12, [ana_matrix_gq+ 8]
+    movd                 m13, [ana_matrix_gq+12]
+    movd                 m14, [ana_matrix_gq+16]
+    movd                 m15, [ana_matrix_gq+20]
+    pshufd               m10, m10, q0000
+    pshufd               m11, m11, q0000
+    pshufd               m12, m12, q0000
+    pshufd               m13, m13, q0000
+    pshufd               m14, m14, q0000
+    pshufd               m15, m15, q0000
+    mova     [rsp+mmsize*6 ], m10
+    mova     [rsp+mmsize*7 ], m11
+    mova     [rsp+mmsize*8 ], m12
+    mova     [rsp+mmsize*9 ], m13
+    mova     [rsp+mmsize*10], m14
+    mova     [rsp+mmsize*11], m15
+
+    movd                 m10, [ana_matrix_bq+ 0]
+    movd                 m11, [ana_matrix_bq+ 4]
+    movd                 m12, [ana_matrix_bq+ 8]
+    movd                 m13, [ana_matrix_bq+12]
+    movd                 m14, [ana_matrix_bq+16]
+    movd                 m15, [ana_matrix_bq+20]
+    pshufd               m10, m10, q0000
+    pshufd               m11, m11, q0000
+    pshufd               m12, m12, q0000
+    pshufd               m13, m13, q0000
+    pshufd               m14, m14, q0000
+    pshufd               m15, m15, q0000
+    mova     [rsp+mmsize*12], m10
+    mova     [rsp+mmsize*13], m11
+    mova     [rsp+mmsize*14], m12
+    mova     [rsp+mmsize*15], m13
+    mova     [rsp+mmsize*16], m14
+    mova     [rsp+mmsize*17], m15
+.nextrow:
+    mov       r11q, widthq
+    mov       r12q, 0
+    %define      o  r12q
+
+    .loop:
+        movu                 m0, [lsrcq+o+0]
+        pshufb               m1, m0, [ex_r]
+        pshufb               m2, m0, [ex_g]
+        pshufb               m3, m0, [ex_b]
+        movu                 m0, [rsrcq+o+0]
+        pshufb               m4, m0, [ex_r]
+        pshufb               m5, m0, [ex_g]
+        pshufb               m6, m0, [ex_b]
+        pmulld               m1, [rsp+mmsize*0]
+        pmulld               m2, [rsp+mmsize*1]
+        pmulld               m3, [rsp+mmsize*2]
+        pmulld               m4, [rsp+mmsize*3]
+        pmulld               m5, [rsp+mmsize*4]
+        pmulld               m6, [rsp+mmsize*5]
+        paddd                m1, m2
+        paddd                m1, m3
+        paddd                m1, m4
+        paddd                m1, m5
+        paddd                m1, m6
+        psrld                m1, 16
+
+        movu                 m0, [lsrcq+o+0]
+        pshufb              m10, m0, [ex_r]
+        pshufb               m2, m0, [ex_g]
+        pshufb               m3, m0, [ex_b]
+        movu                 m0, [rsrcq+o+0]
+        pshufb               m4, m0, [ex_r]
+        pshufb               m5, m0, [ex_g]
+        pshufb               m6, m0, [ex_b]
+        pmulld              m10, [rsp+mmsize*6]
+        pmulld               m2, [rsp+mmsize*7]
+        pmulld               m3, [rsp+mmsize*8]
+        pmulld               m4, [rsp+mmsize*9]
+        pmulld               m5, [rsp+mmsize*10]
+        pmulld               m6, [rsp+mmsize*11]
+        paddd               m10, m2
+        paddd               m10, m3
+        paddd               m10, m4
+        paddd               m10, m5
+        paddd               m10, m6
+        psrld               m10, 16
+
+        movu                 m0, [lsrcq+o+0]
+        pshufb               m11, m0, [ex_r]
+        pshufb               m2, m0, [ex_g]
+        pshufb               m3, m0, [ex_b]
+        movu                 m0, [rsrcq+o+0]
+        pshufb               m4, m0, [ex_r]
+        pshufb               m5, m0, [ex_g]
+        pshufb               m6, m0, [ex_b]
+        pmulld               m11, [rsp+mmsize*12]
+        pmulld               m2, [rsp+mmsize*13]
+        pmulld               m3, [rsp+mmsize*14]
+        pmulld               m4, [rsp+mmsize*15]
+        pmulld               m5, [rsp+mmsize*16]
+        pmulld               m6, [rsp+mmsize*17]
+        paddd                m11, m2
+        paddd                m11, m3
+        paddd                m11, m4
+        paddd                m11, m5
+        paddd                m11, m6
+        psrld                m11, 16
+
+        packusdw              m1, m10
+        packusdw             m11, m12
+        packuswb              m1, m11
+        pshufb                m1, [shuf]
+
+        movq         [dstq+o+0], m1
+        psrldq               m1, 8
+        movd         [dstq+o+8], m1
+        add                r12d, 12
+        sub                r11d, 4
+    jg .loop
+
+    add          dstq, dst_linesizeq
+    add         lsrcq, l_linesizeq
+    add         rsrcq, r_linesizeq
+    sub       heightd, 1
+    jg .nextrow
+REP_RET
+%endif
diff --git a/libavfilter/x86/vf_stereo3d_init.c b/libavfilter/x86/vf_stereo3d_init.c
new file mode 100644
index 0000000..c837bfa
--- /dev/null
+++ b/libavfilter/x86/vf_stereo3d_init.c
@@ -0,0 +1,37 @@
+/*
+ * 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/cpu.h"
+
+#include "libavfilter/stereo3d.h"
+
+void ff_anaglyph_sse4(uint8_t *dst, uint8_t *lsrc, uint8_t *rsrc,
+                      ptrdiff_t dst_linesize, ptrdiff_t l_linesize, ptrdiff_t r_linesize,
+                      int width, int height,
+                      const int *ana_matrix_r, const int *ana_matrix_g, const int *ana_matrix_b);
+
+void ff_stereo3d_init_x86(Stereo3DDSPContext *dsp)
+{
+    int cpu_flags = av_get_cpu_flags();
+
+    if (0 && ARCH_X86_64 && EXTERNAL_SSE4(cpu_flags)) {
+        dsp->anaglyph = ff_anaglyph_sse4;
+    }
+}
-- 
1.9.1



More information about the ffmpeg-devel mailing list