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