vp9_ethread_test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (c) 2014 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 <string>
  11. #include <vector>
  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/md5_helper.h"
  16. #include "test/util.h"
  17. #include "test/y4m_video_source.h"
  18. #include "vp9/encoder/vp9_firstpass.h"
  19. namespace {
  20. // FIRSTPASS_STATS struct:
  21. // {
  22. // 25 double members;
  23. // 1 int64_t member;
  24. // }
  25. // Whenever FIRSTPASS_STATS struct is modified, the following constants need to
  26. // be revisited.
  27. const int kDbl = 25;
  28. const int kInt = 1;
  29. const size_t kFirstPassStatsSz = kDbl * sizeof(double) + kInt * sizeof(int64_t);
  30. class VPxFirstPassEncoderThreadTest
  31. : public ::libvpx_test::EncoderTest,
  32. public ::libvpx_test::CodecTestWith2Params<libvpx_test::TestMode, int> {
  33. protected:
  34. VPxFirstPassEncoderThreadTest()
  35. : EncoderTest(GET_PARAM(0)), encoder_initialized_(false), tiles_(0),
  36. encoding_mode_(GET_PARAM(1)), set_cpu_used_(GET_PARAM(2)) {
  37. init_flags_ = VPX_CODEC_USE_PSNR;
  38. row_mt_mode_ = 1;
  39. first_pass_only_ = true;
  40. firstpass_stats_.buf = NULL;
  41. firstpass_stats_.sz = 0;
  42. }
  43. virtual ~VPxFirstPassEncoderThreadTest() { free(firstpass_stats_.buf); }
  44. virtual void SetUp() {
  45. InitializeConfig();
  46. SetMode(encoding_mode_);
  47. cfg_.rc_end_usage = VPX_VBR;
  48. cfg_.rc_2pass_vbr_minsection_pct = 5;
  49. cfg_.rc_2pass_vbr_maxsection_pct = 2000;
  50. cfg_.rc_max_quantizer = 56;
  51. cfg_.rc_min_quantizer = 0;
  52. }
  53. virtual void BeginPassHook(unsigned int /*pass*/) {
  54. encoder_initialized_ = false;
  55. abort_ = false;
  56. }
  57. virtual void EndPassHook() {
  58. // For first pass stats test, only run first pass encoder.
  59. if (first_pass_only_ && cfg_.g_pass == VPX_RC_FIRST_PASS)
  60. abort_ |= first_pass_only_;
  61. }
  62. virtual void PreEncodeFrameHook(::libvpx_test::VideoSource * /*video*/,
  63. ::libvpx_test::Encoder *encoder) {
  64. if (!encoder_initialized_) {
  65. // Encode in 2-pass mode.
  66. encoder->Control(VP9E_SET_TILE_COLUMNS, tiles_);
  67. encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
  68. encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
  69. encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
  70. encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
  71. encoder->Control(VP8E_SET_ARNR_TYPE, 3);
  72. encoder->Control(VP9E_SET_FRAME_PARALLEL_DECODING, 0);
  73. if (encoding_mode_ == ::libvpx_test::kTwoPassGood)
  74. encoder->Control(VP9E_SET_ROW_MT, row_mt_mode_);
  75. encoder_initialized_ = true;
  76. }
  77. }
  78. virtual void StatsPktHook(const vpx_codec_cx_pkt_t *pkt) {
  79. const uint8_t *const pkt_buf =
  80. reinterpret_cast<uint8_t *>(pkt->data.twopass_stats.buf);
  81. const size_t pkt_size = pkt->data.twopass_stats.sz;
  82. // First pass stats size equals sizeof(FIRSTPASS_STATS)
  83. EXPECT_EQ(pkt_size, kFirstPassStatsSz)
  84. << "Error: First pass stats size doesn't equal kFirstPassStatsSz";
  85. firstpass_stats_.buf =
  86. realloc(firstpass_stats_.buf, firstpass_stats_.sz + pkt_size);
  87. memcpy((uint8_t *)firstpass_stats_.buf + firstpass_stats_.sz, pkt_buf,
  88. pkt_size);
  89. firstpass_stats_.sz += pkt_size;
  90. }
  91. bool encoder_initialized_;
  92. int tiles_;
  93. ::libvpx_test::TestMode encoding_mode_;
  94. int set_cpu_used_;
  95. int row_mt_mode_;
  96. bool first_pass_only_;
  97. vpx_fixed_buf_t firstpass_stats_;
  98. };
  99. static void compare_fp_stats(vpx_fixed_buf_t *fp_stats, double factor) {
  100. // fp_stats consists of 2 set of first pass encoding stats. These 2 set of
  101. // stats are compared to check if the stats match or at least are very close.
  102. FIRSTPASS_STATS *stats1 = reinterpret_cast<FIRSTPASS_STATS *>(fp_stats->buf);
  103. int nframes_ = (int)(fp_stats->sz / sizeof(FIRSTPASS_STATS));
  104. FIRSTPASS_STATS *stats2 = stats1 + nframes_ / 2;
  105. int i, j;
  106. // The total stats are also output and included in the first pass stats. Here
  107. // ignore that in the comparison.
  108. for (i = 0; i < (nframes_ / 2 - 1); ++i) {
  109. const double *frame_stats1 = reinterpret_cast<double *>(stats1);
  110. const double *frame_stats2 = reinterpret_cast<double *>(stats2);
  111. for (j = 0; j < kDbl; ++j) {
  112. EXPECT_LE(fabs(*frame_stats1 - *frame_stats2),
  113. fabs(*frame_stats1) / factor);
  114. frame_stats1++;
  115. frame_stats2++;
  116. }
  117. stats1++;
  118. stats2++;
  119. }
  120. // Reset firstpass_stats_ to 0.
  121. memset((uint8_t *)fp_stats->buf, 0, fp_stats->sz);
  122. fp_stats->sz = 0;
  123. }
  124. static void compare_fp_stats_md5(vpx_fixed_buf_t *fp_stats) {
  125. // fp_stats consists of 2 set of first pass encoding stats. These 2 set of
  126. // stats are compared to check if the stats match.
  127. uint8_t *stats1 = reinterpret_cast<uint8_t *>(fp_stats->buf);
  128. uint8_t *stats2 = stats1 + fp_stats->sz / 2;
  129. ::libvpx_test::MD5 md5_row_mt_0, md5_row_mt_1;
  130. md5_row_mt_0.Add(stats1, fp_stats->sz / 2);
  131. const char *md5_row_mt_0_str = md5_row_mt_0.Get();
  132. md5_row_mt_1.Add(stats2, fp_stats->sz / 2);
  133. const char *md5_row_mt_1_str = md5_row_mt_1.Get();
  134. // Check md5 match.
  135. ASSERT_STREQ(md5_row_mt_0_str, md5_row_mt_1_str)
  136. << "MD5 checksums don't match";
  137. // Reset firstpass_stats_ to 0.
  138. memset((uint8_t *)fp_stats->buf, 0, fp_stats->sz);
  139. fp_stats->sz = 0;
  140. }
  141. TEST_P(VPxFirstPassEncoderThreadTest, FirstPassStatsTest) {
  142. ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
  143. first_pass_only_ = true;
  144. cfg_.rc_target_bitrate = 1000;
  145. // Test row_mt_mode: 0 vs 1 at single thread case(threads = 1, tiles_ = 0)
  146. tiles_ = 0;
  147. cfg_.g_threads = 1;
  148. row_mt_mode_ = 0;
  149. init_flags_ = VPX_CODEC_USE_PSNR;
  150. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  151. row_mt_mode_ = 1;
  152. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  153. // Compare to check if using or not using row-mt generates close stats.
  154. compare_fp_stats(&firstpass_stats_, 1000.0);
  155. // Test single thread vs multiple threads
  156. row_mt_mode_ = 1;
  157. tiles_ = 0;
  158. cfg_.g_threads = 1;
  159. init_flags_ = VPX_CODEC_USE_PSNR;
  160. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  161. cfg_.g_threads = 4;
  162. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  163. // Compare to check if single-thread and multi-thread stats are close enough.
  164. compare_fp_stats(&firstpass_stats_, 1000.0);
  165. // Bit exact test in row_mt mode.
  166. // When row_mt_mode_=1 and using >1 threads, the encoder generates bit exact
  167. // result.
  168. row_mt_mode_ = 1;
  169. tiles_ = 2;
  170. cfg_.g_threads = 2;
  171. init_flags_ = VPX_CODEC_USE_PSNR;
  172. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  173. cfg_.g_threads = 8;
  174. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  175. // Compare to check if stats match with row-mt=0/1.
  176. compare_fp_stats_md5(&firstpass_stats_);
  177. }
  178. class VPxEncoderThreadTest
  179. : public ::libvpx_test::EncoderTest,
  180. public ::libvpx_test::CodecTestWith4Params<libvpx_test::TestMode, int,
  181. int, int> {
  182. protected:
  183. VPxEncoderThreadTest()
  184. : EncoderTest(GET_PARAM(0)), encoder_initialized_(false),
  185. tiles_(GET_PARAM(3)), threads_(GET_PARAM(4)),
  186. encoding_mode_(GET_PARAM(1)), set_cpu_used_(GET_PARAM(2)) {
  187. init_flags_ = VPX_CODEC_USE_PSNR;
  188. md5_.clear();
  189. row_mt_mode_ = 1;
  190. psnr_ = 0.0;
  191. nframes_ = 0;
  192. }
  193. virtual ~VPxEncoderThreadTest() {}
  194. virtual void SetUp() {
  195. InitializeConfig();
  196. SetMode(encoding_mode_);
  197. if (encoding_mode_ != ::libvpx_test::kRealTime) {
  198. cfg_.rc_end_usage = VPX_VBR;
  199. cfg_.rc_2pass_vbr_minsection_pct = 5;
  200. cfg_.rc_2pass_vbr_maxsection_pct = 2000;
  201. } else {
  202. cfg_.g_lag_in_frames = 0;
  203. cfg_.rc_end_usage = VPX_CBR;
  204. cfg_.g_error_resilient = 1;
  205. }
  206. cfg_.rc_max_quantizer = 56;
  207. cfg_.rc_min_quantizer = 0;
  208. }
  209. virtual void BeginPassHook(unsigned int /*pass*/) {
  210. encoder_initialized_ = false;
  211. psnr_ = 0.0;
  212. nframes_ = 0;
  213. }
  214. virtual void PreEncodeFrameHook(::libvpx_test::VideoSource * /*video*/,
  215. ::libvpx_test::Encoder *encoder) {
  216. if (!encoder_initialized_) {
  217. // Encode 4 column tiles.
  218. encoder->Control(VP9E_SET_TILE_COLUMNS, tiles_);
  219. encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
  220. if (encoding_mode_ != ::libvpx_test::kRealTime) {
  221. encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
  222. encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
  223. encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
  224. encoder->Control(VP8E_SET_ARNR_TYPE, 3);
  225. encoder->Control(VP9E_SET_FRAME_PARALLEL_DECODING, 0);
  226. } else {
  227. encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 0);
  228. encoder->Control(VP9E_SET_AQ_MODE, 3);
  229. }
  230. encoder->Control(VP9E_SET_ROW_MT, row_mt_mode_);
  231. encoder_initialized_ = true;
  232. }
  233. }
  234. virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
  235. psnr_ += pkt->data.psnr.psnr[0];
  236. nframes_++;
  237. }
  238. virtual void DecompressedFrameHook(const vpx_image_t &img,
  239. vpx_codec_pts_t /*pts*/) {
  240. ::libvpx_test::MD5 md5_res;
  241. md5_res.Add(&img);
  242. md5_.push_back(md5_res.Get());
  243. }
  244. virtual bool HandleDecodeResult(const vpx_codec_err_t res,
  245. const libvpx_test::VideoSource & /*video*/,
  246. libvpx_test::Decoder * /*decoder*/) {
  247. if (res != VPX_CODEC_OK) {
  248. EXPECT_EQ(VPX_CODEC_OK, res);
  249. return false;
  250. }
  251. return true;
  252. }
  253. double GetAveragePsnr() const { return nframes_ ? (psnr_ / nframes_) : 0.0; }
  254. bool encoder_initialized_;
  255. int tiles_;
  256. int threads_;
  257. ::libvpx_test::TestMode encoding_mode_;
  258. int set_cpu_used_;
  259. int row_mt_mode_;
  260. double psnr_;
  261. unsigned int nframes_;
  262. std::vector<std::string> md5_;
  263. };
  264. TEST_P(VPxEncoderThreadTest, EncoderResultTest) {
  265. ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 15, 20);
  266. cfg_.rc_target_bitrate = 1000;
  267. // Part 1: Bit exact test for row_mt_mode_ = 0.
  268. // This part keeps original unit tests done before row-mt code is checked in.
  269. row_mt_mode_ = 0;
  270. // Encode using single thread.
  271. cfg_.g_threads = 1;
  272. init_flags_ = VPX_CODEC_USE_PSNR;
  273. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  274. const std::vector<std::string> single_thr_md5 = md5_;
  275. md5_.clear();
  276. // Encode using multiple threads.
  277. cfg_.g_threads = threads_;
  278. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  279. const std::vector<std::string> multi_thr_md5 = md5_;
  280. md5_.clear();
  281. // Compare to check if two vectors are equal.
  282. ASSERT_EQ(single_thr_md5, multi_thr_md5);
  283. // Part 2: row_mt_mode_ = 0 vs row_mt_mode_ = 1 single thread bit exact test.
  284. row_mt_mode_ = 1;
  285. // Encode using single thread
  286. cfg_.g_threads = 1;
  287. init_flags_ = VPX_CODEC_USE_PSNR;
  288. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  289. std::vector<std::string> row_mt_single_thr_md5 = md5_;
  290. md5_.clear();
  291. ASSERT_EQ(single_thr_md5, row_mt_single_thr_md5);
  292. // Part 3: Bit exact test with row-mt on
  293. // When row_mt_mode_=1 and using >1 threads, the encoder generates bit exact
  294. // result.
  295. row_mt_mode_ = 1;
  296. row_mt_single_thr_md5.clear();
  297. // Encode using 2 threads.
  298. cfg_.g_threads = 2;
  299. init_flags_ = VPX_CODEC_USE_PSNR;
  300. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  301. row_mt_single_thr_md5 = md5_;
  302. md5_.clear();
  303. // Encode using multiple threads.
  304. cfg_.g_threads = threads_;
  305. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  306. const std::vector<std::string> row_mt_multi_thr_md5 = md5_;
  307. md5_.clear();
  308. // Compare to check if two vectors are equal.
  309. ASSERT_EQ(row_mt_single_thr_md5, row_mt_multi_thr_md5);
  310. // Part 4: PSNR test with bit_match_mode_ = 0
  311. row_mt_mode_ = 1;
  312. // Encode using single thread.
  313. cfg_.g_threads = 1;
  314. init_flags_ = VPX_CODEC_USE_PSNR;
  315. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  316. const double single_thr_psnr = GetAveragePsnr();
  317. // Encode using multiple threads.
  318. cfg_.g_threads = threads_;
  319. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  320. const double multi_thr_psnr = GetAveragePsnr();
  321. EXPECT_NEAR(single_thr_psnr, multi_thr_psnr, 0.1);
  322. }
  323. INSTANTIATE_TEST_CASE_P(
  324. VP9, VPxFirstPassEncoderThreadTest,
  325. ::testing::Combine(
  326. ::testing::Values(
  327. static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP9)),
  328. ::testing::Values(::libvpx_test::kTwoPassGood),
  329. ::testing::Range(0, 4))); // cpu_used
  330. // Split this into two instantiations so that we can distinguish
  331. // between very slow runs ( ie cpu_speed 0 ) vs ones that can be
  332. // run nightly by adding Large to the title.
  333. INSTANTIATE_TEST_CASE_P(
  334. VP9, VPxEncoderThreadTest,
  335. ::testing::Combine(
  336. ::testing::Values(
  337. static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP9)),
  338. ::testing::Values(::libvpx_test::kTwoPassGood,
  339. ::libvpx_test::kOnePassGood,
  340. ::libvpx_test::kRealTime),
  341. ::testing::Range(3, 9), // cpu_used
  342. ::testing::Range(0, 3), // tile_columns
  343. ::testing::Range(2, 5))); // threads
  344. INSTANTIATE_TEST_CASE_P(
  345. VP9Large, VPxEncoderThreadTest,
  346. ::testing::Combine(
  347. ::testing::Values(
  348. static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP9)),
  349. ::testing::Values(::libvpx_test::kTwoPassGood,
  350. ::libvpx_test::kOnePassGood,
  351. ::libvpx_test::kRealTime),
  352. ::testing::Range(0, 3), // cpu_used
  353. ::testing::Range(0, 3), // tile_columns
  354. ::testing::Range(2, 5))); // threads
  355. } // namespace