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