blob: c7d42ea9f89bbc231bd582d3fdd601c2e0d0d403 [file] [log] [blame]
kjellander@webrtc.org82d91ae2011-12-05 13:03:38 +00001/*
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.org5b97b122011-12-08 07:42:18 +000011#include "testsupport/metrics/video_metrics.h"
kjellander@webrtc.org82d91ae2011-12-05 13:03:38 +000012
13#include <cstdio>
14
15#include "gtest/gtest.h"
16#include "testsupport/fileutils.h"
17
18namespace webrtc {
19
20static const char* kEmptyFileName = "video_metrics_unittest_empty_file.tmp";
21static const char* kNonExistingFileName = "video_metrics_unittest_non_existing";
22static const int kWidth = 352;
23static const int kHeight = 288;
24
25static const int kMissingReferenceFileReturnCode = -1;
26static const int kMissingTestFileReturnCode = -2;
27static const int kEmptyFileReturnCode = -3;
28static const double kPsnrPerfectResult = std::numeric_limits<double>::max();
29static const double kSsimPerfectResult = 1.0;
30
31class VideoMetricsTest: public testing::Test {
32 protected:
33 VideoMetricsTest() {
34 video_file = webrtc::test::ProjectRootPath() + "resources/foreman_cif.yuv";
35 }
36 virtual ~VideoMetricsTest() {}
37 void SetUp() {
38 // Create an empty file:
39 FILE* dummy = fopen(kEmptyFileName, "wb");
40 fclose(dummy);
41 }
42 void TearDown() {
43 std::remove(kEmptyFileName);
44 }
45 QualityMetricsResult result_;
46 std::string video_file;
47};
48
49// Tests that it is possible to run with the same reference as test file
50TEST_F(VideoMetricsTest, ReturnsPerfectResultForIdenticalFiles) {
51 EXPECT_EQ(0, PsnrFromFiles(video_file.c_str(), video_file.c_str(),
52 kWidth, kHeight, &result_));
53 EXPECT_EQ(kPsnrPerfectResult, result_.average);
54 EXPECT_EQ(SsimFromFiles(video_file.c_str(), video_file.c_str(), kWidth, kHeight,
55 &result_), 0);
56 EXPECT_EQ(kSsimPerfectResult, result_.average);
57}
58
59// Tests that the right return code is given when the reference file is missing.
60TEST_F(VideoMetricsTest, MissingReferenceFile) {
61 EXPECT_EQ(kMissingReferenceFileReturnCode,
62 PsnrFromFiles(kNonExistingFileName, video_file.c_str(), kWidth,
63 kHeight, &result_));
64 EXPECT_EQ(kMissingReferenceFileReturnCode,
65 SsimFromFiles(kNonExistingFileName, video_file.c_str(), kWidth,
66 kHeight, &result_));
67}
68
69// Tests that the right return code is given when the test file is missing.
70TEST_F(VideoMetricsTest, MissingTestFile) {
71 EXPECT_EQ(kMissingTestFileReturnCode,
72 PsnrFromFiles(video_file.c_str(), kNonExistingFileName, kWidth,
73 kHeight, &result_));
74 EXPECT_EQ(kMissingTestFileReturnCode,
75 SsimFromFiles(video_file.c_str(), kNonExistingFileName, kWidth,
76 kHeight, &result_));
77}
78
79// Tests that the method can be executed with empty files.
80TEST_F(VideoMetricsTest, EmptyFiles) {
81 EXPECT_EQ(kEmptyFileReturnCode,
82 PsnrFromFiles(kEmptyFileName, video_file.c_str(), kWidth, kHeight,
83 &result_));
84 EXPECT_EQ(kEmptyFileReturnCode,
85 SsimFromFiles(kEmptyFileName, video_file.c_str(), kWidth, kHeight,
86 &result_));
87 // Run the same again with the empty file switched.
88 EXPECT_EQ(kEmptyFileReturnCode,
89 PsnrFromFiles(video_file.c_str(), kEmptyFileName, kWidth, kHeight,
90 &result_));
91 EXPECT_EQ(kEmptyFileReturnCode,
92 SsimFromFiles(video_file.c_str(), kEmptyFileName, kWidth, kHeight,
93 &result_));
94}
95
96} // namespace webrtc
97