1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <string>
13 #include "third_party/googletest/src/googletest/include/gtest/gtest.h"
14 
15 #include "config/aom_config.h"
16 #include "config/aom_version.h"
17 
18 #include "test/codec_factory.h"
19 #include "test/encode_test_driver.h"
20 #include "test/i420_video_source.h"
21 #include "test/util.h"
22 #include "test/y4m_video_source.h"
23 #include "aom_ports/aom_timer.h"
24 
25 namespace {
26 
27 const int kMaxPsnr = 100;
28 const double kUsecsInSec = 1000000.0;
29 
30 struct EncodePerfTestVideo {
EncodePerfTestVideo__anon5fda42a00111::EncodePerfTestVideo31   EncodePerfTestVideo(const char *name_, uint32_t width_, uint32_t height_,
32                       uint32_t bitrate_, int frames_)
33       : name(name_), width(width_), height(height_), bitrate(bitrate_),
34         frames(frames_) {}
35   const char *name;
36   uint32_t width;
37   uint32_t height;
38   uint32_t bitrate;
39   int frames;
40 };
41 
42 const EncodePerfTestVideo kAV1EncodePerfTestVectors[] = {
43   EncodePerfTestVideo("desktop_640_360_30.yuv", 640, 360, 200, 2484),
44   EncodePerfTestVideo("kirland_640_480_30.yuv", 640, 480, 200, 300),
45   EncodePerfTestVideo("macmarcomoving_640_480_30.yuv", 640, 480, 200, 987),
46   EncodePerfTestVideo("macmarcostationary_640_480_30.yuv", 640, 480, 200, 718),
47   EncodePerfTestVideo("niklas_640_480_30.yuv", 640, 480, 200, 471),
48   EncodePerfTestVideo("tacomanarrows_640_480_30.yuv", 640, 480, 200, 300),
49   EncodePerfTestVideo("tacomasmallcameramovement_640_480_30.yuv", 640, 480, 200,
50                       300),
51   EncodePerfTestVideo("thaloundeskmtg_640_480_30.yuv", 640, 480, 200, 300),
52   EncodePerfTestVideo("niklas_1280_720_30.yuv", 1280, 720, 600, 470),
53 };
54 
55 const int kEncodePerfTestSpeeds[] = { 5, 6, 7, 8 };
56 const int kEncodePerfTestThreads[] = { 1, 2, 4 };
57 
58 class AV1EncodePerfTest
59     : public ::libaom_test::CodecTestWithParam<libaom_test::TestMode>,
60       public ::libaom_test::EncoderTest {
61  protected:
AV1EncodePerfTest()62   AV1EncodePerfTest()
63       : EncoderTest(GET_PARAM(0)), min_psnr_(kMaxPsnr), nframes_(0),
64         encoding_mode_(GET_PARAM(1)), speed_(0), threads_(1) {}
65 
~AV1EncodePerfTest()66   virtual ~AV1EncodePerfTest() {}
67 
SetUp()68   virtual void SetUp() {
69     InitializeConfig();
70     SetMode(encoding_mode_);
71 
72     cfg_.g_lag_in_frames = 0;
73     cfg_.rc_min_quantizer = 2;
74     cfg_.rc_max_quantizer = 56;
75     cfg_.rc_dropframe_thresh = 0;
76     cfg_.rc_undershoot_pct = 50;
77     cfg_.rc_overshoot_pct = 50;
78     cfg_.rc_buf_sz = 1000;
79     cfg_.rc_buf_initial_sz = 500;
80     cfg_.rc_buf_optimal_sz = 600;
81     cfg_.rc_end_usage = AOM_CBR;
82     cfg_.g_error_resilient = 1;
83     cfg_.g_threads = threads_;
84   }
85 
PreEncodeFrameHook(::libaom_test::VideoSource * video,::libaom_test::Encoder * encoder)86   virtual void PreEncodeFrameHook(::libaom_test::VideoSource *video,
87                                   ::libaom_test::Encoder *encoder) {
88     if (video->frame() == 0) {
89       const int log2_tile_columns = 3;
90       encoder->Control(AOME_SET_CPUUSED, speed_);
91       encoder->Control(AV1E_SET_TILE_COLUMNS, log2_tile_columns);
92       encoder->Control(AV1E_SET_FRAME_PARALLEL_DECODING, 1);
93       encoder->Control(AOME_SET_ENABLEAUTOALTREF, 0);
94     }
95   }
96 
BeginPassHook(unsigned int)97   virtual void BeginPassHook(unsigned int /*pass*/) {
98     min_psnr_ = kMaxPsnr;
99     nframes_ = 0;
100   }
101 
PSNRPktHook(const aom_codec_cx_pkt_t * pkt)102   virtual void PSNRPktHook(const aom_codec_cx_pkt_t *pkt) {
103     if (pkt->data.psnr.psnr[0] < min_psnr_) {
104       min_psnr_ = pkt->data.psnr.psnr[0];
105     }
106   }
107 
108   // for performance reasons don't decode
DoDecode()109   virtual bool DoDecode() { return 0; }
110 
min_psnr() const111   double min_psnr() const { return min_psnr_; }
112 
set_speed(unsigned int speed)113   void set_speed(unsigned int speed) { speed_ = speed; }
114 
set_threads(unsigned int threads)115   void set_threads(unsigned int threads) { threads_ = threads; }
116 
117  private:
118   double min_psnr_;
119   unsigned int nframes_;
120   libaom_test::TestMode encoding_mode_;
121   unsigned speed_;
122   unsigned int threads_;
123 };
124 
TEST_P(AV1EncodePerfTest,PerfTest)125 TEST_P(AV1EncodePerfTest, PerfTest) {
126   for (size_t i = 0; i < NELEMENTS(kAV1EncodePerfTestVectors); ++i) {
127     for (size_t j = 0; j < NELEMENTS(kEncodePerfTestSpeeds); ++j) {
128       for (size_t k = 0; k < NELEMENTS(kEncodePerfTestThreads); ++k) {
129         if (kAV1EncodePerfTestVectors[i].width < 512 &&
130             kEncodePerfTestThreads[k] > 1)
131           continue;
132         else if (kAV1EncodePerfTestVectors[i].width < 1024 &&
133                  kEncodePerfTestThreads[k] > 2)
134           continue;
135 
136         set_threads(kEncodePerfTestThreads[k]);
137         SetUp();
138 
139         const aom_rational timebase = { 33333333, 1000000000 };
140         cfg_.g_timebase = timebase;
141         cfg_.rc_target_bitrate = kAV1EncodePerfTestVectors[i].bitrate;
142 
143         init_flags_ = AOM_CODEC_USE_PSNR;
144 
145         const unsigned frames = kAV1EncodePerfTestVectors[i].frames;
146         const char *video_name = kAV1EncodePerfTestVectors[i].name;
147         libaom_test::I420VideoSource video(
148             video_name, kAV1EncodePerfTestVectors[i].width,
149             kAV1EncodePerfTestVectors[i].height, timebase.den, timebase.num, 0,
150             kAV1EncodePerfTestVectors[i].frames);
151         set_speed(kEncodePerfTestSpeeds[j]);
152 
153         aom_usec_timer t;
154         aom_usec_timer_start(&t);
155 
156         ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
157 
158         aom_usec_timer_mark(&t);
159         const double elapsed_secs = aom_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 AV1_INSTANTIATE_TEST_CASE(AV1EncodePerfTest,
187                           ::testing::Values(::libaom_test::kRealTime));
188 }  // namespace
189