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