vp8_datarate_test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "./vpx_config.h"
  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/i420_video_source.h"
  15. #include "test/util.h"
  16. #include "test/y4m_video_source.h"
  17. #include "vpx/vpx_codec.h"
  18. namespace {
  19. class DatarateTestLarge
  20. : public ::libvpx_test::EncoderTest,
  21. public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
  22. public:
  23. DatarateTestLarge() : EncoderTest(GET_PARAM(0)) {}
  24. virtual ~DatarateTestLarge() {}
  25. protected:
  26. virtual void SetUp() {
  27. InitializeConfig();
  28. SetMode(GET_PARAM(1));
  29. set_cpu_used_ = GET_PARAM(2);
  30. ResetModel();
  31. }
  32. virtual void ResetModel() {
  33. last_pts_ = 0;
  34. bits_in_buffer_model_ = cfg_.rc_target_bitrate * cfg_.rc_buf_initial_sz;
  35. frame_number_ = 0;
  36. first_drop_ = 0;
  37. bits_total_ = 0;
  38. duration_ = 0.0;
  39. denoiser_offon_test_ = 0;
  40. denoiser_offon_period_ = -1;
  41. gf_boost_ = 0;
  42. use_roi_ = false;
  43. }
  44. virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
  45. ::libvpx_test::Encoder *encoder) {
  46. if (video->frame() == 0) {
  47. encoder->Control(VP8E_SET_NOISE_SENSITIVITY, denoiser_on_);
  48. encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
  49. encoder->Control(VP8E_SET_GF_CBR_BOOST_PCT, gf_boost_);
  50. }
  51. if (use_roi_) {
  52. encoder->Control(VP8E_SET_ROI_MAP, &roi_);
  53. }
  54. if (denoiser_offon_test_) {
  55. ASSERT_GT(denoiser_offon_period_, 0)
  56. << "denoiser_offon_period_ is not positive.";
  57. if ((video->frame() + 1) % denoiser_offon_period_ == 0) {
  58. // Flip denoiser_on_ periodically
  59. denoiser_on_ ^= 1;
  60. }
  61. encoder->Control(VP8E_SET_NOISE_SENSITIVITY, denoiser_on_);
  62. }
  63. const vpx_rational_t tb = video->timebase();
  64. timebase_ = static_cast<double>(tb.num) / tb.den;
  65. duration_ = 0;
  66. }
  67. virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
  68. // Time since last timestamp = duration.
  69. vpx_codec_pts_t duration = pkt->data.frame.pts - last_pts_;
  70. // TODO(jimbankoski): Remove these lines when the issue:
  71. // http://code.google.com/p/webm/issues/detail?id=496 is fixed.
  72. // For now the codec assumes buffer starts at starting buffer rate
  73. // plus one frame's time.
  74. if (last_pts_ == 0) duration = 1;
  75. // Add to the buffer the bits we'd expect from a constant bitrate server.
  76. bits_in_buffer_model_ += static_cast<int64_t>(
  77. duration * timebase_ * cfg_.rc_target_bitrate * 1000);
  78. /* Test the buffer model here before subtracting the frame. Do so because
  79. * the way the leaky bucket model works in libvpx is to allow the buffer to
  80. * empty - and then stop showing frames until we've got enough bits to
  81. * show one. As noted in comment below (issue 495), this does not currently
  82. * apply to key frames. For now exclude key frames in condition below. */
  83. const bool key_frame =
  84. (pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
  85. if (!key_frame) {
  86. ASSERT_GE(bits_in_buffer_model_, 0)
  87. << "Buffer Underrun at frame " << pkt->data.frame.pts;
  88. }
  89. const int64_t frame_size_in_bits = pkt->data.frame.sz * 8;
  90. // Subtract from the buffer the bits associated with a played back frame.
  91. bits_in_buffer_model_ -= frame_size_in_bits;
  92. // Update the running total of bits for end of test datarate checks.
  93. bits_total_ += frame_size_in_bits;
  94. // If first drop not set and we have a drop set it to this time.
  95. if (!first_drop_ && duration > 1) first_drop_ = last_pts_ + 1;
  96. // Update the most recent pts.
  97. last_pts_ = pkt->data.frame.pts;
  98. // We update this so that we can calculate the datarate minus the last
  99. // frame encoded in the file.
  100. bits_in_last_frame_ = frame_size_in_bits;
  101. ++frame_number_;
  102. }
  103. virtual void EndPassHook(void) {
  104. if (bits_total_) {
  105. const double file_size_in_kb = bits_total_ / 1000.; // bits per kilobit
  106. duration_ = (last_pts_ + 1) * timebase_;
  107. // Effective file datarate includes the time spent prebuffering.
  108. effective_datarate_ = (bits_total_ - bits_in_last_frame_) / 1000.0 /
  109. (cfg_.rc_buf_initial_sz / 1000.0 + duration_);
  110. file_datarate_ = file_size_in_kb / duration_;
  111. }
  112. }
  113. virtual void DenoiserLevelsTest() {
  114. cfg_.rc_buf_initial_sz = 500;
  115. cfg_.rc_dropframe_thresh = 1;
  116. cfg_.rc_max_quantizer = 56;
  117. cfg_.rc_end_usage = VPX_CBR;
  118. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352,
  119. 288, 30, 1, 0, 140);
  120. for (int j = 1; j < 5; ++j) {
  121. // Run over the denoiser levels.
  122. // For the temporal denoiser (#if CONFIG_TEMPORAL_DENOISING) the level j
  123. // refers to the 4 denoiser modes: denoiserYonly, denoiserOnYUV,
  124. // denoiserOnAggressive, and denoiserOnAdaptive.
  125. denoiser_on_ = j;
  126. cfg_.rc_target_bitrate = 300;
  127. ResetModel();
  128. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  129. ASSERT_GE(cfg_.rc_target_bitrate, effective_datarate_ * 0.95)
  130. << " The datarate for the file exceeds the target!";
  131. ASSERT_LE(cfg_.rc_target_bitrate, file_datarate_ * 1.4)
  132. << " The datarate for the file missed the target!";
  133. }
  134. }
  135. virtual void DenoiserOffOnTest() {
  136. cfg_.rc_buf_initial_sz = 500;
  137. cfg_.rc_dropframe_thresh = 1;
  138. cfg_.rc_max_quantizer = 56;
  139. cfg_.rc_end_usage = VPX_CBR;
  140. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352,
  141. 288, 30, 1, 0, 299);
  142. cfg_.rc_target_bitrate = 300;
  143. ResetModel();
  144. // The denoiser is off by default.
  145. denoiser_on_ = 0;
  146. // Set the offon test flag.
  147. denoiser_offon_test_ = 1;
  148. denoiser_offon_period_ = 100;
  149. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  150. ASSERT_GE(cfg_.rc_target_bitrate, effective_datarate_ * 0.95)
  151. << " The datarate for the file exceeds the target!";
  152. ASSERT_LE(cfg_.rc_target_bitrate, file_datarate_ * 1.4)
  153. << " The datarate for the file missed the target!";
  154. }
  155. virtual void BasicBufferModelTest() {
  156. denoiser_on_ = 0;
  157. cfg_.rc_buf_initial_sz = 500;
  158. cfg_.rc_dropframe_thresh = 1;
  159. cfg_.rc_max_quantizer = 56;
  160. cfg_.rc_end_usage = VPX_CBR;
  161. // 2 pass cbr datarate control has a bug hidden by the small # of
  162. // frames selected in this encode. The problem is that even if the buffer is
  163. // negative we produce a keyframe on a cutscene. Ignoring datarate
  164. // constraints
  165. // TODO(jimbankoski): ( Fix when issue
  166. // http://code.google.com/p/webm/issues/detail?id=495 is addressed. )
  167. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352,
  168. 288, 30, 1, 0, 140);
  169. // There is an issue for low bitrates in real-time mode, where the
  170. // effective_datarate slightly overshoots the target bitrate.
  171. // This is same the issue as noted about (#495).
  172. // TODO(jimbankoski/marpan): Update test to run for lower bitrates (< 100),
  173. // when the issue is resolved.
  174. for (int i = 100; i < 800; i += 200) {
  175. cfg_.rc_target_bitrate = i;
  176. ResetModel();
  177. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  178. ASSERT_GE(cfg_.rc_target_bitrate, effective_datarate_ * 0.95)
  179. << " The datarate for the file exceeds the target!";
  180. ASSERT_LE(cfg_.rc_target_bitrate, file_datarate_ * 1.4)
  181. << " The datarate for the file missed the target!";
  182. }
  183. }
  184. virtual void ChangingDropFrameThreshTest() {
  185. denoiser_on_ = 0;
  186. cfg_.rc_buf_initial_sz = 500;
  187. cfg_.rc_max_quantizer = 36;
  188. cfg_.rc_end_usage = VPX_CBR;
  189. cfg_.rc_target_bitrate = 200;
  190. cfg_.kf_mode = VPX_KF_DISABLED;
  191. const int frame_count = 40;
  192. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352,
  193. 288, 30, 1, 0, frame_count);
  194. // Here we check that the first dropped frame gets earlier and earlier
  195. // as the drop frame threshold is increased.
  196. const int kDropFrameThreshTestStep = 30;
  197. vpx_codec_pts_t last_drop = frame_count;
  198. for (int i = 1; i < 91; i += kDropFrameThreshTestStep) {
  199. cfg_.rc_dropframe_thresh = i;
  200. ResetModel();
  201. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  202. ASSERT_LE(first_drop_, last_drop)
  203. << " The first dropped frame for drop_thresh " << i
  204. << " > first dropped frame for drop_thresh "
  205. << i - kDropFrameThreshTestStep;
  206. last_drop = first_drop_;
  207. }
  208. }
  209. virtual void DropFramesMultiThreadsTest() {
  210. denoiser_on_ = 0;
  211. cfg_.rc_buf_initial_sz = 500;
  212. cfg_.rc_dropframe_thresh = 30;
  213. cfg_.rc_max_quantizer = 56;
  214. cfg_.rc_end_usage = VPX_CBR;
  215. cfg_.g_threads = 2;
  216. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352,
  217. 288, 30, 1, 0, 140);
  218. cfg_.rc_target_bitrate = 200;
  219. ResetModel();
  220. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  221. ASSERT_GE(cfg_.rc_target_bitrate, effective_datarate_ * 0.95)
  222. << " The datarate for the file exceeds the target!";
  223. ASSERT_LE(cfg_.rc_target_bitrate, file_datarate_ * 1.4)
  224. << " The datarate for the file missed the target!";
  225. }
  226. vpx_codec_pts_t last_pts_;
  227. int64_t bits_in_buffer_model_;
  228. double timebase_;
  229. int frame_number_;
  230. vpx_codec_pts_t first_drop_;
  231. int64_t bits_total_;
  232. double duration_;
  233. double file_datarate_;
  234. double effective_datarate_;
  235. int64_t bits_in_last_frame_;
  236. int denoiser_on_;
  237. int denoiser_offon_test_;
  238. int denoiser_offon_period_;
  239. int set_cpu_used_;
  240. int gf_boost_;
  241. bool use_roi_;
  242. vpx_roi_map_t roi_;
  243. };
  244. #if CONFIG_TEMPORAL_DENOISING
  245. // Check basic datarate targeting, for a single bitrate, but loop over the
  246. // various denoiser settings.
  247. TEST_P(DatarateTestLarge, DenoiserLevels) { DenoiserLevelsTest(); }
  248. // Check basic datarate targeting, for a single bitrate, when denoiser is off
  249. // and on.
  250. TEST_P(DatarateTestLarge, DenoiserOffOn) { DenoiserOffOnTest(); }
  251. #endif // CONFIG_TEMPORAL_DENOISING
  252. TEST_P(DatarateTestLarge, BasicBufferModel) { BasicBufferModelTest(); }
  253. TEST_P(DatarateTestLarge, ChangingDropFrameThresh) {
  254. ChangingDropFrameThreshTest();
  255. }
  256. TEST_P(DatarateTestLarge, DropFramesMultiThreads) {
  257. DropFramesMultiThreadsTest();
  258. }
  259. class DatarateTestRealTime : public DatarateTestLarge {
  260. public:
  261. virtual ~DatarateTestRealTime() {}
  262. };
  263. #if CONFIG_TEMPORAL_DENOISING
  264. // Check basic datarate targeting, for a single bitrate, but loop over the
  265. // various denoiser settings.
  266. TEST_P(DatarateTestRealTime, DenoiserLevels) { DenoiserLevelsTest(); }
  267. // Check basic datarate targeting, for a single bitrate, when denoiser is off
  268. // and on.
  269. TEST_P(DatarateTestRealTime, DenoiserOffOn) {}
  270. #endif // CONFIG_TEMPORAL_DENOISING
  271. TEST_P(DatarateTestRealTime, BasicBufferModel) { BasicBufferModelTest(); }
  272. TEST_P(DatarateTestRealTime, ChangingDropFrameThresh) {
  273. ChangingDropFrameThreshTest();
  274. }
  275. TEST_P(DatarateTestRealTime, DropFramesMultiThreads) {
  276. DropFramesMultiThreadsTest();
  277. }
  278. TEST_P(DatarateTestRealTime, RegionOfInterest) {
  279. denoiser_on_ = 0;
  280. cfg_.rc_buf_initial_sz = 500;
  281. cfg_.rc_dropframe_thresh = 0;
  282. cfg_.rc_max_quantizer = 56;
  283. cfg_.rc_end_usage = VPX_CBR;
  284. // Encode using multiple threads.
  285. cfg_.g_threads = 2;
  286. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
  287. 30, 1, 0, 300);
  288. cfg_.rc_target_bitrate = 450;
  289. cfg_.g_w = 352;
  290. cfg_.g_h = 288;
  291. ResetModel();
  292. // Set ROI parameters
  293. use_roi_ = true;
  294. memset(&roi_, 0, sizeof(roi_));
  295. roi_.rows = (cfg_.g_h + 15) / 16;
  296. roi_.cols = (cfg_.g_w + 15) / 16;
  297. roi_.delta_q[0] = 0;
  298. roi_.delta_q[1] = -20;
  299. roi_.delta_q[2] = 0;
  300. roi_.delta_q[3] = 0;
  301. roi_.delta_lf[0] = 0;
  302. roi_.delta_lf[1] = -20;
  303. roi_.delta_lf[2] = 0;
  304. roi_.delta_lf[3] = 0;
  305. roi_.static_threshold[0] = 0;
  306. roi_.static_threshold[1] = 1000;
  307. roi_.static_threshold[2] = 0;
  308. roi_.static_threshold[3] = 0;
  309. // Use 2 states: 1 is center square, 0 is the rest.
  310. roi_.roi_map =
  311. (uint8_t *)calloc(roi_.rows * roi_.cols, sizeof(*roi_.roi_map));
  312. for (unsigned int i = 0; i < roi_.rows; ++i) {
  313. for (unsigned int j = 0; j < roi_.cols; ++j) {
  314. if (i > (roi_.rows >> 2) && i < ((roi_.rows * 3) >> 2) &&
  315. j > (roi_.cols >> 2) && j < ((roi_.cols * 3) >> 2)) {
  316. roi_.roi_map[i * roi_.cols + j] = 1;
  317. }
  318. }
  319. }
  320. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  321. ASSERT_GE(cfg_.rc_target_bitrate, effective_datarate_ * 0.95)
  322. << " The datarate for the file exceeds the target!";
  323. ASSERT_LE(cfg_.rc_target_bitrate, file_datarate_ * 1.4)
  324. << " The datarate for the file missed the target!";
  325. free(roi_.roi_map);
  326. }
  327. TEST_P(DatarateTestRealTime, GFBoost) {
  328. denoiser_on_ = 0;
  329. cfg_.rc_buf_initial_sz = 500;
  330. cfg_.rc_dropframe_thresh = 0;
  331. cfg_.rc_max_quantizer = 56;
  332. cfg_.rc_end_usage = VPX_CBR;
  333. cfg_.g_error_resilient = 0;
  334. ::libvpx_test::I420VideoSource video("hantro_collage_w352h288.yuv", 352, 288,
  335. 30, 1, 0, 300);
  336. cfg_.rc_target_bitrate = 300;
  337. ResetModel();
  338. // Apply a gf boost.
  339. gf_boost_ = 50;
  340. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  341. ASSERT_GE(cfg_.rc_target_bitrate, effective_datarate_ * 0.95)
  342. << " The datarate for the file exceeds the target!";
  343. ASSERT_LE(cfg_.rc_target_bitrate, file_datarate_ * 1.4)
  344. << " The datarate for the file missed the target!";
  345. }
  346. VP8_INSTANTIATE_TEST_CASE(DatarateTestLarge, ALL_TEST_MODES,
  347. ::testing::Values(0));
  348. VP8_INSTANTIATE_TEST_CASE(DatarateTestRealTime,
  349. ::testing::Values(::libvpx_test::kRealTime),
  350. ::testing::Values(-6, -12));
  351. } // namespace