From 1270fe2d9e23a5dfaeb0e54ac62c59edbc81de0b Mon Sep 17 00:00:00 2001
From: Martin Vignali <martin.vignali@gmail.com>
Date: Sat, 16 Dec 2017 18:50:32 +0100
Subject: [PATCH 7/9] checkasm/vf_limiter : add checkasm test for 8 and 16
 limiter

---
 tests/checkasm/Makefile     |  1 +
 tests/checkasm/checkasm.c   |  3 ++
 tests/checkasm/checkasm.h   |  1 +
 tests/checkasm/vf_limiter.c | 82 +++++++++++++++++++++++++++++++++++++++++++++
 tests/fate/checkasm.mak     |  1 +
 5 files changed, 88 insertions(+)
 create mode 100644 tests/checkasm/vf_limiter.c

diff --git a/tests/checkasm/Makefile b/tests/checkasm/Makefile
index 6b5b1684a7..56e21f9cd8 100644
--- a/tests/checkasm/Makefile
+++ b/tests/checkasm/Makefile
@@ -34,6 +34,7 @@ AVFILTEROBJS-$(CONFIG_BLEND_FILTER) += vf_blend.o
 AVFILTEROBJS-$(CONFIG_COLORSPACE_FILTER) += vf_colorspace.o
 AVFILTEROBJS-$(CONFIG_HFLIP_FILTER)      += vf_hflip.o
 AVFILTEROBJS-$(CONFIG_INTERLACE_FILTER)  += vf_interlace.o
+AVFILTEROBJS-$(CONFIG_LIMITER_FILTER)    += vf_limiter.o
 AVFILTEROBJS-$(CONFIG_THRESHOLD_FILTER)  += vf_threshold.o
 
 CHECKASMOBJS-$(CONFIG_AVFILTER) += $(AVFILTEROBJS-yes)
diff --git a/tests/checkasm/checkasm.c b/tests/checkasm/checkasm.c
index 8a9480c3ca..0794272042 100644
--- a/tests/checkasm/checkasm.c
+++ b/tests/checkasm/checkasm.c
@@ -158,6 +158,9 @@ static const struct {
     #if CONFIG_INTERLACE_FILTER
         { "vf_interlace", checkasm_check_vf_interlace },
     #endif
+    #if CONFIG_LIMITER_FILTER
+        { "vf_limiter", checkasm_check_vf_limiter },
+    #endif
     #if CONFIG_THRESHOLD_FILTER
         { "vf_threshold", checkasm_check_vf_threshold },
     #endif
diff --git a/tests/checkasm/checkasm.h b/tests/checkasm/checkasm.h
index ec57c8a26f..0d10242c62 100644
--- a/tests/checkasm/checkasm.h
+++ b/tests/checkasm/checkasm.h
@@ -67,6 +67,7 @@ void checkasm_check_utvideodsp(void);
 void checkasm_check_v210enc(void);
 void checkasm_check_vf_hflip(void);
 void checkasm_check_vf_interlace(void);
+void checkasm_check_vf_limiter(void);
 void checkasm_check_vf_threshold(void);
 void checkasm_check_vp8dsp(void);
 void checkasm_check_vp9dsp(void);
diff --git a/tests/checkasm/vf_limiter.c b/tests/checkasm/vf_limiter.c
new file mode 100644
index 0000000000..6706f6e94b
--- /dev/null
+++ b/tests/checkasm/vf_limiter.c
@@ -0,0 +1,82 @@
+/*
+ * 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/limiter.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_limiter(int depth){
+    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 min, max, tmp_min, tmp_max;
+    LimiterDSPContext s;
+
+    declare_func(void, const uint8_t *src, uint8_t *dst,
+                 ptrdiff_t slinesize, ptrdiff_t dlinesize,
+                 int w, int h, int min, int max);
+
+    memset(src,     0, WIDTH_PADDED);
+    memset(dst_ref, 0, WIDTH_PADDED);
+    memset(dst_new, 0, WIDTH_PADDED);
+    randomize_buffers(src, WIDTH_PADDED);
+
+    if (depth == 8) {
+        tmp_min = rnd() & 0xFF;
+        tmp_max = rnd() & 0xFF;
+        min = FFMIN(tmp_min, tmp_max);
+        max = FFMAX(tmp_max, tmp_min);
+    } else {
+        tmp_min = rnd() & 0xFFFF;
+        tmp_max = rnd() & 0xFFFF;
+        min = FFMIN(tmp_min, tmp_max);
+        max = FFMAX(tmp_max, tmp_min);
+        w /= 2;
+    }
+
+    ff_limiter_init(&s, depth);
+
+    if (check_func(s.limiter, "limiter_%d", depth)) {
+        call_ref(src, dst_ref, WIDTH_PADDED, WIDTH_PADDED, w, 1, min, max);
+        call_new(src, dst_new, WIDTH_PADDED, WIDTH_PADDED, w, 1, min, max);
+        if (memcmp(dst_ref, dst_new, WIDTH))
+            fail();
+        bench_new(src, dst_new, WIDTH_PADDED, WIDTH_PADDED, w, 1, min, max);
+    }
+}
+void checkasm_check_vf_limiter(void)
+{
+    check_limiter(8);
+    report("limiter_8");
+
+    check_limiter(16);
+    report("limiter_16");
+}
diff --git a/tests/fate/checkasm.mak b/tests/fate/checkasm.mak
index 03f640b31f..ab986de6ce 100644
--- a/tests/fate/checkasm.mak
+++ b/tests/fate/checkasm.mak
@@ -24,6 +24,7 @@ FATE_CHECKASM = fate-checkasm-aacpsdsp                                  \
                 fate-checkasm-vf_colorspace                             \
                 fate-checkasm-vf_hflip                                  \
                 fate-checkasm-vf_interlace                              \
+                fate-checkasm-vf_limiter                                \
                 fate-checkasm-vf_threshold                              \
                 fate-checkasm-videodsp                                  \
                 fate-checkasm-vp8dsp                                    \
-- 
2.14.3 (Apple Git-98)

