From f47ace68edb95f8c57a8ac4ddee11d1e8b608210 Mon Sep 17 00:00:00 2001
From: Martin Vignali <martin.vignali@gmail.com>
Date: Sat, 5 Aug 2017 22:28:50 +0200
Subject: [PATCH] libavcodec/exr : add sse2 simd for reorder_pixels

---
 libavcodec/Makefile          |  2 +-
 libavcodec/exr.c             | 37 +++++++----------
 libavcodec/exrdsp.c          | 48 ++++++++++++++++++++++
 libavcodec/exrdsp.h          | 31 ++++++++++++++
 libavcodec/x86/Makefile      |  2 +
 libavcodec/x86/exrdsp.asm    | 98 ++++++++++++++++++++++++++++++++++++++++++++
 libavcodec/x86/exrdsp_init.c | 77 ++++++++++++++++++++++++++++++++++
 7 files changed, 271 insertions(+), 24 deletions(-)
 create mode 100644 libavcodec/exrdsp.c
 create mode 100644 libavcodec/exrdsp.h
 create mode 100644 libavcodec/x86/exrdsp.asm
 create mode 100644 libavcodec/x86/exrdsp_init.c

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index b0c39ac040..06ff2ef12b 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -286,7 +286,7 @@ OBJS-$(CONFIG_EIGHTSVX_FIB_DECODER)    += 8svx.o
 OBJS-$(CONFIG_ESCAPE124_DECODER)       += escape124.o
 OBJS-$(CONFIG_ESCAPE130_DECODER)       += escape130.o
 OBJS-$(CONFIG_EVRC_DECODER)            += evrcdec.o acelp_vectors.o lsp.o
-OBJS-$(CONFIG_EXR_DECODER)             += exr.o
+OBJS-$(CONFIG_EXR_DECODER)             += exr.o exrdsp.o
 OBJS-$(CONFIG_FFV1_DECODER)            += ffv1dec.o ffv1.o
 OBJS-$(CONFIG_FFV1_ENCODER)            += ffv1enc.o ffv1.o
 OBJS-$(CONFIG_FFWAVESYNTH_DECODER)     += ffwavesynth.o
diff --git a/libavcodec/exr.c b/libavcodec/exr.c
index 759880756d..fb3479b683 100644
--- a/libavcodec/exr.c
+++ b/libavcodec/exr.c
@@ -40,6 +40,7 @@
 #include "libavutil/avassert.h"
 #include "libavutil/common.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/timer.h"
 #include "libavutil/intfloat.h"
 #include "libavutil/opt.h"
 #include "libavutil/color_utils.h"
@@ -55,6 +56,7 @@
 #include "internal.h"
 #include "mathops.h"
 #include "thread.h"
+#include "exrdsp.h"
 
 enum ExrCompr {
     EXR_RAW,
@@ -121,6 +123,7 @@ typedef struct EXRContext {
     AVClass *class;
     AVFrame *picture;
     AVCodecContext *avctx;
+    ExrDSPContext dsp;
 
 #if HAVE_BIGENDIAN
     BswapDSPContext bbdsp;
@@ -275,23 +278,7 @@ static void predictor(uint8_t *src, int size)
     }
 }
 
-static void reorder_pixels(uint8_t *src, uint8_t *dst, int size)
-{
-    const uint8_t *t1 = src;
-    int half_size     = size / 2;
-    const uint8_t *t2 = src + half_size;
-    uint8_t *s        = dst;
-    int i;
-
-    av_assert1(size % 2 == 0);
-
-    for (i = 0; i < half_size; i++) {
-        *(s++) = *(t1++);
-        *(s++) = *(t2++);
-    }
-}
-
-static int zip_uncompress(const uint8_t *src, int compressed_size,
+static int zip_uncompress(EXRContext *s, const uint8_t *src, int compressed_size,
                           int uncompressed_size, EXRThreadData *td)
 {
     unsigned long dest_len = uncompressed_size;
@@ -299,14 +286,16 @@ static int zip_uncompress(const uint8_t *src, int compressed_size,
     if (uncompress(td->tmp, &dest_len, src, compressed_size) != Z_OK ||
         dest_len != uncompressed_size)
         return AVERROR_INVALIDDATA;
-
     predictor(td->tmp, uncompressed_size);
-    reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
+
+    START_TIMER;
+    s->dsp.reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
+    STOP_TIMER("reorder_pixels_zip");
 
     return 0;
 }
 
-static int rle_uncompress(const uint8_t *src, int compressed_size,
+static int rle_uncompress(EXRContext *ctx, const uint8_t *src, int compressed_size,
                           int uncompressed_size, EXRThreadData *td)
 {
     uint8_t *d      = td->tmp;
@@ -346,7 +335,7 @@ static int rle_uncompress(const uint8_t *src, int compressed_size,
         return AVERROR_INVALIDDATA;
 
     predictor(td->tmp, uncompressed_size);
-    reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
+    ctx->dsp.reorder_pixels(td->tmp, td->uncompressed_data, uncompressed_size);
 
     return 0;
 }
@@ -1161,7 +1150,7 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
         switch (s->compression) {
         case EXR_ZIP1:
         case EXR_ZIP16:
-            ret = zip_uncompress(src, data_size, uncompressed_size, td);
+            ret = zip_uncompress(s, src, data_size, uncompressed_size, td);
             break;
         case EXR_PIZ:
             ret = piz_uncompress(s, src, data_size, uncompressed_size, td);
@@ -1170,7 +1159,7 @@ static int decode_block(AVCodecContext *avctx, void *tdata,
             ret = pxr24_uncompress(s, src, data_size, uncompressed_size, td);
             break;
         case EXR_RLE:
-            ret = rle_uncompress(src, data_size, uncompressed_size, td);
+            ret = rle_uncompress(s, src, data_size, uncompressed_size, td);
             break;
         case EXR_B44:
         case EXR_B44A:
@@ -1804,6 +1793,8 @@ static av_cold int decode_init(AVCodecContext *avctx)
 
     s->avctx              = avctx;
 
+    ff_exrdsp_init(&s->dsp);
+
 #if HAVE_BIGENDIAN
     ff_bswapdsp_init(&s->bbdsp);
 #endif
diff --git a/libavcodec/exrdsp.c b/libavcodec/exrdsp.c
new file mode 100644
index 0000000000..964665cda3
--- /dev/null
+++ b/libavcodec/exrdsp.c
@@ -0,0 +1,48 @@
+/*
+ * 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 <stdint.h>
+
+#include "libavutil/attributes.h"
+#include "libavutil/avassert.h"
+#include "exrdsp.h"
+#include "config.h"
+
+static void reorder_pixels_scalar(uint8_t *src, uint8_t *dst, int size)
+{
+    const uint8_t *t1 = src;
+    int half_size     = size / 2;
+    const uint8_t *t2 = src + half_size;
+    uint8_t *s        = dst;
+    int i;
+
+    av_assert1(size % 2 == 0);
+
+    for (i = 0; i < half_size; i++) {
+        *(s++) = *(t1++);
+        *(s++) = *(t2++);
+    }
+}
+
+av_cold void ff_exrdsp_init(ExrDSPContext *c)
+{
+    c->reorder_pixels   = reorder_pixels_scalar;
+
+    if (ARCH_X86)
+        ff_exrdsp_init_x86(c);
+}
diff --git a/libavcodec/exrdsp.h b/libavcodec/exrdsp.h
new file mode 100644
index 0000000000..f991164d15
--- /dev/null
+++ b/libavcodec/exrdsp.h
@@ -0,0 +1,31 @@
+/*
+ * 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 AVCODEC_EXRDSP_H
+#define AVCODEC_EXRDSP_H
+
+#include <stdint.h>
+
+typedef struct ExrDSPContext {
+    void (*reorder_pixels)(uint8_t *src, uint8_t *dst, int size);
+} ExrDSPContext;
+
+void ff_exrdsp_init(ExrDSPContext *c);
+void ff_exrdsp_init_x86(ExrDSPContext *c);
+
+#endif /* AVCODEC_EXRDSP_H */
diff --git a/libavcodec/x86/Makefile b/libavcodec/x86/Makefile
index 0dbc46504e..3089302591 100644
--- a/libavcodec/x86/Makefile
+++ b/libavcodec/x86/Makefile
@@ -52,6 +52,7 @@ OBJS-$(CONFIG_APNG_DECODER)            += x86/pngdsp_init.o
 OBJS-$(CONFIG_CAVS_DECODER)            += x86/cavsdsp.o
 OBJS-$(CONFIG_DCA_DECODER)             += x86/dcadsp_init.o x86/synth_filter_init.o
 OBJS-$(CONFIG_DNXHD_ENCODER)           += x86/dnxhdenc_init.o
+OBJS-$(CONFIG_EXR_DECODER)             += x86/exrdsp_init.o
 OBJS-$(CONFIG_HEVC_DECODER)            += x86/hevcdsp_init.o
 OBJS-$(CONFIG_JPEG2000_DECODER)        += x86/jpeg2000dsp_init.o
 OBJS-$(CONFIG_MLP_DECODER)             += x86/mlpdsp_init.o
@@ -150,6 +151,7 @@ X86ASM-OBJS-$(CONFIG_DCA_DECODER)      += x86/dcadsp.o x86/synth_filter.o
 X86ASM-OBJS-$(CONFIG_DIRAC_DECODER)    += x86/diracdsp.o                \
                                           x86/dirac_dwt.o
 X86ASM-OBJS-$(CONFIG_DNXHD_ENCODER)    += x86/dnxhdenc.o
+X86ASM-OBJS-$(CONFIG_EXR_DECODER)      += x86/exrdsp.o
 X86ASM-OBJS-$(CONFIG_FLAC_DECODER)     += x86/flacdsp.o
 ifdef CONFIG_GPL
 X86ASM-OBJS-$(CONFIG_FLAC_ENCODER)     += x86/flac_dsp_gpl.o
diff --git a/libavcodec/x86/exrdsp.asm b/libavcodec/x86/exrdsp.asm
new file mode 100644
index 0000000000..92fe018d0a
--- /dev/null
+++ b/libavcodec/x86/exrdsp.asm
@@ -0,0 +1,98 @@
+;******************************************************************************
+;* X86 Optimized functions for Open Exr Decoder
+;* Copyright (c) 2006 Industrial Light & Magic, a division of Lucas Digital Ltd. LLC
+;*
+;* reorder_pixels based on patch by John Loy
+;* port to ASM by Jokyo Images support by CNC - French National Center for Cinema
+;*
+;* 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
+
+;------------------------------------------------------------------------------
+; void ff_reorder_pixels_sse2(uint8_t *src, uint8_t *dst, int size)
+;------------------------------------------------------------------------------
+
+
+INIT_XMM sse2
+cglobal reorder_pixels, 3,5,3, src, dst, size
+
+shr    r2,    1;half_size
+
+;calc loop count for simd reorder
+mov    r3,    r2;
+shr    r3,    4;calc loop count simd
+
+;jump to afterloop2 if loop count simd is 0
+cmp    r3,    0;
+jle    afterloop2;
+
+;simd loop
+loop1:
+movdqa    xmm0,    [r0];load first 16 bytes
+
+lea r4, [r0 + r2];
+
+movdqu    xmm1, [r4]; unaligned load
+movdqa     xmm2,    xmm0;copy xmm0
+
+punpcklbw     xmm2,   xmm1;
+movdqa     [r1],   xmm2;
+add     r1, 16;
+
+movdqa     xmm2,   xmm0;
+punpckhbw     xmm2,   xmm1;
+movdqa    [r1],   xmm2;
+add    r1,    16;
+
+dec    r3;
+add    r0,    16;
+
+; test repeat
+cmp    r3,   0;
+jge    loop1;
+
+
+afterloop2:
+;scalar part
+
+mov r3, r2;
+and r3, 15;half_size % 16
+lea r4, [r0 + r2];
+
+;initial condition loop
+cmp    r3,    0;
+jle    end;
+
+loop2:
+mov r1, r0;
+inc r1;
+mov r1, r4;
+inc r1;
+inc r0;
+inc r4;
+dec r3;
+
+; test repeat
+cmp    r3,   0;
+jg    loop2;
+
+end:
+    RET
diff --git a/libavcodec/x86/exrdsp_init.c b/libavcodec/x86/exrdsp_init.c
new file mode 100644
index 0000000000..a9014f1fcc
--- /dev/null
+++ b/libavcodec/x86/exrdsp_init.c
@@ -0,0 +1,77 @@
+/*
+ * OpenEXR (.exr) image decoder
+ *
+ * Copyright (c) 2006 Industrial Light & Magic, a division of Lucas Digital Ltd. LLC
+ *
+ * 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/x86/cpu.h"
+#include "libavcodec/exrdsp.h"
+
+//#include <emmintrin.h>//SSE2
+
+void ff_reorder_pixels_sse2(uint8_t *src, uint8_t *dst, int size);
+
+/*
+static void ff_reorder_pixels_sse2_intrinsics(uint8_t *src, uint8_t *dst, int size)
+{
+    int half_size     = size >> 1;
+
+    int i;
+    const uint8_t *t1;
+    const uint8_t *t2;
+    uint8_t *s;
+
+    const __m128i *vector1 = (__m128i*)src;
+    const __m128i *vector2 = (__m128i*)(src+half_size);
+
+    __m128i *vector_out = (__m128i*)dst;
+
+    for (i = 0; i < half_size/sizeof(__m128i); i++) {
+        __m128i a = _mm_load_si128(vector1++);//Start is aligned
+        __m128i b = _mm_loadu_si128(vector2++);//Half is not aligned
+
+        __m128i lo = _mm_unpacklo_epi8(a, b);
+        __m128i hi = _mm_unpackhi_epi8(a, b);
+
+        _mm_store_si128(vector_out++, lo);//Can be store aligned
+        _mm_store_si128(vector_out++, hi);//here too
+    }
+
+    t1 = (uint8_t*)vector1;
+    t2 = (uint8_t*)vector2;
+    s = (uint8_t*)vector_out;
+
+    for (i = 0; i < half_size % sizeof(__m128i); i++) {
+        *(s++) = *(t1++);
+        *(s++) = *(t2++);
+    }
+}*/
+
+
+av_cold void ff_exrdsp_init_x86(ExrDSPContext *dsp)
+{
+#if ARCH_X86_64
+    int cpu_flags = av_get_cpu_flags();
+
+    if (EXTERNAL_SSE2(cpu_flags)) {
+        dsp->reorder_pixels = ff_reorder_pixels_sse2;
+    }
+#endif /* ARCH_X86_64 */
+}
-- 
2.11.0 (Apple Git-81)

