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
11#include "testsupport/metrics/video_metrics.h"
12
13#include "gtest/gtest.h"
14#include "testsupport/fileutils.h"
15
16namespace webrtc {
17
18static const char* kEmptyFileName = "video_metrics_unittest_empty_file.tmp";
19static const char* kNonExistingFileName = "video_metrics_unittest_non_existing";
20static const int kWidth = 352;
21static const int kHeight = 288;
22
23static const int kMissingReferenceFileReturnCode = -1;
24static const int kMissingTestFileReturnCode = -2;
25static const int kEmptyFileReturnCode = -3;
26static const double kPsnrPerfectResult =  48.0;
27static const double kSsimPerfectResult = 1.0;
28
29class VideoMetricsTest: public testing::Test {
30 protected:
31  VideoMetricsTest() {
32    video_file_ = webrtc::test::ResourcePath("foreman_cif_short", "yuv");
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  }
43  webrtc::test::QualityMetricsResult psnr_result_;
44  webrtc::test::QualityMetricsResult ssim_result_;
45  std::string video_file_;
46};
47
48// Tests that it is possible to run with the same reference as test file
49TEST_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
55TEST_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
61TEST_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);
67}
68
69// Tests that the right return code is given when the reference file is missing.
70TEST_F(VideoMetricsTest, MissingReferenceFilePSNR) {
71  EXPECT_EQ(kMissingReferenceFileReturnCode,
72            I420PSNRFromFiles(kNonExistingFileName, video_file_.c_str(),
73                              kWidth, kHeight, &ssim_result_));
74}
75
76TEST_F(VideoMetricsTest, MissingReferenceFileSSIM) {
77  EXPECT_EQ(kMissingReferenceFileReturnCode,
78            I420SSIMFromFiles(kNonExistingFileName, video_file_.c_str(),
79                              kWidth, kHeight, &ssim_result_));
80}
81
82TEST_F(VideoMetricsTest, MissingReferenceFileBothMetrics) {
83  EXPECT_EQ(kMissingReferenceFileReturnCode,
84            I420MetricsFromFiles(kNonExistingFileName, video_file_.c_str(),
85                                 kWidth, kHeight,
86                                 &psnr_result_, &ssim_result_));
87}
88
89// Tests that the right return code is given when the test file is missing.
90TEST_F(VideoMetricsTest, MissingTestFilePSNR) {
91  EXPECT_EQ(kMissingTestFileReturnCode,
92            I420PSNRFromFiles(video_file_.c_str(), kNonExistingFileName,
93                              kWidth, kHeight, &ssim_result_));
94}
95
96TEST_F(VideoMetricsTest, MissingTestFileSSIM) {
97  EXPECT_EQ(kMissingTestFileReturnCode,
98            I420SSIMFromFiles(video_file_.c_str(), kNonExistingFileName,
99                              kWidth, kHeight, &ssim_result_));
100}
101
102TEST_F(VideoMetricsTest, MissingTestFileBothMetrics) {
103  EXPECT_EQ(kMissingTestFileReturnCode,
104            I420MetricsFromFiles(video_file_.c_str(), kNonExistingFileName,
105                                 kWidth, kHeight,
106                                 &psnr_result_, &ssim_result_));
107}
108
109// Tests that the method can be executed with empty files.
110TEST_F(VideoMetricsTest, EmptyFilesPSNR) {
111  EXPECT_EQ(kEmptyFileReturnCode,
112            I420PSNRFromFiles(kEmptyFileName, video_file_.c_str(),
113                              kWidth, kHeight, &ssim_result_));
114  EXPECT_EQ(kEmptyFileReturnCode,
115            I420PSNRFromFiles(video_file_.c_str(), kEmptyFileName,
116                              kWidth, kHeight, &ssim_result_));
117}
118
119TEST_F(VideoMetricsTest, EmptyFilesSSIM) {
120  EXPECT_EQ(kEmptyFileReturnCode,
121            I420SSIMFromFiles(kEmptyFileName, video_file_.c_str(),
122                              kWidth, kHeight, &ssim_result_));
123  EXPECT_EQ(kEmptyFileReturnCode,
124            I420SSIMFromFiles(video_file_.c_str(), kEmptyFileName,
125                              kWidth, kHeight, &ssim_result_));
126}
127
128TEST_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_));
137}
138
139}  // namespace webrtc
140