cq_test.cc 4.1 KB

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