kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC 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 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 11 | #include "testsupport/metrics/video_metrics.h" |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 12 | |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
| 14 | #include "testsupport/fileutils.h" |
| 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | static const char* kEmptyFileName = "video_metrics_unittest_empty_file.tmp"; |
| 19 | static const char* kNonExistingFileName = "video_metrics_unittest_non_existing"; |
| 20 | static const int kWidth = 352; |
| 21 | static const int kHeight = 288; |
| 22 | |
| 23 | static const int kMissingReferenceFileReturnCode = -1; |
| 24 | static const int kMissingTestFileReturnCode = -2; |
| 25 | static const int kEmptyFileReturnCode = -3; |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 26 | static const double kPsnrPerfectResult = 48.0; |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 27 | static const double kSsimPerfectResult = 1.0; |
| 28 | |
| 29 | class VideoMetricsTest: public testing::Test { |
| 30 | protected: |
| 31 | VideoMetricsTest() { |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 32 | video_file_ = webrtc::test::ResourcePath("foreman_cif_short", "yuv"); |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 33 | } |
| 34 | virtual ~VideoMetricsTest() {} |
| 35 | void SetUp() { |
| 36 | // Create an empty file: |
| 37 | FILE* dummy = fopen(kEmptyFileName, "wb"); |
| 38 | fclose(dummy); |
| 39 | } |
| 40 | void TearDown() { |
| 41 | std::remove(kEmptyFileName); |
| 42 | } |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 43 | webrtc::test::QualityMetricsResult psnr_result_; |
| 44 | webrtc::test::QualityMetricsResult ssim_result_; |
| 45 | std::string video_file_; |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | // Tests that it is possible to run with the same reference as test file |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 49 | TEST_F(VideoMetricsTest, ReturnsPerfectResultForIdenticalFilesPSNR) { |
| 50 | EXPECT_EQ(0, I420PSNRFromFiles(video_file_.c_str(), video_file_.c_str(), |
| 51 | kWidth, kHeight, &psnr_result_)); |
| 52 | EXPECT_EQ(kPsnrPerfectResult, psnr_result_.average); |
| 53 | } |
| 54 | |
| 55 | TEST_F(VideoMetricsTest, ReturnsPerfectResultForIdenticalFilesSSIM) { |
| 56 | EXPECT_EQ(0, I420SSIMFromFiles(video_file_.c_str(), video_file_.c_str(), |
| 57 | kWidth, kHeight, &ssim_result_)); |
| 58 | EXPECT_EQ(kSsimPerfectResult, ssim_result_.average); |
| 59 | } |
| 60 | |
| 61 | TEST_F(VideoMetricsTest, ReturnsPerfectResultForIdenticalFilesBothMetrics) { |
| 62 | EXPECT_EQ(0, I420MetricsFromFiles(video_file_.c_str(), video_file_.c_str(), |
| 63 | kWidth, kHeight, &psnr_result_, |
| 64 | &ssim_result_)); |
| 65 | EXPECT_EQ(kPsnrPerfectResult, psnr_result_.average); |
| 66 | EXPECT_EQ(kSsimPerfectResult, ssim_result_.average); |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Tests that the right return code is given when the reference file is missing. |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 70 | TEST_F(VideoMetricsTest, MissingReferenceFilePSNR) { |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 71 | EXPECT_EQ(kMissingReferenceFileReturnCode, |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 72 | I420PSNRFromFiles(kNonExistingFileName, video_file_.c_str(), |
| 73 | kWidth, kHeight, &ssim_result_)); |
| 74 | } |
| 75 | |
| 76 | TEST_F(VideoMetricsTest, MissingReferenceFileSSIM) { |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 77 | EXPECT_EQ(kMissingReferenceFileReturnCode, |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 78 | I420SSIMFromFiles(kNonExistingFileName, video_file_.c_str(), |
| 79 | kWidth, kHeight, &ssim_result_)); |
| 80 | } |
| 81 | |
| 82 | TEST_F(VideoMetricsTest, MissingReferenceFileBothMetrics) { |
| 83 | EXPECT_EQ(kMissingReferenceFileReturnCode, |
| 84 | I420MetricsFromFiles(kNonExistingFileName, video_file_.c_str(), |
| 85 | kWidth, kHeight, |
| 86 | &psnr_result_, &ssim_result_)); |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Tests that the right return code is given when the test file is missing. |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 90 | TEST_F(VideoMetricsTest, MissingTestFilePSNR) { |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 91 | EXPECT_EQ(kMissingTestFileReturnCode, |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 92 | I420PSNRFromFiles(video_file_.c_str(), kNonExistingFileName, |
| 93 | kWidth, kHeight, &ssim_result_)); |
| 94 | } |
| 95 | |
| 96 | TEST_F(VideoMetricsTest, MissingTestFileSSIM) { |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 97 | EXPECT_EQ(kMissingTestFileReturnCode, |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 98 | I420SSIMFromFiles(video_file_.c_str(), kNonExistingFileName, |
| 99 | kWidth, kHeight, &ssim_result_)); |
| 100 | } |
| 101 | |
| 102 | TEST_F(VideoMetricsTest, MissingTestFileBothMetrics) { |
| 103 | EXPECT_EQ(kMissingTestFileReturnCode, |
| 104 | I420MetricsFromFiles(video_file_.c_str(), kNonExistingFileName, |
| 105 | kWidth, kHeight, |
| 106 | &psnr_result_, &ssim_result_)); |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // Tests that the method can be executed with empty files. |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 110 | TEST_F(VideoMetricsTest, EmptyFilesPSNR) { |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 111 | EXPECT_EQ(kEmptyFileReturnCode, |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 112 | I420PSNRFromFiles(kEmptyFileName, video_file_.c_str(), |
| 113 | kWidth, kHeight, &ssim_result_)); |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 114 | EXPECT_EQ(kEmptyFileReturnCode, |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 115 | I420PSNRFromFiles(video_file_.c_str(), kEmptyFileName, |
| 116 | kWidth, kHeight, &ssim_result_)); |
| 117 | } |
| 118 | |
| 119 | TEST_F(VideoMetricsTest, EmptyFilesSSIM) { |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 120 | EXPECT_EQ(kEmptyFileReturnCode, |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 121 | I420SSIMFromFiles(kEmptyFileName, video_file_.c_str(), |
| 122 | kWidth, kHeight, &ssim_result_)); |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 123 | EXPECT_EQ(kEmptyFileReturnCode, |
kjellander@webrtc.org | cc33737 | 2012-01-04 08:09:32 +0000 | [diff] [blame^] | 124 | I420SSIMFromFiles(video_file_.c_str(), kEmptyFileName, |
| 125 | kWidth, kHeight, &ssim_result_)); |
| 126 | } |
| 127 | |
| 128 | TEST_F(VideoMetricsTest, EmptyFilesBothMetrics) { |
| 129 | EXPECT_EQ(kEmptyFileReturnCode, |
| 130 | I420MetricsFromFiles(kEmptyFileName, video_file_.c_str(), |
| 131 | kWidth, kHeight, |
| 132 | &psnr_result_, &ssim_result_)); |
| 133 | EXPECT_EQ(kEmptyFileReturnCode, |
| 134 | I420MetricsFromFiles(video_file_.c_str(), kEmptyFileName, |
| 135 | kWidth, kHeight, |
| 136 | &psnr_result_, &ssim_result_)); |
kjellander@webrtc.org | 82d91ae | 2011-12-05 13:03:38 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | } // namespace webrtc |