libyuv: Adding psnr/ssim to libyuv and updating unit tests according to latest conventions.

Review URL: http://webrtc-codereview.appspot.com/331007

git-svn-id: http://webrtc.googlecode.com/svn/trunk@1253 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
mikhal@webrtc.org
2011-12-20 17:38:28 +00:00
parent 061fa5b828
commit 6f7fbc7fbe
8 changed files with 321 additions and 297 deletions

View File

@@ -0,0 +1,234 @@
/*
* Copyright (c) 2011 The WebRTC 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.
*/
#include <math.h>
#include <string.h>
#include "common_video/libyuv/include/scaler.h"
#include "gtest/gtest.h"
#include "system_wrappers/interface/tick_util.h"
#include "testsupport/fileutils.h"
namespace webrtc {
class TestScaler : public ::testing::Test {
protected:
TestScaler();
virtual void SetUp();
virtual void TearDown();
FILE* source_file_;
const int width_;
const int height_;
const int frame_length_;
};
void ScaleSequence(ScaleMethod method,
FILE* source_file, std::string out_name,
int src_width, int src_height,
int dst_width, int dst_height);
// TODO (mikhal): Use scoped_ptr when handling buffers.
TestScaler::TestScaler()
: source_file_(NULL),
width_(352),
height_(288),
frame_length_(CalcBufferSize(kI420, 352, 288)) {
}
void TestScaler::SetUp() {
const std::string input_file_name = webrtc::test::ProjectRootPath() +
"resources/foreman_cif.yuv";
source_file_ = fopen(input_file_name.c_str(), "rb");
ASSERT_TRUE(source_file_ != NULL) << "Cannot read file: "<<
input_file_name << "\n";
}
void TestScaler::TearDown() {
if (source_file_ != NULL) {
ASSERT_EQ(0, fclose(source_file_));
}
source_file_ = NULL;
}
TEST_F(TestScaler, ScaleSanityTest) {
Scaler test_scaler;
uint8_t* test_buffer = new uint8_t[frame_length_];
// Scaling without setting values
int size = 100;
EXPECT_EQ(-2, test_scaler.Scale(test_buffer, test_buffer, size));
// Setting bad initial values
EXPECT_EQ(-1, test_scaler.Set(0, 288, 352, 288, kI420, kI420, kScalePoint));
EXPECT_EQ(-1, test_scaler.Set(704, 0, 352, 288, kI420, kI420, kScaleBox));
EXPECT_EQ(-1, test_scaler.Set(704, 576, 352, 0, kI420, kI420,
kScaleBilinear));
EXPECT_EQ(-1, test_scaler.Set(704, 576, 0, 288, kI420, kI420, kScalePoint));
// Sending NULL pointer
size = 0;
EXPECT_EQ(-1, test_scaler.Scale(NULL, test_buffer, size));
// Sending a buffer which is too small (should reallocate and update size)
EXPECT_EQ(0, test_scaler.Set(352, 288, 144, 288, kI420, kI420, kScalePoint));
uint8_t* test_buffer2 = NULL;
size = 0;
EXPECT_GT(fread(test_buffer, 1, frame_length_, source_file_), 0U);
EXPECT_EQ(0, test_scaler.Scale(test_buffer, test_buffer2, size));
EXPECT_EQ(144 * 288 * 3 / 2, size);
delete [] test_buffer;
}
//TODO (mikhal): Converge the test into one function that accepts the method.
TEST_F(TestScaler, PointScaleTest) {
ScaleMethod method = kScalePoint;
std::string out_name = webrtc::test::OutputPath() +
"LibYuvTest_PointScale_176_144.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
width_ / 2, height_ / 2);
out_name = webrtc::test::OutputPath() + "LibYuvTest_PointScale_320_240.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
320, 240);
out_name = webrtc::test::OutputPath() + "LibYuvTest_PointScale_704_576.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
width_ * 2, height_ * 2);
out_name = webrtc::test::OutputPath() + "LibYuvTest_PointScale_300_200.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
300, 200);
out_name = webrtc::test::OutputPath() + "LibYuvTest_PointScale_400_300.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
400, 300);
}
TEST_F(TestScaler, BiLinearScaleTest) {
ScaleMethod method = kScaleBilinear;
std::string out_name = webrtc::test::OutputPath() +
"LibYuvTest_BilinearScale_176_144.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
width_ / 2, height_ / 2);
out_name = webrtc::test::OutputPath() +
"LibYuvTest_BilinearScale_320_240.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
320, 240);
out_name = webrtc::test::OutputPath() +
"LibYuvTest_BilinearScale_704_576.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
width_ * 2, height_ * 2);
out_name = webrtc::test::OutputPath() +
"LibYuvTest_BilinearScale_300_200.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
300, 200);
out_name = webrtc::test::OutputPath() +
"LibYuvTest_BilinearScale_400_300.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
400, 300);
}
TEST_F(TestScaler, BoxScaleTest) {
ScaleMethod method = kScaleBox;
std::string out_name = webrtc::test::OutputPath() +
"LibYuvTest_BoxScale_176_144.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
width_ / 2, height_ / 2);
out_name = webrtc::test::OutputPath() + "LibYuvTest_BoxScale_320_240.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
320, 240);
out_name = webrtc::test::OutputPath() + "LibYuvTest_BoxScale_704_576.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
width_ * 2, height_ * 2);
out_name = webrtc::test::OutputPath() + "LibYuvTest_BoxScale_300_200.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
300, 200);
out_name = webrtc::test::OutputPath() + "LibYuvTest_BoxScale_400_300.yuv";
ScaleSequence(method,
source_file_, out_name,
width_, height_,
400, 300);
}
// TODO (mikhal): Move part to a separate scale test.
void ScaleSequence(ScaleMethod method,
FILE* source_file, std::string out_name,
int src_width, int src_height,
int dst_width, int dst_height) {
Scaler test_scaler;
FILE* output_file;
EXPECT_EQ(0, test_scaler.Set(src_width, src_height,
dst_width, dst_height,
kI420, kI420, method));
output_file = fopen(out_name.c_str(), "wb");
ASSERT_TRUE(output_file != NULL);
rewind(source_file);
int out_required_size = dst_width * dst_height * 3 / 2;
int in_required_size = src_height * src_width * 3 / 2;
uint8_t* input_buffer = new uint8_t[in_required_size];
uint8_t* output_buffer = new uint8_t[out_required_size];
int64_t start_clock, total_clock;
total_clock = 0;
int frame_count = 0;
// Running through entire sequence
while (feof(source_file) == 0) {
if ((size_t)in_required_size !=
fread(input_buffer, 1, in_required_size, source_file))
break;
start_clock = TickTime::MillisecondTimestamp();
EXPECT_EQ(0, test_scaler.Scale(input_buffer, output_buffer,
out_required_size));
total_clock += TickTime::MillisecondTimestamp() - start_clock;
fwrite(output_buffer, out_required_size, 1, output_file);
frame_count++;
}
if (frame_count) {
printf("Scaling[%d %d] => [%d %d]: ",
src_width, src_height, dst_width, dst_height);
printf("Average time per frame[ms]: %.2lf\n",
(static_cast<double>(total_clock) / frame_count));
}
delete [] input_buffer;
delete [] output_buffer;
}
} // namespace