vp9_arf_freq_test.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "third_party/googletest/src/include/gtest/gtest.h"
  11. #include "test/codec_factory.h"
  12. #include "test/encode_test_driver.h"
  13. #include "test/util.h"
  14. #include "test/y4m_video_source.h"
  15. #include "test/yuv_video_source.h"
  16. #include "vp9/encoder/vp9_ratectrl.h"
  17. namespace {
  18. const unsigned int kFrames = 100;
  19. const int kBitrate = 500;
  20. #define ARF_NOT_SEEN 1000001
  21. #define ARF_SEEN_ONCE 1000000
  22. typedef struct {
  23. const char *filename;
  24. unsigned int width;
  25. unsigned int height;
  26. unsigned int framerate_num;
  27. unsigned int framerate_den;
  28. unsigned int input_bit_depth;
  29. vpx_img_fmt fmt;
  30. vpx_bit_depth_t bit_depth;
  31. unsigned int profile;
  32. } TestVideoParam;
  33. typedef struct {
  34. libvpx_test::TestMode mode;
  35. int cpu_used;
  36. } TestEncodeParam;
  37. const TestVideoParam kTestVectors[] = {
  38. // artificially increase framerate to trigger default check
  39. { "hantro_collage_w352h288.yuv", 352, 288, 5000, 1, 8, VPX_IMG_FMT_I420,
  40. VPX_BITS_8, 0 },
  41. { "hantro_collage_w352h288.yuv", 352, 288, 30, 1, 8, VPX_IMG_FMT_I420,
  42. VPX_BITS_8, 0 },
  43. { "rush_hour_444.y4m", 352, 288, 30, 1, 8, VPX_IMG_FMT_I444, VPX_BITS_8, 1 },
  44. #if CONFIG_VP9_HIGHBITDEPTH
  45. // Add list of profile 2/3 test videos here ...
  46. #endif // CONFIG_VP9_HIGHBITDEPTH
  47. };
  48. const TestEncodeParam kEncodeVectors[] = {
  49. { ::libvpx_test::kOnePassGood, 2 }, { ::libvpx_test::kOnePassGood, 5 },
  50. { ::libvpx_test::kTwoPassGood, 1 }, { ::libvpx_test::kTwoPassGood, 2 },
  51. { ::libvpx_test::kTwoPassGood, 5 }, { ::libvpx_test::kRealTime, 5 },
  52. };
  53. const int kMinArfVectors[] = {
  54. // NOTE: 0 refers to the default built-in logic in:
  55. // vp9_rc_get_default_min_gf_interval(...)
  56. 0, 4, 8, 12, 15
  57. };
  58. int is_extension_y4m(const char *filename) {
  59. const char *dot = strrchr(filename, '.');
  60. if (!dot || dot == filename) {
  61. return 0;
  62. } else {
  63. return !strcmp(dot, ".y4m");
  64. }
  65. }
  66. class ArfFreqTest
  67. : public ::libvpx_test::EncoderTest,
  68. public ::libvpx_test::CodecTestWith3Params<TestVideoParam,
  69. TestEncodeParam, int> {
  70. protected:
  71. ArfFreqTest()
  72. : EncoderTest(GET_PARAM(0)), test_video_param_(GET_PARAM(1)),
  73. test_encode_param_(GET_PARAM(2)), min_arf_requested_(GET_PARAM(3)) {}
  74. virtual ~ArfFreqTest() {}
  75. virtual void SetUp() {
  76. InitializeConfig();
  77. SetMode(test_encode_param_.mode);
  78. if (test_encode_param_.mode != ::libvpx_test::kRealTime) {
  79. cfg_.g_lag_in_frames = 25;
  80. cfg_.rc_end_usage = VPX_VBR;
  81. } else {
  82. cfg_.g_lag_in_frames = 0;
  83. cfg_.rc_end_usage = VPX_CBR;
  84. cfg_.rc_buf_sz = 1000;
  85. cfg_.rc_buf_initial_sz = 500;
  86. cfg_.rc_buf_optimal_sz = 600;
  87. }
  88. dec_cfg_.threads = 4;
  89. }
  90. virtual void BeginPassHook(unsigned int) {
  91. min_run_ = ARF_NOT_SEEN;
  92. run_of_visible_frames_ = 0;
  93. }
  94. int GetNumFramesInPkt(const vpx_codec_cx_pkt_t *pkt) {
  95. const uint8_t *buffer = reinterpret_cast<uint8_t *>(pkt->data.frame.buf);
  96. const uint8_t marker = buffer[pkt->data.frame.sz - 1];
  97. const int mag = ((marker >> 3) & 3) + 1;
  98. int frames = (marker & 0x7) + 1;
  99. const unsigned int index_sz = 2 + mag * frames;
  100. // Check for superframe or not.
  101. // Assume superframe has only one visible frame, the rest being
  102. // invisible. If superframe index is not found, then there is only
  103. // one frame.
  104. if (!((marker & 0xe0) == 0xc0 && pkt->data.frame.sz >= index_sz &&
  105. buffer[pkt->data.frame.sz - index_sz] == marker)) {
  106. frames = 1;
  107. }
  108. return frames;
  109. }
  110. virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
  111. if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) return;
  112. const int frames = GetNumFramesInPkt(pkt);
  113. if (frames == 1) {
  114. run_of_visible_frames_++;
  115. } else if (frames == 2) {
  116. if (min_run_ == ARF_NOT_SEEN) {
  117. min_run_ = ARF_SEEN_ONCE;
  118. } else if (min_run_ == ARF_SEEN_ONCE ||
  119. run_of_visible_frames_ < min_run_) {
  120. min_run_ = run_of_visible_frames_;
  121. }
  122. run_of_visible_frames_ = 1;
  123. } else {
  124. min_run_ = 0;
  125. run_of_visible_frames_ = 1;
  126. }
  127. }
  128. virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
  129. ::libvpx_test::Encoder *encoder) {
  130. if (video->frame() == 0) {
  131. encoder->Control(VP9E_SET_FRAME_PARALLEL_DECODING, 1);
  132. encoder->Control(VP9E_SET_TILE_COLUMNS, 4);
  133. encoder->Control(VP8E_SET_CPUUSED, test_encode_param_.cpu_used);
  134. encoder->Control(VP9E_SET_MIN_GF_INTERVAL, min_arf_requested_);
  135. if (test_encode_param_.mode != ::libvpx_test::kRealTime) {
  136. encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
  137. encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
  138. encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
  139. encoder->Control(VP8E_SET_ARNR_TYPE, 3);
  140. }
  141. }
  142. }
  143. int GetMinVisibleRun() const { return min_run_; }
  144. int GetMinArfDistanceRequested() const {
  145. if (min_arf_requested_) {
  146. return min_arf_requested_;
  147. } else {
  148. return vp9_rc_get_default_min_gf_interval(
  149. test_video_param_.width, test_video_param_.height,
  150. (double)test_video_param_.framerate_num /
  151. test_video_param_.framerate_den);
  152. }
  153. }
  154. TestVideoParam test_video_param_;
  155. TestEncodeParam test_encode_param_;
  156. private:
  157. int min_arf_requested_;
  158. int min_run_;
  159. int run_of_visible_frames_;
  160. };
  161. TEST_P(ArfFreqTest, MinArfFreqTest) {
  162. cfg_.rc_target_bitrate = kBitrate;
  163. cfg_.g_error_resilient = 0;
  164. cfg_.g_profile = test_video_param_.profile;
  165. cfg_.g_input_bit_depth = test_video_param_.input_bit_depth;
  166. cfg_.g_bit_depth = test_video_param_.bit_depth;
  167. init_flags_ = VPX_CODEC_USE_PSNR;
  168. if (cfg_.g_bit_depth > 8) init_flags_ |= VPX_CODEC_USE_HIGHBITDEPTH;
  169. testing::internal::scoped_ptr<libvpx_test::VideoSource> video;
  170. if (is_extension_y4m(test_video_param_.filename)) {
  171. video.reset(new libvpx_test::Y4mVideoSource(test_video_param_.filename, 0,
  172. kFrames));
  173. } else {
  174. video.reset(new libvpx_test::YUVVideoSource(
  175. test_video_param_.filename, test_video_param_.fmt,
  176. test_video_param_.width, test_video_param_.height,
  177. test_video_param_.framerate_num, test_video_param_.framerate_den, 0,
  178. kFrames));
  179. }
  180. ASSERT_NO_FATAL_FAILURE(RunLoop(video.get()));
  181. const int min_run = GetMinVisibleRun();
  182. const int min_arf_dist_requested = GetMinArfDistanceRequested();
  183. if (min_run != ARF_NOT_SEEN && min_run != ARF_SEEN_ONCE) {
  184. const int min_arf_dist = min_run + 1;
  185. EXPECT_GE(min_arf_dist, min_arf_dist_requested);
  186. }
  187. }
  188. VP9_INSTANTIATE_TEST_CASE(ArfFreqTest, ::testing::ValuesIn(kTestVectors),
  189. ::testing::ValuesIn(kEncodeVectors),
  190. ::testing::ValuesIn(kMinArfVectors));
  191. } // namespace