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

@ -306,6 +306,14 @@ int ConvertToI420AndRotate(const uint8_t* src_frame,
int width,
VideoType src_video_type,
VideoRotationMode mode);
// Compute PSNR for an I420 frame (all planes).
double I420PSNR(const uint8_t* ref_frame,
const uint8_t* test_frame,
int width, int height);
// Compute SSIM for an I420 frame (all planes).
double I420SSIM(const uint8_t* ref_frame,
const uint8_t* test_frame,
int width, int height);
}
#endif // WEBRTC_COMMON_VIDEO_LIBYUV_INCLUDE_LIBYUV_H_

View File

@ -891,4 +891,58 @@ int ConvertToI420AndRotate180(const uint8_t* src_frame,
src_width, src_height,
libyuv::kRotate180);
}
// Compute PSNR for an I420 frame (all planes)
double I420PSNR(const uint8_t* ref_frame,
const uint8_t* test_frame,
int width, int height) {
if (!ref_frame || !test_frame)
return -1;
else if (height < 0 || width < 0)
return -1;
const uint8_t* src_y_a = ref_frame;
const uint8_t* src_u_a = src_y_a + width * height;
const uint8_t* src_v_a = src_u_a + (width * height / 4);
const uint8_t* src_y_b = test_frame;
const uint8_t* src_u_b = src_y_b + width * height;
const uint8_t* src_v_b = src_u_b + (width * height / 4);
int stride_y = width;
int stride_uv = (width + 1) / 2;
double psnr = libyuv::I420Psnr(src_y_a, stride_y,
src_u_a, stride_uv,
src_v_a, stride_uv,
src_y_b, stride_y,
src_u_b, stride_uv,
src_v_b, stride_uv,
width, height);
// LibYuv sets the max psnr value to 128, we restrict it to 48.
// In case of 0 mse in one frame, 128 can skew the results significantly.
return (psnr > 48.0) ? 48.0 : psnr;
}
// Compute SSIM for an I420 frame (all planes)
double I420SSIM(const uint8_t* ref_frame,
const uint8_t* test_frame,
int width, int height) {
if (!ref_frame || !test_frame)
return -1;
else if (height < 0 || width < 0)
return -1;
const uint8_t* src_y_a = ref_frame;
const uint8_t* src_u_a = src_y_a + width * height;
const uint8_t* src_v_a = src_u_a + (width * height / 4);
const uint8_t* src_y_b = test_frame;
const uint8_t* src_u_b = src_y_b + width * height;
const uint8_t* src_v_b = src_u_b + (width * height / 4);
int stride_y = width;
int stride_uv = (width + 1) / 2;
return libyuv::I420Ssim(src_y_a, stride_y,
src_u_a, stride_uv,
src_v_a, stride_uv,
src_y_b, stride_y,
src_u_b, stride_uv,
src_v_b, stride_uv,
width, height);
}
} // namespace webrtc

View File

@ -38,9 +38,8 @@
'<(webrtc_root)/../test/test.gyp:test_support_main',
],
'sources': [
'test/test_util.h',
'test/test_util.cc',
'test/unit_test.cc',
'libyuv_unittest.cc',
'scaler_unittest.cc',
],
},
], # targets

View File

@ -0,0 +1,246 @@
/*
* 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/libyuv.h"
#include "gtest/gtest.h"
#include "system_wrappers/interface/tick_util.h"
#include "testsupport/fileutils.h"
namespace webrtc {
int PrintFrame(const uint8_t* frame, int width, int height) {
if (frame == NULL)
return -1;
int k = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
printf("%d ", frame[k++]);
}
printf(" \n");
}
printf(" \n");
return 0;
}
int PrintFrame(const uint8_t* frame, int width,
int height, const char* str) {
if (frame == NULL)
return -1;
printf("%s %dx%d \n", str, width, height);
const uint8_t* frame_y = frame;
const uint8_t* frame_u = frame_y + width * height;
const uint8_t* frame_v = frame_u + width * height / 4;
int ret = 0;
ret += PrintFrame(frame_y, width, height);
ret += PrintFrame(frame_u, width / 2, height / 2);
ret += PrintFrame(frame_v, width / 2, height / 2);
return ret;
}
void CreateImage(int width, int height,
uint8_t* frame, int offset,
int height_factor, int width_factor) {
if (frame == NULL)
return;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
*frame = static_cast<uint8_t>((i + offset) * height_factor
+ j * width_factor);
frame++;
}
}
}
class TestLibYuv : public ::testing::Test {
protected:
TestLibYuv();
virtual void SetUp();
virtual void TearDown();
FILE* source_file_;
const int width_;
const int height_;
const int frame_length_;
};
// TODO (mikhal): Use scoped_ptr when handling buffers.
TestLibYuv::TestLibYuv()
: source_file_(NULL),
width_(352),
height_(288),
frame_length_(CalcBufferSize(kI420, 352, 288)) {
}
void TestLibYuv::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 TestLibYuv::TearDown() {
if (source_file_ != NULL) {
ASSERT_EQ(0, fclose(source_file_));
}
source_file_ = NULL;
}
TEST_F(TestLibYuv, ConvertSanityTest) {
// TODO(mikhal)
}
TEST_F(TestLibYuv, ConvertTest) {
// Reading YUV frame - testing on the first frame of the foreman sequence
int j = 0;
std::string output_file_name = webrtc::test::OutputPath() +
"LibYuvTest_conversion.yuv";
FILE* output_file = fopen(output_file_name.c_str(), "wb");
ASSERT_TRUE(output_file != NULL);
double psnr = 0;
uint8_t* orig_buffer = new uint8_t[frame_length_];
EXPECT_GT(fread(orig_buffer, 1, frame_length_, source_file_), 0U);
// printf("\nConvert #%d I420 <-> RGB24\n", j);
uint8_t* res_rgb_buffer2 = new uint8_t[width_ * height_ * 3];
uint8_t* res_i420_buffer = new uint8_t[frame_length_];
EXPECT_EQ(0, ConvertFromI420(kRGB24, orig_buffer, width_, height_,
res_rgb_buffer2, false, kRotateNone));
EXPECT_EQ(0, ConvertToI420(kRGB24, res_rgb_buffer2, width_, height_,
res_i420_buffer, false, kRotateNone));
fwrite(res_i420_buffer, frame_length_, 1, output_file);
//ImagePSNRfromBuffer(orig_buffer, res_i420_buffer, width_, height_, &psnr);
psnr = I420PSNR(orig_buffer, res_i420_buffer, width_, height_);
// Optimization Speed- quality trade-off => 45 dB only.
EXPECT_GT(ceil(psnr), 45);
j++;
delete [] res_rgb_buffer2;
// printf("\nConvert #%d I420 <-> UYVY\n", j);
uint8_t* out_uyvy_buffer = new uint8_t[width_ * height_ * 2];
EXPECT_EQ(0, ConvertFromI420(kUYVY, orig_buffer, width_,
height_, out_uyvy_buffer, false, kRotateNone));
EXPECT_EQ(0, ConvertToI420(kUYVY, out_uyvy_buffer, width_, height_,
res_i420_buffer, false, kRotateNone));
//ImagePSNRfromBuffer(orig_buffer, res_i420_buffer, width_, height_, &psnr);
psnr = I420PSNR(orig_buffer, res_i420_buffer, width_, height_);
EXPECT_EQ(48.0, psnr);
fwrite(res_i420_buffer, frame_length_, 1, output_file);
j++;
delete [] out_uyvy_buffer;
// printf("\nConvert #%d I420 <-> I420 \n", j);
uint8_t* out_i420_buffer = new uint8_t[width_ * height_ * 3 / 2 ];
EXPECT_EQ(0, ConvertToI420(kI420, orig_buffer, width_, height_,
out_i420_buffer, false, kRotateNone));
EXPECT_EQ(0, ConvertFromI420(kI420 , out_i420_buffer, width_, height_,
res_i420_buffer, false, kRotateNone));
fwrite(res_i420_buffer, frame_length_, 1, output_file);
//ImagePSNRfromBuffer(orig_buffer, res_i420_buffer, width_, height_, &psnr);
psnr = I420PSNR(orig_buffer, res_i420_buffer, width_, height_);
EXPECT_EQ(48.0, psnr);
j++;
delete [] out_i420_buffer;
// printf("\nConvert #%d I420 <-> YV12\n", j);
uint8_t* outYV120Buffer = new uint8_t[frame_length_];
EXPECT_EQ(0, ConvertFromI420(kYV12, orig_buffer, width_, height_,
outYV120Buffer, false, kRotateNone));
EXPECT_EQ(0, ConvertYV12ToI420(outYV120Buffer, width_, height_,
res_i420_buffer));
fwrite(res_i420_buffer, frame_length_, 1, output_file);
//ImagePSNRfromBuffer(orig_buffer, res_i420_buffer, width_, height_, &psnr);
psnr = I420PSNR(orig_buffer, res_i420_buffer, width_, height_);
EXPECT_EQ(48.0, psnr);
j++;
delete [] outYV120Buffer;
// printf("\nConvert #%d I420 <-> YUY2\n", j);
uint8_t* out_yuy2_buffer = new uint8_t[width_ * height_ * 2];
EXPECT_EQ(0, ConvertFromI420(kYUY2, orig_buffer, width_, height_,
out_yuy2_buffer, false, kRotateNone));
EXPECT_EQ(0, ConvertToI420(kYUY2, out_yuy2_buffer, width_, height_,
res_i420_buffer, false, kRotateNone));
fwrite(res_i420_buffer, frame_length_, 1, output_file);
//ImagePSNRfromBuffer(orig_buffer, res_i420_buffer, width_, height_, &psnr);
psnr = I420PSNR(orig_buffer, res_i420_buffer, width_, height_);
EXPECT_EQ(48.0, psnr);
delete [] out_yuy2_buffer;
delete [] res_i420_buffer;
delete [] orig_buffer;
}
TEST_F(TestLibYuv, MirrorTest) {
// TODO (mikhal): Add an automated test to confirm output.
std::string str;
int width = 16;
int height = 8;
int factor_y = 1;
int factor_u = 1;
int factor_v = 1;
int start_buffer_offset = 10;
int length = webrtc::CalcBufferSize(kI420, width, height);
uint8_t* test_frame = new uint8_t[length];
memset(test_frame, 255, length);
// Create input frame
uint8_t* in_frame = test_frame;
uint8_t* in_frame_cb = in_frame + width * height;
uint8_t* in_frame_cr = in_frame_cb + (width * height) / 4;
CreateImage(width, height, in_frame, 10, factor_y, 1); // Y
CreateImage(width / 2, height / 2, in_frame_cb, 100, factor_u, 1); // Cb
CreateImage(width / 2, height / 2, in_frame_cr, 200, factor_v, 1); // Cr
EXPECT_EQ(0, PrintFrame(test_frame, width, height, "InputFrame"));
uint8_t* test_frame2 = new uint8_t[length + start_buffer_offset * 2];
memset(test_frame2, 255, length + start_buffer_offset * 2);
uint8_t* out_frame = test_frame2;
// LeftRight
std::cout << "Test Mirror function: LeftRight" << std::endl;
EXPECT_EQ(0, MirrorI420LeftRight(in_frame, out_frame, width, height));
EXPECT_EQ(0, PrintFrame(test_frame2, width, height, "OutputFrame"));
EXPECT_EQ(0, MirrorI420LeftRight(out_frame, test_frame, width, height));
EXPECT_EQ(0, memcmp(in_frame, test_frame, length));
// UpDown
std::cout << "Test Mirror function: UpDown" << std::endl;
EXPECT_EQ(0, MirrorI420UpDown(in_frame, out_frame, width, height));
EXPECT_EQ(0, PrintFrame(test_frame2, width, height, "OutputFrame"));
EXPECT_EQ(0, MirrorI420UpDown(out_frame, test_frame, width, height));
EXPECT_EQ(0, memcmp(in_frame, test_frame, length));
// TODO(mikhal): Write to a file, and ask to look at the file.
std::cout << "Do the mirrored frames look correct?" << std::endl;
delete [] test_frame;
delete [] test_frame2;
}
} // namespace

View File

@ -11,18 +11,16 @@
#include <math.h>
#include <string.h>
#include "testsupport/fileutils.h"
#include "gtest/gtest.h"
#include "common_video/libyuv/include/libyuv.h"
#include "common_video/libyuv/include/scaler.h"
#include "common_video/libyuv/test/test_util.h"
#include "gtest/gtest.h"
#include "system_wrappers/interface/tick_util.h"
#include "testsupport/fileutils.h"
namespace webrtc {
class LibYuvTest : public ::testing::Test {
class TestScaler : public ::testing::Test {
protected:
LibYuvTest();
TestScaler();
virtual void SetUp();
virtual void TearDown();
@ -38,14 +36,14 @@ void ScaleSequence(ScaleMethod method,
int dst_width, int dst_height);
// TODO (mikhal): Use scoped_ptr when handling buffers.
LibYuvTest::LibYuvTest()
TestScaler::TestScaler()
: source_file_(NULL),
width_(352),
height_(288),
frame_length_(CalcBufferSize(kI420, 352, 288)) {
}
void LibYuvTest::SetUp() {
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");
@ -53,18 +51,14 @@ void LibYuvTest::SetUp() {
input_file_name << "\n";
}
void LibYuvTest::TearDown() {
void TestScaler::TearDown() {
if (source_file_ != NULL) {
ASSERT_EQ(0, fclose(source_file_));
}
source_file_ = NULL;
}
TEST_F(LibYuvTest, ConvertSanityTest) {
// TODO(mikhal)
}
TEST_F(LibYuvTest, ScaleSanityTest) {
TEST_F(TestScaler, ScaleSanityTest) {
Scaler test_scaler;
uint8_t* test_buffer = new uint8_t[frame_length_];
// Scaling without setting values
@ -93,107 +87,8 @@ TEST_F(LibYuvTest, ScaleSanityTest) {
delete [] test_buffer;
}
TEST_F(LibYuvTest, MirrorSanityTest) {
// TODO (mikhal): look into scoped_ptr for implementation
// Sending NULL pointers
uint8_t* test_buffer1 = new uint8_t[frame_length_];
uint8_t* test_buffer2 = new uint8_t[frame_length_];
// Setting bad initial values
EXPECT_EQ(-1, MirrorI420LeftRight(test_buffer1, test_buffer2, -352, height_));
EXPECT_EQ(-1, MirrorI420LeftRight(NULL, test_buffer2, width_, height_));
EXPECT_EQ(-1, MirrorI420LeftRight(test_buffer1, NULL, width_, height_));
delete [] test_buffer1;
delete [] test_buffer2;
}
TEST_F(LibYuvTest, ConvertTest) {
// Reading YUV frame - testing on the first frame of the foreman sequence
int j = 0;
std::string output_file_name = webrtc::test::OutputPath() +
"LibYuvTest_conversion.yuv";
FILE* output_file = fopen(output_file_name.c_str(), "wb");
ASSERT_TRUE(output_file != NULL);
double psnr = 0;
uint8_t* orig_buffer = new uint8_t[frame_length_];
EXPECT_GT(fread(orig_buffer, 1, frame_length_, source_file_), 0U);
// printf("\nConvert #%d I420 <-> RGB24\n", j);
uint8_t* res_rgb_buffer2 = new uint8_t[width_ * height_ * 3];
uint8_t* res_i420_buffer = new uint8_t[frame_length_];
EXPECT_EQ(0, ConvertFromI420(kRGB24, orig_buffer, width_, height_,
res_rgb_buffer2, false, kRotateNone));
EXPECT_EQ(0, ConvertToI420(kRGB24, res_rgb_buffer2, width_, height_,
res_i420_buffer, false, kRotateNone));
fwrite(res_i420_buffer, frame_length_, 1, output_file);
ImagePSNRfromBuffer(orig_buffer, res_i420_buffer, width_, height_, &psnr);
// Optimization Speed- quality trade-off => 45 dB only.
EXPECT_GT(ceil(psnr), 45);
j++;
delete [] res_rgb_buffer2;
// printf("\nConvert #%d I420 <-> UYVY\n", j);
uint8_t* out_uyvy_buffer = new uint8_t[width_ * height_ * 2];
EXPECT_EQ(0, ConvertFromI420(kUYVY, orig_buffer, width_,
height_, out_uyvy_buffer, false, kRotateNone));
EXPECT_EQ(0, ConvertToI420(kUYVY, out_uyvy_buffer, width_, height_,
res_i420_buffer, false, kRotateNone));
ImagePSNRfromBuffer(orig_buffer, res_i420_buffer, width_, height_, &psnr);
EXPECT_EQ(48.0, psnr);
fwrite(res_i420_buffer, frame_length_, 1, output_file);
j++;
delete [] out_uyvy_buffer;
// printf("\nConvert #%d I420 <-> I420 \n", j);
uint8_t* out_i420_buffer = new uint8_t[width_ * height_ * 3 / 2 ];
EXPECT_EQ(0, ConvertToI420(kI420, orig_buffer, width_, height_,
out_i420_buffer, false, kRotateNone));
EXPECT_EQ(0, ConvertFromI420(kI420 , out_i420_buffer, width_, height_,
res_i420_buffer, false, kRotateNone));
fwrite(res_i420_buffer, frame_length_, 1, output_file);
ImagePSNRfromBuffer(orig_buffer, res_i420_buffer, width_, height_, &psnr);
EXPECT_EQ(48.0, psnr);
j++;
delete [] out_i420_buffer;
// printf("\nConvert #%d I420 <-> YV12\n", j);
uint8_t* outYV120Buffer = new uint8_t[frame_length_];
EXPECT_EQ(0, ConvertFromI420(kYV12, orig_buffer, width_, height_,
outYV120Buffer, false, kRotateNone));
EXPECT_EQ(0, ConvertYV12ToI420(outYV120Buffer, width_, height_,
res_i420_buffer));
fwrite(res_i420_buffer, frame_length_, 1, output_file);
ImagePSNRfromBuffer(orig_buffer, res_i420_buffer, width_, height_, &psnr);
EXPECT_EQ(48.0, psnr);
j++;
delete [] outYV120Buffer;
// printf("\nConvert #%d I420 <-> YUY2\n", j);
uint8_t* out_yuy2_buffer = new uint8_t[width_ * height_ * 2];
EXPECT_EQ(0, ConvertFromI420(kYUY2, orig_buffer, width_, height_,
out_yuy2_buffer, false, kRotateNone));
EXPECT_EQ(0, ConvertToI420(kYUY2, out_yuy2_buffer, width_, height_,
res_i420_buffer, false, kRotateNone));
fwrite(res_i420_buffer, frame_length_, 1, output_file);
ImagePSNRfromBuffer(orig_buffer, res_i420_buffer, width_, height_, &psnr);
EXPECT_EQ(48.0, psnr);
delete [] out_yuy2_buffer;
delete [] res_i420_buffer;
delete [] orig_buffer;
}
//TODO (mikhal): Converge the test into one function that accepts the method.
TEST_F(LibYuvTest, PointScaleTest) {
TEST_F(TestScaler, PointScaleTest) {
ScaleMethod method = kScalePoint;
std::string out_name = webrtc::test::OutputPath() +
"LibYuvTest_PointScale_176_144.yuv";
@ -223,7 +118,7 @@ TEST_F(LibYuvTest, PointScaleTest) {
400, 300);
}
TEST_F(LibYuvTest, BiLinearScaleTest) {
TEST_F(TestScaler, BiLinearScaleTest) {
ScaleMethod method = kScaleBilinear;
std::string out_name = webrtc::test::OutputPath() +
"LibYuvTest_BilinearScale_176_144.yuv";
@ -257,7 +152,7 @@ TEST_F(LibYuvTest, BiLinearScaleTest) {
400, 300);
}
TEST_F(LibYuvTest, BoxScaleTest) {
TEST_F(TestScaler, BoxScaleTest) {
ScaleMethod method = kScaleBox;
std::string out_name = webrtc::test::OutputPath() +
"LibYuvTest_BoxScale_176_144.yuv";
@ -287,56 +182,6 @@ TEST_F(LibYuvTest, BoxScaleTest) {
400, 300);
}
TEST_F(LibYuvTest, MirrorTest) {
// TODO (mikhal): Add an automated test to confirm output.
std::string str;
int width = 16;
int height = 8;
int factor_y = 1;
int factor_u = 1;
int factor_v = 1;
int start_buffer_offset = 10;
int length = webrtc::CalcBufferSize(kI420, width, height);
uint8_t* test_frame = new uint8_t[length];
memset(test_frame, 255, length);
// Create input frame
uint8_t* in_frame = test_frame;
uint8_t* in_frame_cb = in_frame + width * height;
uint8_t* in_frame_cr = in_frame_cb + (width * height) / 4;
CreateImage(width, height, in_frame, 10, factor_y, 1); // Y
CreateImage(width / 2, height / 2, in_frame_cb, 100, factor_u, 1); // Cb
CreateImage(width / 2, height / 2, in_frame_cr, 200, factor_v, 1); // Cr
EXPECT_EQ(0, PrintFrame(test_frame, width, height, "InputFrame"));
uint8_t* test_frame2 = new uint8_t[length + start_buffer_offset * 2];
memset(test_frame2, 255, length + start_buffer_offset * 2);
uint8_t* out_frame = test_frame2;
// LeftRight
std::cout << "Test Mirror function: LeftRight" << std::endl;
EXPECT_EQ(0, MirrorI420LeftRight(in_frame, out_frame, width, height));
EXPECT_EQ(0, PrintFrame(test_frame2, width, height, "OutputFrame"));
EXPECT_EQ(0, MirrorI420LeftRight(out_frame, test_frame, width, height));
EXPECT_EQ(0, memcmp(in_frame, test_frame, length));
// UpDown
std::cout << "Test Mirror function: UpDown" << std::endl;
EXPECT_EQ(0, MirrorI420UpDown(in_frame, out_frame, width, height));
EXPECT_EQ(0, PrintFrame(test_frame2, width, height, "OutputFrame"));
EXPECT_EQ(0, MirrorI420UpDown(out_frame, test_frame, width, height));
EXPECT_EQ(0, memcmp(in_frame, test_frame, length));
// TODO(mikhal): Write to a file, and ask to look at the file.
std::cout << "Do the mirrored frames look correct?" << std::endl;
delete [] test_frame;
delete [] test_frame2;
}
// TODO (mikhal): Move part to a separate scale test.
void ScaleSequence(ScaleMethod method,
FILE* source_file, std::string out_name,

View File

@ -1,97 +0,0 @@
/*
* 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 "common_video/libyuv/test/test_util.h"
#include <math.h>
#include <stdio.h>
namespace webrtc {
int PrintFrame(const uint8_t* frame, int width, int height) {
if (frame == NULL)
return -1;
int k = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
printf("%d ", frame[k++]);
}
printf(" \n");
}
printf(" \n");
return 0;
}
int PrintFrame(const uint8_t* frame, int width,
int height, const char* str) {
if (frame == NULL)
return -1;
printf("%s %dx%d \n", str, width, height);
const uint8_t* frame_y = frame;
const uint8_t* frame_u = frame_y + width * height;
const uint8_t* frame_v = frame_u + width * height / 4;
int ret = 0;
ret += PrintFrame(frame_y, width, height);
ret += PrintFrame(frame_u, width / 2, height / 2);
ret += PrintFrame(frame_v, width / 2, height / 2);
return ret;
}
void CreateImage(int width, int height,
uint8_t* frame, int offset,
int height_factor, int width_factor) {
if (frame == NULL)
return;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
*frame = static_cast<uint8_t>((i + offset) * height_factor
+ j * width_factor);
frame++;
}
}
}
// TODO (mikhal): Following update to latest version, use PSNR tool from libyuv.
int ImagePSNRfromBuffer(const uint8_t* ref_frame,
const uint8_t* test_frame,
int width, int height, double* YPSNRptr) {
if (height <= 0 || width <= 0 || ref_frame == NULL || test_frame == NULL)
return -1;
// Assumes I420, one frame
double mse = 0.0;
double mse_log_sum = 0.0;
const uint8_t *ref = ref_frame;
const uint8_t *test = test_frame;
mse = 0.0;
// Calculate Y sum-square-difference.
for ( int k = 0; k < width * height; k++ ) {
mse += (test[k] - ref[k]) * (test[k] - ref[k]);
}
// Divide by number of pixels.
mse /= static_cast<double> (width * height);
if (mse == 0) {
*YPSNRptr = 48;
return 0;
}
// Accumulate for total average
mse_log_sum += log10(mse);
*YPSNRptr = 20.0 * log10(255.0) - 10.0 * mse_log_sum;
return 0;
}
} // namespace webrtc

View File

@ -1,31 +0,0 @@
/*
* 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.
*/
#ifndef WEBRTC_COMMON_VIDEO_LIBYUV_TEST_TEST_UTIL_H_
#define WEBRTC_COMMON_VIDEO_LIBYUV_TEST_TEST_UTIL_H_
#include "typedefs.h"
namespace webrtc {
int PrintFrame(const uint8_t* frame, int width, int height);
int PrintFrame(const uint8_t* frame, int width, int height, const char* str);
void CreateImage(int width, int height,
uint8_t* frame, int offset,
int height_factor, int width_factor);
int ImagePSNRfromBuffer(const uint8_t *ref_frame,
const uint8_t *test_frame,
int width, int height,
double *YPSNRptr);
} // namespace webrtc
#endif // WEBRTC_COMMON_VIDEO_LIBYUV_TEST_TEST_UTIL_H_