vp9_ethread_test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. ASSERT_LE(fabs(*frame_stats1 - *frame_stats2),
  113. fabs(*frame_stats1) / factor)
  114. << "First failure @ frame #" << i << " stat #" << j << " ("
  115. << *frame_stats1 << " vs. " << *frame_stats2 << ")";
  116. frame_stats1++;
  117. frame_stats2++;
  118. }
  119. stats1++;
  120. stats2++;
  121. }
  122. // Reset firstpass_stats_ to 0.
  123. memset((uint8_t *)fp_stats->buf, 0, fp_stats->sz);
  124. fp_stats->sz = 0;
  125. }
  126. static void compare_fp_stats_md5(vpx_fixed_buf_t *fp_stats) {
  127. // fp_stats consists of 2 set of first pass encoding stats. These 2 set of
  128. // stats are compared to check if the stats match.
  129. uint8_t *stats1 = reinterpret_cast<uint8_t *>(fp_stats->buf);
  130. uint8_t *stats2 = stats1 + fp_stats->sz / 2;
  131. ::libvpx_test::MD5 md5_row_mt_0, md5_row_mt_1;
  132. md5_row_mt_0.Add(stats1, fp_stats->sz / 2);
  133. const char *md5_row_mt_0_str = md5_row_mt_0.Get();
  134. md5_row_mt_1.Add(stats2, fp_stats->sz / 2);
  135. const char *md5_row_mt_1_str = md5_row_mt_1.Get();
  136. // Check md5 match.
  137. ASSERT_STREQ(md5_row_mt_0_str, md5_row_mt_1_str)
  138. << "MD5 checksums don't match";
  139. // Reset firstpass_stats_ to 0.
  140. memset((uint8_t *)fp_stats->buf, 0, fp_stats->sz);
  141. fp_stats->sz = 0;
  142. }
  143. TEST_P(VPxFirstPassEncoderThreadTest, FirstPassStatsTest) {
  144. ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 0, 60);
  145. first_pass_only_ = true;
  146. cfg_.rc_target_bitrate = 1000;
  147. // Test row_mt_mode: 0 vs 1 at single thread case(threads = 1, tiles_ = 0)
  148. tiles_ = 0;
  149. cfg_.g_threads = 1;
  150. row_mt_mode_ = 0;
  151. init_flags_ = VPX_CODEC_USE_PSNR;
  152. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  153. row_mt_mode_ = 1;
  154. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  155. // Compare to check if using or not using row-mt generates close stats.
  156. ASSERT_NO_FATAL_FAILURE(compare_fp_stats(&firstpass_stats_, 1000.0));
  157. // Test single thread vs multiple threads
  158. row_mt_mode_ = 1;
  159. tiles_ = 0;
  160. cfg_.g_threads = 1;
  161. init_flags_ = VPX_CODEC_USE_PSNR;
  162. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  163. cfg_.g_threads = 4;
  164. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  165. // Compare to check if single-thread and multi-thread stats are close enough.
  166. ASSERT_NO_FATAL_FAILURE(compare_fp_stats(&firstpass_stats_, 1000.0));
  167. // Bit exact test in row_mt mode.
  168. // When row_mt_mode_=1 and using >1 threads, the encoder generates bit exact
  169. // result.
  170. row_mt_mode_ = 1;
  171. tiles_ = 2;
  172. cfg_.g_threads = 2;
  173. init_flags_ = VPX_CODEC_USE_PSNR;
  174. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  175. cfg_.g_threads = 8;
  176. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  177. // Compare to check if stats match with row-mt=0/1.
  178. compare_fp_stats_md5(&firstpass_stats_);
  179. }
  180. class VPxEncoderThreadTest
  181. : public ::libvpx_test::EncoderTest,
  182. public ::libvpx_test::CodecTestWith4Params<libvpx_test::TestMode, int,
  183. int, int> {
  184. protected:
  185. VPxEncoderThreadTest()
  186. : EncoderTest(GET_PARAM(0)), encoder_initialized_(false),
  187. tiles_(GET_PARAM(3)), threads_(GET_PARAM(4)),
  188. encoding_mode_(GET_PARAM(1)), set_cpu_used_(GET_PARAM(2)) {
  189. init_flags_ = VPX_CODEC_USE_PSNR;
  190. md5_.clear();
  191. row_mt_mode_ = 1;
  192. psnr_ = 0.0;
  193. nframes_ = 0;
  194. }
  195. virtual ~VPxEncoderThreadTest() {}
  196. virtual void SetUp() {
  197. InitializeConfig();
  198. SetMode(encoding_mode_);
  199. if (encoding_mode_ != ::libvpx_test::kRealTime) {
  200. cfg_.rc_end_usage = VPX_VBR;
  201. cfg_.rc_2pass_vbr_minsection_pct = 5;
  202. cfg_.rc_2pass_vbr_maxsection_pct = 2000;
  203. } else {
  204. cfg_.g_lag_in_frames = 0;
  205. cfg_.rc_end_usage = VPX_CBR;
  206. cfg_.g_error_resilient = 1;
  207. }
  208. cfg_.rc_max_quantizer = 56;
  209. cfg_.rc_min_quantizer = 0;
  210. }
  211. virtual void BeginPassHook(unsigned int /*pass*/) {
  212. encoder_initialized_ = false;
  213. psnr_ = 0.0;
  214. nframes_ = 0;
  215. }
  216. virtual void PreEncodeFrameHook(::libvpx_test::VideoSource * /*video*/,
  217. ::libvpx_test::Encoder *encoder) {
  218. if (!encoder_initialized_) {
  219. // Encode 4 column tiles.
  220. encoder->Control(VP9E_SET_TILE_COLUMNS, tiles_);
  221. encoder->Control(VP8E_SET_CPUUSED, set_cpu_used_);
  222. if (encoding_mode_ != ::libvpx_test::kRealTime) {
  223. encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 1);
  224. encoder->Control(VP8E_SET_ARNR_MAXFRAMES, 7);
  225. encoder->Control(VP8E_SET_ARNR_STRENGTH, 5);
  226. encoder->Control(VP8E_SET_ARNR_TYPE, 3);
  227. encoder->Control(VP9E_SET_FRAME_PARALLEL_DECODING, 0);
  228. } else {
  229. encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 0);
  230. encoder->Control(VP9E_SET_AQ_MODE, 3);
  231. }
  232. encoder->Control(VP9E_SET_ROW_MT, row_mt_mode_);
  233. encoder_initialized_ = true;
  234. }
  235. }
  236. virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
  237. psnr_ += pkt->data.psnr.psnr[0];
  238. nframes_++;
  239. }
  240. virtual void DecompressedFrameHook(const vpx_image_t &img,
  241. vpx_codec_pts_t /*pts*/) {
  242. ::libvpx_test::MD5 md5_res;
  243. md5_res.Add(&img);
  244. md5_.push_back(md5_res.Get());
  245. }
  246. virtual bool HandleDecodeResult(const vpx_codec_err_t res,
  247. const libvpx_test::VideoSource & /*video*/,
  248. libvpx_test::Decoder * /*decoder*/) {
  249. if (res != VPX_CODEC_OK) {
  250. EXPECT_EQ(VPX_CODEC_OK, res);
  251. return false;
  252. }
  253. return true;
  254. }
  255. double GetAveragePsnr() const { return nframes_ ? (psnr_ / nframes_) : 0.0; }
  256. bool encoder_initialized_;
  257. int tiles_;
  258. int threads_;
  259. ::libvpx_test::TestMode encoding_mode_;
  260. int set_cpu_used_;
  261. int row_mt_mode_;
  262. double psnr_;
  263. unsigned int nframes_;
  264. std::vector<std::string> md5_;
  265. };
  266. TEST_P(VPxEncoderThreadTest, EncoderResultTest) {
  267. ::libvpx_test::Y4mVideoSource video("niklas_1280_720_30.y4m", 15, 20);
  268. cfg_.rc_target_bitrate = 1000;
  269. // Part 1: Bit exact test for row_mt_mode_ = 0.
  270. // This part keeps original unit tests done before row-mt code is checked in.
  271. row_mt_mode_ = 0;
  272. // Encode using single thread.
  273. cfg_.g_threads = 1;
  274. init_flags_ = VPX_CODEC_USE_PSNR;
  275. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  276. const std::vector<std::string> single_thr_md5 = md5_;
  277. md5_.clear();
  278. // Encode using multiple threads.
  279. cfg_.g_threads = threads_;
  280. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  281. const std::vector<std::string> multi_thr_md5 = md5_;
  282. md5_.clear();
  283. // Compare to check if two vectors are equal.
  284. ASSERT_EQ(single_thr_md5, multi_thr_md5);
  285. // Part 2: row_mt_mode_ = 0 vs row_mt_mode_ = 1 single thread bit exact test.
  286. row_mt_mode_ = 1;
  287. // Encode using single thread
  288. cfg_.g_threads = 1;
  289. init_flags_ = VPX_CODEC_USE_PSNR;
  290. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  291. std::vector<std::string> row_mt_single_thr_md5 = md5_;
  292. md5_.clear();
  293. ASSERT_EQ(single_thr_md5, row_mt_single_thr_md5);
  294. // Part 3: Bit exact test with row-mt on
  295. // When row_mt_mode_=1 and using >1 threads, the encoder generates bit exact
  296. // result.
  297. row_mt_mode_ = 1;
  298. row_mt_single_thr_md5.clear();
  299. // Encode using 2 threads.
  300. cfg_.g_threads = 2;
  301. init_flags_ = VPX_CODEC_USE_PSNR;
  302. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  303. row_mt_single_thr_md5 = md5_;
  304. md5_.clear();
  305. // Encode using multiple threads.
  306. cfg_.g_threads = threads_;
  307. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  308. const std::vector<std::string> row_mt_multi_thr_md5 = md5_;
  309. md5_.clear();
  310. // Compare to check if two vectors are equal.
  311. ASSERT_EQ(row_mt_single_thr_md5, row_mt_multi_thr_md5);
  312. // Part 4: PSNR test with bit_match_mode_ = 0
  313. row_mt_mode_ = 1;
  314. // Encode using single thread.
  315. cfg_.g_threads = 1;
  316. init_flags_ = VPX_CODEC_USE_PSNR;
  317. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  318. const double single_thr_psnr = GetAveragePsnr();
  319. // Encode using multiple threads.
  320. cfg_.g_threads = threads_;
  321. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  322. const double multi_thr_psnr = GetAveragePsnr();
  323. EXPECT_NEAR(single_thr_psnr, multi_thr_psnr, 0.2);
  324. }
  325. INSTANTIATE_TEST_CASE_P(
  326. VP9, VPxFirstPassEncoderThreadTest,
  327. ::testing::Combine(
  328. ::testing::Values(
  329. static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP9)),
  330. ::testing::Values(::libvpx_test::kTwoPassGood),
  331. ::testing::Range(0, 4))); // cpu_used
  332. // Split this into two instantiations so that we can distinguish
  333. // between very slow runs ( ie cpu_speed 0 ) vs ones that can be
  334. // run nightly by adding Large to the title.
  335. INSTANTIATE_TEST_CASE_P(
  336. VP9, VPxEncoderThreadTest,
  337. ::testing::Combine(
  338. ::testing::Values(
  339. static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP9)),
  340. ::testing::Values(::libvpx_test::kTwoPassGood,
  341. ::libvpx_test::kOnePassGood,
  342. ::libvpx_test::kRealTime),
  343. ::testing::Range(3, 10), // cpu_used
  344. ::testing::Range(0, 3), // tile_columns
  345. ::testing::Range(2, 5))); // threads
  346. INSTANTIATE_TEST_CASE_P(
  347. VP9Large, VPxEncoderThreadTest,
  348. ::testing::Combine(
  349. ::testing::Values(
  350. static_cast<const libvpx_test::CodecFactory *>(&libvpx_test::kVP9)),
  351. ::testing::Values(::libvpx_test::kTwoPassGood,
  352. ::libvpx_test::kOnePassGood,
  353. ::libvpx_test::kRealTime),
  354. ::testing::Range(0, 3), // cpu_used
  355. ::testing::Range(0, 3), // tile_columns
  356. ::testing::Range(2, 5))); // threads
  357. } // namespace