active_map_refresh_test.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2015 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 <algorithm>
  11. #include "third_party/googletest/src/include/gtest/gtest.h"
  12. #include "test/codec_factory.h"
  13. #include "test/encode_test_driver.h"
  14. #include "test/util.h"
  15. #include "test/y4m_video_source.h"
  16. namespace {
  17. // Check if any pixel in a 16x16 macroblock varies between frames.
  18. int CheckMb(const vpx_image_t &current, const vpx_image_t &previous, int mb_r,
  19. int mb_c) {
  20. for (int plane = 0; plane < 3; plane++) {
  21. int r = 16 * mb_r;
  22. int c0 = 16 * mb_c;
  23. int r_top = std::min(r + 16, static_cast<int>(current.d_h));
  24. int c_top = std::min(c0 + 16, static_cast<int>(current.d_w));
  25. r = std::max(r, 0);
  26. c0 = std::max(c0, 0);
  27. if (plane > 0 && current.x_chroma_shift) {
  28. c_top = (c_top + 1) >> 1;
  29. c0 >>= 1;
  30. }
  31. if (plane > 0 && current.y_chroma_shift) {
  32. r_top = (r_top + 1) >> 1;
  33. r >>= 1;
  34. }
  35. for (; r < r_top; ++r) {
  36. for (int c = c0; c < c_top; ++c) {
  37. if (current.planes[plane][current.stride[plane] * r + c] !=
  38. previous.planes[plane][previous.stride[plane] * r + c]) {
  39. return 1;
  40. }
  41. }
  42. }
  43. }
  44. return 0;
  45. }
  46. void GenerateMap(int mb_rows, int mb_cols, const vpx_image_t &current,
  47. const vpx_image_t &previous, uint8_t *map) {
  48. for (int mb_r = 0; mb_r < mb_rows; ++mb_r) {
  49. for (int mb_c = 0; mb_c < mb_cols; ++mb_c) {
  50. map[mb_r * mb_cols + mb_c] = CheckMb(current, previous, mb_r, mb_c);
  51. }
  52. }
  53. }
  54. const int kAqModeCyclicRefresh = 3;
  55. class ActiveMapRefreshTest
  56. : public ::libvpx_test::EncoderTest,
  57. public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
  58. protected:
  59. ActiveMapRefreshTest() : EncoderTest(GET_PARAM(0)) {}
  60. virtual ~ActiveMapRefreshTest() {}
  61. virtual void SetUp() {
  62. InitializeConfig();
  63. SetMode(GET_PARAM(1));
  64. cpu_used_ = GET_PARAM(2);
  65. }
  66. virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
  67. ::libvpx_test::Encoder *encoder) {
  68. ::libvpx_test::Y4mVideoSource *y4m_video =
  69. static_cast<libvpx_test::Y4mVideoSource *>(video);
  70. if (video->frame() == 0) {
  71. encoder->Control(VP8E_SET_CPUUSED, cpu_used_);
  72. encoder->Control(VP9E_SET_AQ_MODE, kAqModeCyclicRefresh);
  73. } else if (video->frame() >= 2 && video->img()) {
  74. vpx_image_t *current = video->img();
  75. vpx_image_t *previous = y4m_holder_->img();
  76. ASSERT_TRUE(previous != NULL);
  77. vpx_active_map_t map = vpx_active_map_t();
  78. const int width = static_cast<int>(current->d_w);
  79. const int height = static_cast<int>(current->d_h);
  80. const int mb_width = (width + 15) / 16;
  81. const int mb_height = (height + 15) / 16;
  82. uint8_t *active_map = new uint8_t[mb_width * mb_height];
  83. GenerateMap(mb_height, mb_width, *current, *previous, active_map);
  84. map.cols = mb_width;
  85. map.rows = mb_height;
  86. map.active_map = active_map;
  87. encoder->Control(VP8E_SET_ACTIVEMAP, &map);
  88. delete[] active_map;
  89. }
  90. if (video->img()) {
  91. y4m_video->SwapBuffers(y4m_holder_);
  92. }
  93. }
  94. int cpu_used_;
  95. ::libvpx_test::Y4mVideoSource *y4m_holder_;
  96. };
  97. TEST_P(ActiveMapRefreshTest, Test) {
  98. cfg_.g_lag_in_frames = 0;
  99. cfg_.g_profile = 1;
  100. cfg_.rc_target_bitrate = 600;
  101. cfg_.rc_resize_allowed = 0;
  102. cfg_.rc_min_quantizer = 8;
  103. cfg_.rc_max_quantizer = 30;
  104. cfg_.g_pass = VPX_RC_ONE_PASS;
  105. cfg_.rc_end_usage = VPX_CBR;
  106. cfg_.kf_max_dist = 90000;
  107. ::libvpx_test::Y4mVideoSource video("desktop_credits.y4m", 0, 30);
  108. ::libvpx_test::Y4mVideoSource video_holder("desktop_credits.y4m", 0, 30);
  109. video_holder.Begin();
  110. y4m_holder_ = &video_holder;
  111. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  112. }
  113. VP9_INSTANTIATE_TEST_CASE(ActiveMapRefreshTest,
  114. ::testing::Values(::libvpx_test::kRealTime),
  115. ::testing::Range(5, 6));
  116. } // namespace