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