vp9_arf_freq_test.cc 7.0 KB

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