From 58628e2a9238d854d8b6c80f8ddef04da37f4f22 Mon Sep 17 00:00:00 2001
From: Martin Vignali <martin.vignali@gmail.com>
Date: Sun, 3 Dec 2017 19:02:56 +0100
Subject: [PATCH 2/2] checkasm/vf_hflip : add test for hflip SIMD

---
 libavfilter/hflip.h       |  1 +
 libavfilter/vf_hflip.c    | 12 ++++++--
 tests/checkasm/Makefile   |  1 +
 tests/checkasm/checkasm.c |  3 ++
 tests/checkasm/checkasm.h |  1 +
 tests/checkasm/vf_hflip.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 89 insertions(+), 3 deletions(-)
 create mode 100644 tests/checkasm/vf_hflip.c

diff --git a/libavfilter/hflip.h b/libavfilter/hflip.h
index 138380427c..68c150b064 100644
--- a/libavfilter/hflip.h
+++ b/libavfilter/hflip.h
@@ -33,6 +33,7 @@ typedef struct FlipContext {
     void (*flip_line[4])(const uint8_t *src, uint8_t *dst, int w);
 } FlipContext;
 
+int ff_hflip_init(FlipContext *s, int step[4]);
 void ff_hflip_init_x86(FlipContext *s, int step[4]);
 
 #endif /* AVFILTER_HFLIP_H */
diff --git a/libavfilter/vf_hflip.c b/libavfilter/vf_hflip.c
index 303cc8af60..ffc7f8a63f 100644
--- a/libavfilter/vf_hflip.c
+++ b/libavfilter/vf_hflip.c
@@ -131,7 +131,6 @@ static int config_props(AVFilterLink *inlink)
     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
     const int hsub = pix_desc->log2_chroma_w;
     const int vsub = pix_desc->log2_chroma_h;
-    int i;
 
     av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
     s->planewidth[0]  = s->planewidth[3]  = inlink->w;
@@ -139,8 +138,15 @@ static int config_props(AVFilterLink *inlink)
     s->planeheight[0] = s->planeheight[3] = inlink->h;
     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
 
+    return ff_hflip_init(s, s->max_step);
+}
+
+int ff_hflip_init(FlipContext *s, int step[4])
+{
+    int i;
+
     for (i = 0; i < 4; i++) {
-        switch (s->max_step[i]) {
+        switch (step[i]) {
         case 1: s->flip_line[i] = hflip_byte_c;  break;
         case 2: s->flip_line[i] = hflip_short_c; break;
         case 3: s->flip_line[i] = hflip_b24_c;   break;
@@ -153,7 +159,7 @@ static int config_props(AVFilterLink *inlink)
     }
 
     if (ARCH_X86)
-        ff_hflip_init_x86(s, s->max_step);
+        ff_hflip_init_x86(s, step);
 
     return 0;
 }
diff --git a/tests/checkasm/Makefile b/tests/checkasm/Makefile
index 6add9ded12..292cbbf94d 100644
--- a/tests/checkasm/Makefile
+++ b/tests/checkasm/Makefile
@@ -32,6 +32,7 @@ CHECKASMOBJS-$(CONFIG_AVCODEC)          += $(AVCODECOBJS-yes)
 # libavfilter tests
 AVFILTEROBJS-$(CONFIG_BLEND_FILTER) += vf_blend.o
 AVFILTEROBJS-$(CONFIG_COLORSPACE_FILTER) += vf_colorspace.o
+AVFILTEROBJS-$(CONFIG_HFLIP_FILTER)      += vf_hflip.o
 
 CHECKASMOBJS-$(CONFIG_AVFILTER) += $(AVFILTEROBJS-yes)
 
diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c
index a8b34ba898..0041d93639 100644
--- a/tests/checkasm/checkasm.c
+++ b/tests/checkasm/checkasm.c
@@ -152,6 +152,9 @@ static const struct {
     #if CONFIG_COLORSPACE_FILTER
         { "vf_colorspace", checkasm_check_colorspace },
     #endif
+    #if CONFIG_HFLIP_FILTER
+        { "vf_hflip", checkasm_check_vf_hflip },
+    #endif
 #endif
 #if CONFIG_AVUTIL
         { "fixed_dsp", checkasm_check_fixed_dsp },
diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
index 483f418cb5..6f94c63065 100644
--- a/tests/checkasm/checkasm.h
+++ b/tests/checkasm/checkasm.h
@@ -65,6 +65,7 @@ void checkasm_check_sbrdsp(void);
 void checkasm_check_synth_filter(void);
 void checkasm_check_utvideodsp(void);
 void checkasm_check_v210enc(void);
+void checkasm_check_vf_hflip(void);
 void checkasm_check_vp8dsp(void);
 void checkasm_check_vp9dsp(void);
 void checkasm_check_videodsp(void);
diff --git a/tests/checkasm/vf_hflip.c b/tests/checkasm/vf_hflip.c
new file mode 100644
index 0000000000..0b6ee2ffc7
--- /dev/null
+++ b/tests/checkasm/vf_hflip.c
@@ -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 General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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 <string.h>
+#include "checkasm.h"
+#include "libavfilter/hflip.h"
+#include "libavutil/intreadwrite.h"
+
+#define WIDTH 256
+#define WIDTH_PADDED 256 + 32
+
+#define randomize_buffers(buf, size)      \
+    do {                                  \
+        int j;                            \
+        uint8_t *tmp_buf = (uint8_t *)buf;\
+        for (j = 0; j < size; j++)        \
+            tmp_buf[j] = rnd() & 0xFF;    \
+    } while (0)
+
+static void check_hflip(int step, const char * report_name){
+    LOCAL_ALIGNED_32(uint8_t, src,     [WIDTH_PADDED]);
+    LOCAL_ALIGNED_32(uint8_t, dst_ref, [WIDTH_PADDED]);
+    LOCAL_ALIGNED_32(uint8_t, dst_new, [WIDTH_PADDED]);
+    int w = WIDTH;
+    int i;
+    int step_array[4] = {1, 1, 1, 1};
+    FlipContext s;
+
+    declare_func(void, const uint8_t *src, uint8_t *dst, int w);
+
+    memset(src,     0, WIDTH_PADDED);
+    memset(dst_ref, 0, WIDTH_PADDED);
+    memset(dst_new, 0, WIDTH_PADDED);
+    randomize_buffers(src, WIDTH_PADDED);
+
+    if (step == 2) {
+        w /= 2;
+        for (i = 0; i < 4; i++)
+            step_array[i] = step;
+    }
+
+    ff_hflip_init(&s, step_array);
+
+    if (check_func(s.flip_line[0], "hflip_%s", report_name)) {
+        call_ref(src, dst_ref, w);
+        call_new(src, dst_new, w);
+        if (memcmp(dst_ref, dst_new, WIDTH))
+            fail();
+        bench_new(src, dst_new, WIDTH);
+    }
+}
+void checkasm_check_vf_hflip(void)
+{
+    check_hflip(1, "byte");
+    report("hflip_byte");
+
+    check_hflip(2, "short");
+    report("hflip_short");
+}
-- 
2.11.0 (Apple Git-81)

