encode_perf_test.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "third_party/googletest/src/include/gtest/gtest.h"
  12. #include "./vpx_config.h"
  13. #include "./vpx_version.h"
  14. #include "test/codec_factory.h"
  15. #include "test/encode_test_driver.h"
  16. #include "test/i420_video_source.h"
  17. #include "test/util.h"
  18. #include "test/y4m_video_source.h"
  19. #include "vpx_ports/vpx_timer.h"
  20. namespace {
  21. const int kMaxPsnr = 100;
  22. const double kUsecsInSec = 1000000.0;
  23. struct EncodePerfTestVideo {
  24. EncodePerfTestVideo(const char *name_, uint32_t width_, uint32_t height_,
  25. uint32_t bitrate_, int frames_)
  26. : name(name_), width(width_), height(height_), bitrate(bitrate_),
  27. frames(frames_) {}
  28. const char *name;
  29. uint32_t width;
  30. uint32_t height;
  31. uint32_t bitrate;
  32. int frames;
  33. };
  34. const EncodePerfTestVideo kVP9EncodePerfTestVectors[] = {
  35. EncodePerfTestVideo("desktop_640_360_30.yuv", 640, 360, 200, 2484),
  36. EncodePerfTestVideo("kirland_640_480_30.yuv", 640, 480, 200, 300),
  37. EncodePerfTestVideo("macmarcomoving_640_480_30.yuv", 640, 480, 200, 987),
  38. EncodePerfTestVideo("macmarcostationary_640_480_30.yuv", 640, 480, 200, 718),
  39. EncodePerfTestVideo("niklas_640_480_30.yuv", 640, 480, 200, 471),
  40. EncodePerfTestVideo("tacomanarrows_640_480_30.yuv", 640, 480, 200, 300),
  41. EncodePerfTestVideo("tacomasmallcameramovement_640_480_30.yuv", 640, 480, 200,
  42. 300),
  43. EncodePerfTestVideo("thaloundeskmtg_640_480_30.yuv", 640, 480, 200, 300),
  44. EncodePerfTestVideo("niklas_1280_720_30.yuv", 1280, 720, 600, 470),
  45. };
  46. const int kEncodePerfTestSpeeds[] = { 5, 6, 7, 8, 9 };
  47. const int kEncodePerfTestThreads[] = { 1, 2, 4 };
  48. #define NELEMENTS(x) (sizeof((x)) / sizeof((x)[0]))
  49. class VP9EncodePerfTest
  50. : public ::libvpx_test::EncoderTest,
  51. public ::libvpx_test::CodecTestWithParam<libvpx_test::TestMode> {
  52. protected:
  53. VP9EncodePerfTest()
  54. : EncoderTest(GET_PARAM(0)), min_psnr_(kMaxPsnr), nframes_(0),
  55. encoding_mode_(GET_PARAM(1)), speed_(0), threads_(1) {}
  56. virtual ~VP9EncodePerfTest() {}
  57. virtual void SetUp() {
  58. InitializeConfig();
  59. SetMode(encoding_mode_);
  60. cfg_.g_lag_in_frames = 0;
  61. cfg_.rc_min_quantizer = 2;
  62. cfg_.rc_max_quantizer = 56;
  63. cfg_.rc_dropframe_thresh = 0;
  64. cfg_.rc_undershoot_pct = 50;
  65. cfg_.rc_overshoot_pct = 50;
  66. cfg_.rc_buf_sz = 1000;
  67. cfg_.rc_buf_initial_sz = 500;
  68. cfg_.rc_buf_optimal_sz = 600;
  69. cfg_.rc_resize_allowed = 0;
  70. cfg_.rc_end_usage = VPX_CBR;
  71. cfg_.g_error_resilient = 1;
  72. cfg_.g_threads = threads_;
  73. }
  74. virtual void PreEncodeFrameHook(::libvpx_test::VideoSource *video,
  75. ::libvpx_test::Encoder *encoder) {
  76. if (video->frame() == 0) {
  77. const int log2_tile_columns = 3;
  78. encoder->Control(VP8E_SET_CPUUSED, speed_);
  79. encoder->Control(VP9E_SET_TILE_COLUMNS, log2_tile_columns);
  80. encoder->Control(VP9E_SET_FRAME_PARALLEL_DECODING, 1);
  81. encoder->Control(VP8E_SET_ENABLEAUTOALTREF, 0);
  82. }
  83. }
  84. virtual void BeginPassHook(unsigned int /*pass*/) {
  85. min_psnr_ = kMaxPsnr;
  86. nframes_ = 0;
  87. }
  88. virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
  89. if (pkt->data.psnr.psnr[0] < min_psnr_) {
  90. min_psnr_ = pkt->data.psnr.psnr[0];
  91. }
  92. }
  93. // for performance reasons don't decode
  94. virtual bool DoDecode() const { return false; }
  95. double min_psnr() const { return min_psnr_; }
  96. void set_speed(unsigned int speed) { speed_ = speed; }
  97. void set_threads(unsigned int threads) { threads_ = threads; }
  98. private:
  99. double min_psnr_;
  100. unsigned int nframes_;
  101. libvpx_test::TestMode encoding_mode_;
  102. unsigned speed_;
  103. unsigned int threads_;
  104. };
  105. TEST_P(VP9EncodePerfTest, PerfTest) {
  106. for (size_t i = 0; i < NELEMENTS(kVP9EncodePerfTestVectors); ++i) {
  107. for (size_t j = 0; j < NELEMENTS(kEncodePerfTestSpeeds); ++j) {
  108. for (size_t k = 0; k < NELEMENTS(kEncodePerfTestThreads); ++k) {
  109. if (kVP9EncodePerfTestVectors[i].width < 512 &&
  110. kEncodePerfTestThreads[k] > 1) {
  111. continue;
  112. } else if (kVP9EncodePerfTestVectors[i].width < 1024 &&
  113. kEncodePerfTestThreads[k] > 2) {
  114. continue;
  115. }
  116. set_threads(kEncodePerfTestThreads[k]);
  117. SetUp();
  118. const vpx_rational timebase = { 33333333, 1000000000 };
  119. cfg_.g_timebase = timebase;
  120. cfg_.rc_target_bitrate = kVP9EncodePerfTestVectors[i].bitrate;
  121. init_flags_ = VPX_CODEC_USE_PSNR;
  122. const unsigned frames = kVP9EncodePerfTestVectors[i].frames;
  123. const char *video_name = kVP9EncodePerfTestVectors[i].name;
  124. libvpx_test::I420VideoSource video(
  125. video_name, kVP9EncodePerfTestVectors[i].width,
  126. kVP9EncodePerfTestVectors[i].height, timebase.den, timebase.num, 0,
  127. kVP9EncodePerfTestVectors[i].frames);
  128. set_speed(kEncodePerfTestSpeeds[j]);
  129. vpx_usec_timer t;
  130. vpx_usec_timer_start(&t);
  131. ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
  132. vpx_usec_timer_mark(&t);
  133. const double elapsed_secs = vpx_usec_timer_elapsed(&t) / kUsecsInSec;
  134. const double fps = frames / elapsed_secs;
  135. const double minimum_psnr = min_psnr();
  136. std::string display_name(video_name);
  137. if (kEncodePerfTestThreads[k] > 1) {
  138. char thread_count[32];
  139. snprintf(thread_count, sizeof(thread_count), "_t-%d",
  140. kEncodePerfTestThreads[k]);
  141. display_name += thread_count;
  142. }
  143. printf("{\n");
  144. printf("\t\"type\" : \"encode_perf_test\",\n");
  145. printf("\t\"version\" : \"%s\",\n", VERSION_STRING_NOSP);
  146. printf("\t\"videoName\" : \"%s\",\n", display_name.c_str());
  147. printf("\t\"encodeTimeSecs\" : %f,\n", elapsed_secs);
  148. printf("\t\"totalFrames\" : %u,\n", frames);
  149. printf("\t\"framesPerSecond\" : %f,\n", fps);
  150. printf("\t\"minPsnr\" : %f,\n", minimum_psnr);
  151. printf("\t\"speed\" : %d,\n", kEncodePerfTestSpeeds[j]);
  152. printf("\t\"threads\" : %d\n", kEncodePerfTestThreads[k]);
  153. printf("}\n");
  154. }
  155. }
  156. }
  157. }
  158. VP9_INSTANTIATE_TEST_CASE(VP9EncodePerfTest,
  159. ::testing::Values(::libvpx_test::kRealTime));
  160. } // namespace