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