Home | History | Annotate | Download | only in metrics
      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 char* kNonExistingFileName = "video_metrics_unittest_non_existing";
     19 static const int kWidth = 352;
     20 static const int kHeight = 288;
     21 
     22 static const int kMissingReferenceFileReturnCode = -1;
     23 static const int kMissingTestFileReturnCode = -2;
     24 static const int kEmptyFileReturnCode = -3;
     25 static const double kPsnrPerfectResult =  48.0;
     26 static const double kSsimPerfectResult = 1.0;
     27 
     28 class 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
     51 TEST_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 
     57 TEST_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 
     63 TEST_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.
     72 TEST_F(VideoMetricsTest, MissingReferenceFilePSNR) {
     73   EXPECT_EQ(kMissingReferenceFileReturnCode,
     74             I420PSNRFromFiles(kNonExistingFileName, video_file_.c_str(),
     75                               kWidth, kHeight, &ssim_result_));
     76 }
     77 
     78 TEST_F(VideoMetricsTest, MissingReferenceFileSSIM) {
     79   EXPECT_EQ(kMissingReferenceFileReturnCode,
     80             I420SSIMFromFiles(kNonExistingFileName, video_file_.c_str(),
     81                               kWidth, kHeight, &ssim_result_));
     82 }
     83 
     84 TEST_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.
     92 TEST_F(VideoMetricsTest, MissingTestFilePSNR) {
     93   EXPECT_EQ(kMissingTestFileReturnCode,
     94             I420PSNRFromFiles(video_file_.c_str(), kNonExistingFileName,
     95                               kWidth, kHeight, &ssim_result_));
     96 }
     97 
     98 TEST_F(VideoMetricsTest, MissingTestFileSSIM) {
     99   EXPECT_EQ(kMissingTestFileReturnCode,
    100             I420SSIMFromFiles(video_file_.c_str(), kNonExistingFileName,
    101                               kWidth, kHeight, &ssim_result_));
    102 }
    103 
    104 TEST_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.
    112 TEST_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 
    121 TEST_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 
    130 TEST_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