2016-10-24 21:17:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 The WebM project authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TEST_BUFFER_H_
|
|
|
|
#define TEST_BUFFER_H_
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
#include "third_party/googletest/src/include/gtest/gtest.h"
|
|
|
|
|
2016-12-10 01:51:01 +01:00
|
|
|
#include "test/acm_random.h"
|
2016-10-24 21:17:51 +02:00
|
|
|
#include "vpx/vpx_integer.h"
|
2017-06-07 00:15:47 +02:00
|
|
|
#include "vpx_mem/vpx_mem.h"
|
2016-10-24 21:17:51 +02:00
|
|
|
|
|
|
|
namespace libvpx_test {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Buffer {
|
|
|
|
public:
|
2017-06-08 19:20:21 +02:00
|
|
|
Buffer(int width, int height, int top_padding, int left_padding,
|
|
|
|
int right_padding, int bottom_padding)
|
2016-10-24 21:17:51 +02:00
|
|
|
: width_(width), height_(height), top_padding_(top_padding),
|
|
|
|
left_padding_(left_padding), right_padding_(right_padding),
|
2017-06-07 00:15:47 +02:00
|
|
|
bottom_padding_(bottom_padding), alignment_(0), padding_value_(0),
|
|
|
|
stride_(0), raw_size_(0), num_elements_(0), raw_buffer_(NULL) {}
|
|
|
|
|
|
|
|
Buffer(int width, int height, int top_padding, int left_padding,
|
|
|
|
int right_padding, int bottom_padding, unsigned int alignment)
|
|
|
|
: width_(width), height_(height), top_padding_(top_padding),
|
|
|
|
left_padding_(left_padding), right_padding_(right_padding),
|
|
|
|
bottom_padding_(bottom_padding), alignment_(alignment),
|
|
|
|
padding_value_(0), stride_(0), raw_size_(0), num_elements_(0),
|
|
|
|
raw_buffer_(NULL) {}
|
2016-10-24 21:17:51 +02:00
|
|
|
|
2017-06-08 19:20:21 +02:00
|
|
|
Buffer(int width, int height, int padding)
|
2016-10-24 21:17:51 +02:00
|
|
|
: width_(width), height_(height), top_padding_(padding),
|
|
|
|
left_padding_(padding), right_padding_(padding),
|
2017-06-07 00:15:47 +02:00
|
|
|
bottom_padding_(padding), alignment_(0), padding_value_(0), stride_(0),
|
|
|
|
raw_size_(0), num_elements_(0), raw_buffer_(NULL) {}
|
2016-10-24 21:17:51 +02:00
|
|
|
|
2017-06-07 00:15:47 +02:00
|
|
|
Buffer(int width, int height, int padding, unsigned int alignment)
|
|
|
|
: width_(width), height_(height), top_padding_(padding),
|
|
|
|
left_padding_(padding), right_padding_(padding),
|
|
|
|
bottom_padding_(padding), alignment_(alignment), padding_value_(0),
|
|
|
|
stride_(0), raw_size_(0), num_elements_(0), raw_buffer_(NULL) {}
|
|
|
|
|
|
|
|
~Buffer() {
|
|
|
|
if (alignment_) {
|
|
|
|
vpx_free(raw_buffer_);
|
|
|
|
} else {
|
|
|
|
delete[] raw_buffer_;
|
|
|
|
}
|
|
|
|
}
|
2016-10-24 21:17:51 +02:00
|
|
|
|
|
|
|
T *TopLeftPixel() const;
|
|
|
|
|
2017-06-08 19:20:21 +02:00
|
|
|
int stride() const { return stride_; }
|
2016-10-24 21:17:51 +02:00
|
|
|
|
|
|
|
// Set the buffer (excluding padding) to 'value'.
|
2017-06-06 21:05:14 +02:00
|
|
|
void Set(const T value);
|
2016-10-24 21:17:51 +02:00
|
|
|
|
2017-06-28 23:33:38 +02:00
|
|
|
// Set the buffer (excluding padding) to the output of ACMRandom function
|
|
|
|
// 'rand_func'.
|
2016-12-10 01:51:01 +01:00
|
|
|
void Set(ACMRandom *rand_class, T (ACMRandom::*rand_func)());
|
|
|
|
|
2017-06-28 23:33:38 +02:00
|
|
|
// Set the buffer (excluding padding) to the output of ACMRandom function
|
|
|
|
// 'RandRange' with range 'low' to 'high' which must be within
|
|
|
|
// testing::internal::Random::kMaxRange (1u << 31).
|
|
|
|
void Set(ACMRandom *rand_class, const int32_t low, const int32_t high);
|
|
|
|
|
2016-12-10 01:51:01 +01:00
|
|
|
// Copy the contents of Buffer 'a' (excluding padding).
|
|
|
|
void CopyFrom(const Buffer<T> &a);
|
|
|
|
|
2016-10-24 21:17:51 +02:00
|
|
|
void DumpBuffer() const;
|
|
|
|
|
2016-12-10 01:51:01 +01:00
|
|
|
// Highlight the differences between two buffers if they are the same size.
|
|
|
|
void PrintDifference(const Buffer<T> &a) const;
|
|
|
|
|
2016-10-24 21:17:51 +02:00
|
|
|
bool HasPadding() const;
|
|
|
|
|
|
|
|
// Sets all the values in the buffer to 'padding_value'.
|
2017-06-06 21:05:14 +02:00
|
|
|
void SetPadding(const T padding_value);
|
2016-10-24 21:17:51 +02:00
|
|
|
|
2016-12-10 01:51:01 +01:00
|
|
|
// Checks if all the values (excluding padding) are equal to 'value' if the
|
|
|
|
// Buffers are the same size.
|
2017-06-06 21:05:14 +02:00
|
|
|
bool CheckValues(const T value) const;
|
2016-10-24 21:17:51 +02:00
|
|
|
|
|
|
|
// Check that padding matches the expected value or there is no padding.
|
|
|
|
bool CheckPadding() const;
|
|
|
|
|
2016-12-10 01:51:01 +01:00
|
|
|
// Compare the non-padding portion of two buffers if they are the same size.
|
|
|
|
bool CheckValues(const Buffer<T> &a) const;
|
|
|
|
|
2017-06-06 21:48:01 +02:00
|
|
|
bool Init() {
|
2017-06-07 21:25:05 +02:00
|
|
|
if (raw_buffer_ != NULL) return false;
|
2017-06-08 19:20:21 +02:00
|
|
|
EXPECT_GT(width_, 0);
|
|
|
|
EXPECT_GT(height_, 0);
|
|
|
|
EXPECT_GE(top_padding_, 0);
|
|
|
|
EXPECT_GE(left_padding_, 0);
|
|
|
|
EXPECT_GE(right_padding_, 0);
|
|
|
|
EXPECT_GE(bottom_padding_, 0);
|
2016-10-24 21:17:51 +02:00
|
|
|
stride_ = left_padding_ + width_ + right_padding_;
|
2017-06-06 22:17:07 +02:00
|
|
|
num_elements_ = stride_ * (top_padding_ + height_ + bottom_padding_);
|
|
|
|
raw_size_ = num_elements_ * sizeof(T);
|
2017-06-07 00:15:47 +02:00
|
|
|
if (alignment_) {
|
|
|
|
EXPECT_GE(alignment_, sizeof(T));
|
|
|
|
// Ensure alignment of the first value will be preserved.
|
|
|
|
EXPECT_EQ((left_padding_ * sizeof(T)) % alignment_, 0u);
|
|
|
|
// Ensure alignment of the subsequent rows will be preserved when there is
|
|
|
|
// a stride.
|
|
|
|
if (stride_ != width_) {
|
|
|
|
EXPECT_EQ((stride_ * sizeof(T)) % alignment_, 0u);
|
|
|
|
}
|
|
|
|
raw_buffer_ = reinterpret_cast<T *>(vpx_memalign(alignment_, raw_size_));
|
|
|
|
} else {
|
|
|
|
raw_buffer_ = new (std::nothrow) T[num_elements_];
|
|
|
|
}
|
2017-06-06 21:48:01 +02:00
|
|
|
EXPECT_TRUE(raw_buffer_ != NULL);
|
2016-10-24 21:17:51 +02:00
|
|
|
SetPadding(std::numeric_limits<T>::max());
|
2017-06-06 21:48:01 +02:00
|
|
|
return !::testing::Test::HasFailure();
|
2016-10-24 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 21:48:01 +02:00
|
|
|
private:
|
2016-12-10 01:51:01 +01:00
|
|
|
bool BufferSizesMatch(const Buffer<T> &a) const;
|
|
|
|
|
2017-06-08 19:20:21 +02:00
|
|
|
const int width_;
|
|
|
|
const int height_;
|
|
|
|
const int top_padding_;
|
|
|
|
const int left_padding_;
|
|
|
|
const int right_padding_;
|
|
|
|
const int bottom_padding_;
|
2017-06-07 00:15:47 +02:00
|
|
|
const unsigned int alignment_;
|
2017-06-06 21:05:14 +02:00
|
|
|
T padding_value_;
|
2017-06-08 19:20:21 +02:00
|
|
|
int stride_;
|
|
|
|
int raw_size_;
|
|
|
|
int num_elements_;
|
2016-10-24 21:17:51 +02:00
|
|
|
T *raw_buffer_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T *Buffer<T>::TopLeftPixel() const {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return NULL;
|
2017-06-30 16:37:20 +02:00
|
|
|
return raw_buffer_ + (top_padding_ * stride_) + left_padding_;
|
2016-10-24 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-06-06 21:05:14 +02:00
|
|
|
void Buffer<T>::Set(const T value) {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return;
|
2016-10-24 21:17:51 +02:00
|
|
|
T *src = TopLeftPixel();
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int height = 0; height < height_; ++height) {
|
|
|
|
for (int width = 0; width < width_; ++width) {
|
2016-10-24 21:17:51 +02:00
|
|
|
src[width] = value;
|
|
|
|
}
|
2017-06-30 16:37:20 +02:00
|
|
|
src += stride_;
|
2016-10-24 21:17:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-10 01:51:01 +01:00
|
|
|
template <typename T>
|
|
|
|
void Buffer<T>::Set(ACMRandom *rand_class, T (ACMRandom::*rand_func)()) {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return;
|
2016-12-10 01:51:01 +01:00
|
|
|
T *src = TopLeftPixel();
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int height = 0; height < height_; ++height) {
|
|
|
|
for (int width = 0; width < width_; ++width) {
|
2016-12-10 01:51:01 +01:00
|
|
|
src[width] = (*rand_class.*rand_func)();
|
|
|
|
}
|
2017-06-30 16:37:20 +02:00
|
|
|
src += stride_;
|
2016-12-10 01:51:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-28 23:33:38 +02:00
|
|
|
template <typename T>
|
|
|
|
void Buffer<T>::Set(ACMRandom *rand_class, const int32_t low,
|
|
|
|
const int32_t high) {
|
|
|
|
if (!raw_buffer_) return;
|
|
|
|
|
|
|
|
EXPECT_LE(low, high);
|
|
|
|
EXPECT_GE(low, std::numeric_limits<T>::min());
|
|
|
|
EXPECT_LE(high, std::numeric_limits<T>::max());
|
|
|
|
|
|
|
|
T *src = TopLeftPixel();
|
|
|
|
for (int height = 0; height < height_; ++height) {
|
|
|
|
for (int width = 0; width < width_; ++width) {
|
|
|
|
src[width] = static_cast<T>((*rand_class).RandRange(high - low) + low);
|
|
|
|
}
|
|
|
|
src += stride_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-10 01:51:01 +01:00
|
|
|
template <typename T>
|
|
|
|
void Buffer<T>::CopyFrom(const Buffer<T> &a) {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return;
|
|
|
|
if (!BufferSizesMatch(a)) return;
|
2016-12-10 01:51:01 +01:00
|
|
|
|
|
|
|
T *a_src = a.TopLeftPixel();
|
|
|
|
T *b_src = this->TopLeftPixel();
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int height = 0; height < height_; ++height) {
|
|
|
|
for (int width = 0; width < width_; ++width) {
|
2016-12-10 01:51:01 +01:00
|
|
|
b_src[width] = a_src[width];
|
|
|
|
}
|
|
|
|
a_src += a.stride();
|
|
|
|
b_src += this->stride();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:17:51 +02:00
|
|
|
template <typename T>
|
|
|
|
void Buffer<T>::DumpBuffer() const {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return;
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int height = 0; height < height_ + top_padding_ + bottom_padding_;
|
2016-10-24 21:17:51 +02:00
|
|
|
++height) {
|
2017-06-30 16:37:20 +02:00
|
|
|
for (int width = 0; width < stride_; ++width) {
|
|
|
|
printf("%4d", raw_buffer_[height + width * stride_]);
|
2016-10-24 21:17:51 +02:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool Buffer<T>::HasPadding() const {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return false;
|
2016-10-24 21:17:51 +02:00
|
|
|
return top_padding_ || left_padding_ || right_padding_ || bottom_padding_;
|
|
|
|
}
|
|
|
|
|
2016-12-10 01:51:01 +01:00
|
|
|
template <typename T>
|
|
|
|
void Buffer<T>::PrintDifference(const Buffer<T> &a) const {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return;
|
|
|
|
if (!BufferSizesMatch(a)) return;
|
2016-12-10 01:51:01 +01:00
|
|
|
|
|
|
|
T *a_src = a.TopLeftPixel();
|
|
|
|
T *b_src = TopLeftPixel();
|
|
|
|
|
|
|
|
printf("This buffer:\n");
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int height = 0; height < height_; ++height) {
|
|
|
|
for (int width = 0; width < width_; ++width) {
|
2016-12-10 01:51:01 +01:00
|
|
|
if (a_src[width] != b_src[width]) {
|
|
|
|
printf("*%3d", b_src[width]);
|
|
|
|
} else {
|
|
|
|
printf("%4d", b_src[width]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
a_src += a.stride();
|
|
|
|
b_src += this->stride();
|
|
|
|
}
|
|
|
|
|
|
|
|
a_src = a.TopLeftPixel();
|
|
|
|
b_src = TopLeftPixel();
|
|
|
|
|
|
|
|
printf("Reference buffer:\n");
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int height = 0; height < height_; ++height) {
|
|
|
|
for (int width = 0; width < width_; ++width) {
|
2016-12-10 01:51:01 +01:00
|
|
|
if (a_src[width] != b_src[width]) {
|
|
|
|
printf("*%3d", a_src[width]);
|
|
|
|
} else {
|
|
|
|
printf("%4d", a_src[width]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
a_src += a.stride();
|
|
|
|
b_src += this->stride();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:17:51 +02:00
|
|
|
template <typename T>
|
2017-06-06 21:05:14 +02:00
|
|
|
void Buffer<T>::SetPadding(const T padding_value) {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return;
|
2016-10-24 21:17:51 +02:00
|
|
|
padding_value_ = padding_value;
|
|
|
|
|
|
|
|
T *src = raw_buffer_;
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int i = 0; i < num_elements_; ++i) {
|
2016-10-24 21:17:51 +02:00
|
|
|
src[i] = padding_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-06-06 21:05:14 +02:00
|
|
|
bool Buffer<T>::CheckValues(const T value) const {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return false;
|
2016-10-24 21:17:51 +02:00
|
|
|
T *src = TopLeftPixel();
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int height = 0; height < height_; ++height) {
|
|
|
|
for (int width = 0; width < width_; ++width) {
|
2016-10-24 21:17:51 +02:00
|
|
|
if (value != src[width]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 16:37:20 +02:00
|
|
|
src += stride_;
|
2016-10-24 21:17:51 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool Buffer<T>::CheckPadding() const {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return false;
|
|
|
|
if (!HasPadding()) return true;
|
2016-10-24 21:17:51 +02:00
|
|
|
|
|
|
|
// Top padding.
|
|
|
|
T const *top = raw_buffer_;
|
2017-06-30 16:37:20 +02:00
|
|
|
for (int i = 0; i < stride_ * top_padding_; ++i) {
|
2016-10-24 21:17:51 +02:00
|
|
|
if (padding_value_ != top[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Left padding.
|
|
|
|
T const *left = TopLeftPixel() - left_padding_;
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int height = 0; height < height_; ++height) {
|
|
|
|
for (int width = 0; width < left_padding_; ++width) {
|
2016-10-24 21:17:51 +02:00
|
|
|
if (padding_value_ != left[width]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 16:37:20 +02:00
|
|
|
left += stride_;
|
2016-10-24 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Right padding.
|
|
|
|
T const *right = TopLeftPixel() + width_;
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int height = 0; height < height_; ++height) {
|
|
|
|
for (int width = 0; width < right_padding_; ++width) {
|
2016-10-24 21:17:51 +02:00
|
|
|
if (padding_value_ != right[width]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-06-30 16:37:20 +02:00
|
|
|
right += stride_;
|
2016-10-24 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bottom padding
|
2017-06-30 16:37:20 +02:00
|
|
|
T const *bottom = raw_buffer_ + (top_padding_ + height_) * stride_;
|
|
|
|
for (int i = 0; i < stride_ * bottom_padding_; ++i) {
|
2016-10-24 21:17:51 +02:00
|
|
|
if (padding_value_ != bottom[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-12-10 01:51:01 +01:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool Buffer<T>::CheckValues(const Buffer<T> &a) const {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return false;
|
|
|
|
if (!BufferSizesMatch(a)) return false;
|
2016-12-10 01:51:01 +01:00
|
|
|
|
|
|
|
T *a_src = a.TopLeftPixel();
|
|
|
|
T *b_src = this->TopLeftPixel();
|
2017-06-08 19:20:21 +02:00
|
|
|
for (int height = 0; height < height_; ++height) {
|
|
|
|
for (int width = 0; width < width_; ++width) {
|
2016-12-10 01:51:01 +01:00
|
|
|
if (a_src[width] != b_src[width]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a_src += a.stride();
|
|
|
|
b_src += this->stride();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool Buffer<T>::BufferSizesMatch(const Buffer<T> &a) const {
|
2017-06-06 21:48:01 +02:00
|
|
|
if (!raw_buffer_) return false;
|
2016-12-10 01:51:01 +01:00
|
|
|
if (a.width_ != this->width_ || a.height_ != this->height_) {
|
|
|
|
printf(
|
2017-06-08 19:20:21 +02:00
|
|
|
"Reference buffer of size %dx%d does not match this buffer which is "
|
|
|
|
"size %dx%d\n",
|
2016-12-10 01:51:01 +01:00
|
|
|
a.width_, a.height_, this->width_, this->height_);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-24 21:17:51 +02:00
|
|
|
} // namespace libvpx_test
|
|
|
|
#endif // TEST_BUFFER_H_
|