blob: fa04528a2413c092d25b06fe24e11381913c623e [file] [log] [blame]
Jim Bankoski0ce51d82014-10-07 16:36:14 -07001/*
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11
12#include <string.h>
13#include <limits.h>
14#include <stdio.h>
15
16#include "./vpx_config.h"
17#if CONFIG_VP9_ENCODER
18#include "./vp9_rtcd.h"
19#endif
20#include "vpx_mem/vpx_mem.h"
21
22#include "test/acm_random.h"
23#include "test/clear_system_state.h"
24#include "test/register_state_check.h"
25#include "test/util.h"
26#include "third_party/googletest/src/include/gtest/gtest.h"
27
28using libvpx_test::ACMRandom;
29
30namespace {
31class AverageTestBase : public ::testing::Test {
32 public:
33 AverageTestBase(int width, int height) : width_(width), height_(height) {}
34
35 static void SetUpTestCase() {
36 source_data_ = reinterpret_cast<uint8_t*>(
37 vpx_memalign(kDataAlignment, kDataBlockSize));
38 }
39
40 static void TearDownTestCase() {
41 vpx_free(source_data_);
42 source_data_ = NULL;
43 }
44
45 virtual void TearDown() {
46 libvpx_test::ClearSystemState();
47 }
48
49 protected:
50 // Handle blocks up to 4 blocks 64x64 with stride up to 128
51 static const int kDataAlignment = 16;
52 static const int kDataBlockSize = 64 * 128;
53
54 virtual void SetUp() {
55 source_stride_ = (width_ + 31) & ~31;
56 rnd_.Reset(ACMRandom::DeterministicSeed());
57 }
58
59 // Sum Pixels
Marco8fd3f9a2014-11-12 14:51:49 -080060 unsigned int ReferenceAverage8x8(const uint8_t* source, int pitch ) {
Jim Bankoski0ce51d82014-10-07 16:36:14 -070061 unsigned int average = 0;
62 for (int h = 0; h < 8; ++h)
63 for (int w = 0; w < 8; ++w)
64 average += source[h * source_stride_ + w];
65 return ((average + 32) >> 6);
66 }
67
Marco8fd3f9a2014-11-12 14:51:49 -080068 unsigned int ReferenceAverage4x4(const uint8_t* source, int pitch ) {
69 unsigned int average = 0;
70 for (int h = 0; h < 4; ++h)
71 for (int w = 0; w < 4; ++w)
72 average += source[h * source_stride_ + w];
73 return ((average + 8) >> 4);
74 }
75
Jim Bankoski0ce51d82014-10-07 16:36:14 -070076 void FillConstant(uint8_t fill_constant) {
77 for (int i = 0; i < width_ * height_; ++i) {
78 source_data_[i] = fill_constant;
79 }
80 }
81
82 void FillRandom() {
83 for (int i = 0; i < width_ * height_; ++i) {
84 source_data_[i] = rnd_.Rand8();
85 }
86 }
87
88 int width_, height_;
89 static uint8_t* source_data_;
90 int source_stride_;
91
92 ACMRandom rnd_;
93};
94typedef unsigned int (*AverageFunction)(const uint8_t* s, int pitch);
95
Marco8fd3f9a2014-11-12 14:51:49 -080096typedef std::tr1::tuple<int, int, int, int, AverageFunction> AvgFunc;
Jim Bankoski0ce51d82014-10-07 16:36:14 -070097
98class AverageTest
99 : public AverageTestBase,
100 public ::testing::WithParamInterface<AvgFunc>{
101 public:
102 AverageTest() : AverageTestBase(GET_PARAM(0), GET_PARAM(1)) {}
103
104 protected:
105 void CheckAverages() {
Marco8fd3f9a2014-11-12 14:51:49 -0800106 unsigned int expected = 0;
107 if (GET_PARAM(3) == 8) {
108 expected = ReferenceAverage8x8(source_data_+ GET_PARAM(2),
109 source_stride_);
110 } else if (GET_PARAM(3) == 4) {
111 expected = ReferenceAverage4x4(source_data_+ GET_PARAM(2),
112 source_stride_);
113 }
Jim Bankoski0ce51d82014-10-07 16:36:14 -0700114
Marco8fd3f9a2014-11-12 14:51:49 -0800115 ASM_REGISTER_STATE_CHECK(GET_PARAM(4)(source_data_+ GET_PARAM(2),
Jim Bankoski0ce51d82014-10-07 16:36:14 -0700116 source_stride_));
Marco8fd3f9a2014-11-12 14:51:49 -0800117 unsigned int actual = GET_PARAM(4)(source_data_+ GET_PARAM(2),
Jim Bankoski0ce51d82014-10-07 16:36:14 -0700118 source_stride_);
119
120 EXPECT_EQ(expected, actual);
121 }
122};
123
124
125uint8_t* AverageTestBase::source_data_ = NULL;
126
127TEST_P(AverageTest, MinValue) {
128 FillConstant(0);
129 CheckAverages();
130}
131
132TEST_P(AverageTest, MaxValue) {
133 FillConstant(255);
134 CheckAverages();
135}
136
137TEST_P(AverageTest, Random) {
138 // The reference frame, but not the source frame, may be unaligned for
139 // certain types of searches.
140 for (int i = 0; i < 1000; i++) {
141 FillRandom();
142 CheckAverages();
143 }
144}
145
146using std::tr1::make_tuple;
147
148INSTANTIATE_TEST_CASE_P(
149 C, AverageTest,
150 ::testing::Values(
Marco8fd3f9a2014-11-12 14:51:49 -0800151 make_tuple(16, 16, 1, 8, &vp9_avg_8x8_c),
152 make_tuple(16, 16, 1, 4, &vp9_avg_4x4_c)));
Jim Bankoski0ce51d82014-10-07 16:36:14 -0700153
154
155#if HAVE_SSE2
156INSTANTIATE_TEST_CASE_P(
157 SSE2, AverageTest,
158 ::testing::Values(
Marco8fd3f9a2014-11-12 14:51:49 -0800159 make_tuple(16, 16, 0, 8, &vp9_avg_8x8_sse2),
160 make_tuple(16, 16, 5, 8, &vp9_avg_8x8_sse2),
161 make_tuple(32, 32, 15, 8, &vp9_avg_8x8_sse2),
162 make_tuple(16, 16, 0, 4, &vp9_avg_4x4_sse2),
163 make_tuple(16, 16, 5, 4, &vp9_avg_4x4_sse2),
164 make_tuple(32, 32, 15, 4, &vp9_avg_4x4_sse2)));
Jim Bankoski0ce51d82014-10-07 16:36:14 -0700165
166#endif
167
168} // namespace