• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 The WebM 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 #include <cmath>
11 #include <map>
12 #include "third_party/googletest/src/include/gtest/gtest.h"
13 #include "test/codec_factory.h"
14 #include "test/encode_test_driver.h"
15 #include "test/i420_video_source.h"
16 #include "test/util.h"
17 
18 namespace {
19 
20 // CQ level range: [kCQLevelMin, kCQLevelMax).
21 const int kCQLevelMin = 4;
22 const int kCQLevelMax = 63;
23 const int kCQLevelStep = 8;
24 const unsigned int kCQTargetBitrate = 2000;
25 
26 class CQTest : public ::libvpx_test::EncoderTest,
27                public ::libvpx_test::CodecTestWithParam<int> {
28  public:
29   // maps the cqlevel to the bitrate produced.
30   typedef std::map<int, uint32_t> BitrateMap;
31 
SetUpTestSuite()32   static void SetUpTestSuite() { bitrates_.clear(); }
33 
TearDownTestSuite()34   static void TearDownTestSuite() {
35     ASSERT_TRUE(!HasFailure())
36         << "skipping bitrate validation due to earlier failure.";
37     uint32_t prev_actual_bitrate = kCQTargetBitrate;
38     for (BitrateMap::const_iterator iter = bitrates_.begin();
39          iter != bitrates_.end(); ++iter) {
40       const uint32_t cq_actual_bitrate = iter->second;
41       EXPECT_LE(cq_actual_bitrate, prev_actual_bitrate)
42           << "cq_level: " << iter->first
43           << ", bitrate should decrease with increase in CQ level.";
44       prev_actual_bitrate = cq_actual_bitrate;
45     }
46   }
47 
48  protected:
CQTest()49   CQTest() : EncoderTest(GET_PARAM(0)), cq_level_(GET_PARAM(1)) {
50     init_flags_ = VPX_CODEC_USE_PSNR;
51   }
52 
53   ~CQTest() override = default;
54 
SetUp()55   void SetUp() override {
56     InitializeConfig();
57     SetMode(libvpx_test::kTwoPassGood);
58   }
59 
BeginPassHook(unsigned int)60   void BeginPassHook(unsigned int /*pass*/) override {
61     file_size_ = 0;
62     psnr_ = 0.0;
63     n_frames_ = 0;
64   }
65 
PreEncodeFrameHook(libvpx_test::VideoSource * video,libvpx_test::Encoder * encoder)66   void PreEncodeFrameHook(libvpx_test::VideoSource *video,
67                           libvpx_test::Encoder *encoder) override {
68     if (video->frame() == 0) {
69       if (cfg_.rc_end_usage == VPX_CQ) {
70         encoder->Control(VP8E_SET_CQ_LEVEL, cq_level_);
71       }
72       encoder->Control(VP8E_SET_CPUUSED, 3);
73     }
74   }
75 
PSNRPktHook(const vpx_codec_cx_pkt_t * pkt)76   void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) override {
77     psnr_ += pow(10.0, pkt->data.psnr.psnr[0] / 10.0);
78     n_frames_++;
79   }
80 
FramePktHook(const vpx_codec_cx_pkt_t * pkt)81   void FramePktHook(const vpx_codec_cx_pkt_t *pkt) override {
82     file_size_ += pkt->data.frame.sz;
83   }
84 
GetLinearPSNROverBitrate() const85   double GetLinearPSNROverBitrate() const {
86     double avg_psnr = log10(psnr_ / n_frames_) * 10.0;
87     return pow(10.0, avg_psnr / 10.0) / file_size_;
88   }
89 
cq_level() const90   int cq_level() const { return cq_level_; }
file_size() const91   size_t file_size() const { return file_size_; }
n_frames() const92   int n_frames() const { return n_frames_; }
93 
94   static BitrateMap bitrates_;
95 
96  private:
97   int cq_level_;
98   size_t file_size_;
99   double psnr_;
100   int n_frames_;
101 };
102 
103 CQTest::BitrateMap CQTest::bitrates_;
104 
TEST_P(CQTest,LinearPSNRIsHigherForCQLevel)105 TEST_P(CQTest, LinearPSNRIsHigherForCQLevel) {
106   const vpx_rational timebase = { 33333333, 1000000000 };
107   cfg_.g_timebase = timebase;
108   cfg_.rc_target_bitrate = kCQTargetBitrate;
109   cfg_.g_lag_in_frames = 25;
110 
111   cfg_.rc_end_usage = VPX_CQ;
112   libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
113                                      timebase.den, timebase.num, 0, 30);
114   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
115   const double cq_psnr_lin = GetLinearPSNROverBitrate();
116   const unsigned int cq_actual_bitrate =
117       static_cast<unsigned int>(file_size()) * 8 * 30 / (n_frames() * 1000);
118   EXPECT_LE(cq_actual_bitrate, kCQTargetBitrate);
119   bitrates_[cq_level()] = cq_actual_bitrate;
120 
121   // try targeting the approximate same bitrate with VBR mode
122   cfg_.rc_end_usage = VPX_VBR;
123   cfg_.rc_target_bitrate = cq_actual_bitrate;
124   ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
125   const double vbr_psnr_lin = GetLinearPSNROverBitrate();
126   EXPECT_GE(cq_psnr_lin, vbr_psnr_lin);
127 }
128 
129 VP8_INSTANTIATE_TEST_SUITE(CQTest, ::testing::Range(kCQLevelMin, kCQLevelMax,
130                                                     kCQLevelStep));
131 }  // namespace
132