From ac74e857a26583f67d5518c805cfc23f2a482f2b Mon Sep 17 00:00:00 2001 From: Paul B Mahol Date: Sun, 4 Oct 2015 11:34:03 +0200 Subject: [PATCH] avfilter/vf_stereo3d: add x86 SIMD for anaglyph outputs Signed-off-by: Paul B Mahol --- libavfilter/stereo3d.h | 36 +++++++ libavfilter/vf_stereo3d.c | 70 ++++++------ libavfilter/x86/Makefile | 2 + libavfilter/x86/vf_stereo3d.asm | 167 +++++++++++++++++++++++++++++ libavfilter/x86/vf_stereo3d_init.c | 37 +++++++ 5 files changed, 280 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 0000000000..e7a84568ee --- /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 +#include + +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 f9d344a427..8e9cc83af1 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,40 +550,13 @@ static int config_output(AVFilterLink *outlink) s->hsub = desc->log2_chroma_w; s->vsub = desc->log2_chroma_h; + s->dsp.anaglyph = anaglyph; + if (ARCH_X86) + ff_stereo3d_init_x86(&s->dsp); + 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); -} - -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; - } -} - typedef struct ThreadData { AVFrame *ileft, *iright; AVFrame *out; @@ -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 57b413fd57..c02420476e 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 0000000000..94a0473290 --- /dev/null +++ b/libavfilter/x86/vf_stereo3d.asm @@ -0,0 +1,167 @@ +;***************************************************************************** +;* 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, 2*6*mmsize, dst, lsrc, rsrc, dst_linesize, l_linesize, r_linesize, width, height, ana_matrix_r, ana_matrix_g, ana_matrix_b + movu m13, [ana_matrix_rq+ 0] + movq m15, [ana_matrix_rq+16] + pshufd m10, m13, q0000 + pshufd m11, m13, q1111 + pshufd m12, m13, q2222 + pshufd m13, m13, q3333 + pshufd m14, m15, q0000 + pshufd m15, m15, q1111 + 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 + + movu m13, [ana_matrix_gq+ 0] + movq m15, [ana_matrix_gq+16] + pshufd m10, m13, q0000 + pshufd m11, m13, q1111 + pshufd m12, m13, q2222 + pshufd m13, m13, q3333 + pshufd m14, m15, q0000 + pshufd m15, m15, q1111 + 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 + + movu m13, [ana_matrix_bq+ 0] + movq m15, [ana_matrix_bq+16] + pshufd m10, m13, q0000 + pshufd m11, m13, q1111 + pshufd m12, m13, q2222 + pshufd m13, m13, q3333 + pshufd m14, m15, q0000 + pshufd m15, m15, q1111 +.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 m3, m4 + paddd m5, m6 + paddd m1, m3 + paddd m1, m5 + + movu m0, [lsrcq+o+0] + pshufb m7, 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 m7, [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 m7, m2 + paddd m3, m4 + paddd m5, m6 + paddd m7, m3 + paddd m7, m5 + + movu m0, [lsrcq+o+0] + pshufb m8, 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 m8, m10 + pmulld m2, m11 + pmulld m3, m12 + pmulld m4, m13 + pmulld m5, m14 + pmulld m6, m15 + paddd m8, m2 + paddd m3, m4 + paddd m5, m6 + paddd m8, m3 + paddd m8, m5 + + psrld m1, 16 + psrld m7, 16 + psrld m8, 16 + + packusdw m1, m7 + packusdw m8, m8 + packuswb m1, m8 + 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 0000000000..77d4f7b928 --- /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 (ARCH_X86_64 && EXTERNAL_SSE4(cpu_flags)) { + dsp->anaglyph = ff_anaglyph_sse4; + } +}