shithub: libvpx

Download patch

ref: d1ed2f0d7a8414a07e3283d54dff97d7d2ed411e
parent: 34034789d758440415306a637e6aea57f34e1a4c
author: angiebird <angiebird@google.com>
date: Fri May 29 12:26:57 EDT 2020

Refactor simple_encode_test.cc

1) Avoid using global variables.

2) Add comments to EncodeConsistencyTest.

3) Check frame_type and show_idx in EncodeConsistencyTest.

Change-Id: I2261a0bd65189beb70432d62c077ef618a2712ab

--- a/test/simple_encode_test.cc
+++ b/test/simple_encode_test.cc
@@ -10,6 +10,7 @@
 
 #include <math.h>
 #include <memory>
+#include <string>
 #include <vector>
 #include "third_party/googletest/src/include/gtest/gtest.h"
 #include "vp9/simple_encode.h"
@@ -17,15 +18,6 @@
 namespace vp9 {
 namespace {
 
-// TODO(angirbid): Find a better way to construct encode info
-const int w = 352;
-const int h = 288;
-const int frame_rate_num = 30;
-const int frame_rate_den = 1;
-const int target_bitrate = 1000;
-const int num_frames = 17;
-const char infile_path[] = "bus_352x288_420_f20_b8.yuv";
-
 double GetBitrateInKbps(size_t bit_size, int num_frames, int frame_rate_num,
                         int frame_rate_den) {
   return static_cast<double>(bit_size) / num_frames * frame_rate_num /
@@ -36,14 +28,26 @@
 // For example, if size is 7, return 2.
 int GetNumUnit4x4(int size) { return (size + 3) >> 2; }
 
-TEST(SimpleEncode, ComputeFirstPassStats) {
-  SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                             target_bitrate, num_frames, infile_path);
+class SimpleEncodeTest : public ::testing::Test {
+ protected:
+  const int width_ = 352;
+  const int height_ = 288;
+  const int frame_rate_num_ = 30;
+  const int frame_rate_den_ = 1;
+  const int target_bitrate_ = 1000;
+  const int num_frames_ = 17;
+  const std::string in_file_path_str_ = "bus_352x288_420_f20_b8.yuv";
+};
+
+TEST_F(SimpleEncodeTest, ComputeFirstPassStats) {
+  SimpleEncode simple_encode(width_, height_, frame_rate_num_, frame_rate_den_,
+                             target_bitrate_, num_frames_,
+                             in_file_path_str_.c_str());
   simple_encode.ComputeFirstPassStats();
   std::vector<std::vector<double>> frame_stats =
       simple_encode.ObserveFirstPassStats();
-  EXPECT_EQ(frame_stats.size(), static_cast<size_t>(num_frames));
-  size_t data_num = frame_stats[0].size();
+  EXPECT_EQ(frame_stats.size(), static_cast<size_t>(num_frames_));
+  const size_t data_num = frame_stats[0].size();
   // Read ObserveFirstPassStats before changing FIRSTPASS_STATS.
   EXPECT_EQ(data_num, static_cast<size_t>(25));
   for (size_t i = 0; i < frame_stats.size(); ++i) {
@@ -56,25 +60,27 @@
   }
 }
 
-TEST(SimpleEncode, GetCodingFrameNum) {
-  SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                             target_bitrate, num_frames, infile_path);
+TEST_F(SimpleEncodeTest, GetCodingFrameNum) {
+  SimpleEncode simple_encode(width_, height_, frame_rate_num_, frame_rate_den_,
+                             target_bitrate_, num_frames_,
+                             in_file_path_str_.c_str());
   simple_encode.ComputeFirstPassStats();
-  int num_coding_frames = simple_encode.GetCodingFrameNum();
+  const int num_coding_frames = simple_encode.GetCodingFrameNum();
   EXPECT_EQ(num_coding_frames, 19);
 }
 
-TEST(SimpleEncode, EncodeFrame) {
-  SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                             target_bitrate, num_frames, infile_path);
+TEST_F(SimpleEncodeTest, EncodeFrame) {
+  SimpleEncode simple_encode(width_, height_, frame_rate_num_, frame_rate_den_,
+                             target_bitrate_, num_frames_,
+                             in_file_path_str_.c_str());
   simple_encode.ComputeFirstPassStats();
   int num_coding_frames = simple_encode.GetCodingFrameNum();
-  EXPECT_GE(num_coding_frames, num_frames);
+  EXPECT_GE(num_coding_frames, num_frames_);
   simple_encode.StartEncode();
   size_t total_data_bit_size = 0;
   int coded_show_frame_count = 0;
   int frame_coding_index = 0;
-  while (coded_show_frame_count < num_frames) {
+  while (coded_show_frame_count < num_frames_) {
     const GroupOfPicture group_of_picture =
         simple_encode.ObserveGroupOfPicture();
     const std::vector<EncodeFrameInfo> &encode_frame_list =
@@ -99,22 +105,23 @@
     }
     coded_show_frame_count += group_of_picture.show_frame_count;
   }
-  const double bitrate = GetBitrateInKbps(total_data_bit_size, num_frames,
-                                          frame_rate_num, frame_rate_den);
+  const double bitrate = GetBitrateInKbps(total_data_bit_size, num_frames_,
+                                          frame_rate_num_, frame_rate_den_);
   const double off_target_threshold = 150;
-  EXPECT_LE(fabs(target_bitrate - bitrate), off_target_threshold);
+  EXPECT_LE(fabs(target_bitrate_ - bitrate), off_target_threshold);
   simple_encode.EndEncode();
 }
 
-TEST(SimpleEncode, ObserveKeyFrameMap) {
-  SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                             target_bitrate, num_frames, infile_path);
+TEST_F(SimpleEncodeTest, ObserveKeyFrameMap) {
+  SimpleEncode simple_encode(width_, height_, frame_rate_num_, frame_rate_den_,
+                             target_bitrate_, num_frames_,
+                             in_file_path_str_.c_str());
   simple_encode.ComputeFirstPassStats();
   std::vector<int> key_frame_map = simple_encode.ObserveKeyFrameMap();
-  EXPECT_EQ(key_frame_map.size(), static_cast<size_t>(num_frames));
+  EXPECT_EQ(key_frame_map.size(), static_cast<size_t>(num_frames_));
   simple_encode.StartEncode();
   int coded_show_frame_count = 0;
-  while (coded_show_frame_count < num_frames) {
+  while (coded_show_frame_count < num_frames_) {
     const GroupOfPicture group_of_picture =
         simple_encode.ObserveGroupOfPicture();
     const std::vector<EncodeFrameInfo> &encode_frame_list =
@@ -134,11 +141,12 @@
   simple_encode.EndEncode();
 }
 
-TEST(SimpleEncode, EncodeFrameWithQuantizeIndex) {
-  SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                             target_bitrate, num_frames, infile_path);
+TEST_F(SimpleEncodeTest, EncodeFrameWithQuantizeIndex) {
+  SimpleEncode simple_encode(width_, height_, frame_rate_num_, frame_rate_den_,
+                             target_bitrate_, num_frames_,
+                             in_file_path_str_.c_str());
   simple_encode.ComputeFirstPassStats();
-  int num_coding_frames = simple_encode.GetCodingFrameNum();
+  const int num_coding_frames = simple_encode.GetCodingFrameNum();
   simple_encode.StartEncode();
   for (int i = 0; i < num_coding_frames; ++i) {
     const int assigned_quantize_index = 100 + i;
@@ -150,15 +158,24 @@
   simple_encode.EndEncode();
 }
 
-TEST(SimpleEncode, EncodeConsistencyTest) {
+// This test encodes the video using EncodeFrame(), where quantize indexes
+// are selected by vp9 rate control.
+// Encode stats and the quantize_indexes are collected.
+// Then the test encodes the video again using EncodeFrameWithQuantizeIndex()
+// using the quantize indexes collected from the first run.
+// Then test whether the encode stats of the two encoding runs match.
+TEST_F(SimpleEncodeTest, EncodeConsistencyTest) {
   std::vector<int> quantize_index_list;
   std::vector<uint64_t> ref_sse_list;
   std::vector<double> ref_psnr_list;
   std::vector<size_t> ref_bit_size_list;
+  std::vector<FrameType> ref_frame_type_list;
+  std::vector<int> ref_show_idx_list;
   {
     // The first encode.
-    SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                               target_bitrate, num_frames, infile_path);
+    SimpleEncode simple_encode(width_, height_, frame_rate_num_,
+                               frame_rate_den_, target_bitrate_, num_frames_,
+                               in_file_path_str_.c_str());
     simple_encode.ComputeFirstPassStats();
     const int num_coding_frames = simple_encode.GetCodingFrameNum();
     simple_encode.StartEncode();
@@ -169,13 +186,16 @@
       ref_sse_list.push_back(encode_frame_result.sse);
       ref_psnr_list.push_back(encode_frame_result.psnr);
       ref_bit_size_list.push_back(encode_frame_result.coding_data_bit_size);
+      ref_frame_type_list.push_back(encode_frame_result.frame_type);
+      ref_show_idx_list.push_back(encode_frame_result.show_idx);
     }
     simple_encode.EndEncode();
   }
   {
     // The second encode with quantize index got from the first encode.
-    SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                               target_bitrate, num_frames, infile_path);
+    SimpleEncode simple_encode(width_, height_, frame_rate_num_,
+                               frame_rate_den_, target_bitrate_, num_frames_,
+                               in_file_path_str_.c_str());
     simple_encode.ComputeFirstPassStats();
     const int num_coding_frames = simple_encode.GetCodingFrameNum();
     EXPECT_EQ(static_cast<size_t>(num_coding_frames),
@@ -189,6 +209,8 @@
       EXPECT_EQ(encode_frame_result.sse, ref_sse_list[i]);
       EXPECT_DOUBLE_EQ(encode_frame_result.psnr, ref_psnr_list[i]);
       EXPECT_EQ(encode_frame_result.coding_data_bit_size, ref_bit_size_list[i]);
+      EXPECT_EQ(encode_frame_result.frame_type, ref_frame_type_list[i]);
+      EXPECT_EQ(encode_frame_result.show_idx, ref_show_idx_list[i]);
     }
     simple_encode.EndEncode();
   }
@@ -196,13 +218,14 @@
 
 // Test the information (partition info and motion vector info) stored in
 // encoder is the same between two encode runs.
-TEST(SimpleEncode, EncodeConsistencyTest2) {
-  const int num_rows_4x4 = GetNumUnit4x4(w);
-  const int num_cols_4x4 = GetNumUnit4x4(h);
+TEST_F(SimpleEncodeTest, EncodeConsistencyTest2) {
+  const int num_rows_4x4 = GetNumUnit4x4(width_);
+  const int num_cols_4x4 = GetNumUnit4x4(height_);
   const int num_units_4x4 = num_rows_4x4 * num_cols_4x4;
   // The first encode.
-  SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                             target_bitrate, num_frames, infile_path);
+  SimpleEncode simple_encode(width_, height_, frame_rate_num_, frame_rate_den_,
+                             target_bitrate_, num_frames_,
+                             in_file_path_str_.c_str());
   simple_encode.ComputeFirstPassStats();
   const int num_coding_frames = simple_encode.GetCodingFrameNum();
   std::vector<PartitionInfo> partition_info_list(num_units_4x4 *
@@ -222,8 +245,9 @@
   }
   simple_encode.EndEncode();
   // The second encode.
-  SimpleEncode simple_encode_2(w, h, frame_rate_num, frame_rate_den,
-                               target_bitrate, num_frames, infile_path);
+  SimpleEncode simple_encode_2(width_, height_, frame_rate_num_,
+                               frame_rate_den_, target_bitrate_, num_frames_,
+                               in_file_path_str_.c_str());
   simple_encode_2.ComputeFirstPassStats();
   const int num_coding_frames_2 = simple_encode_2.GetCodingFrameNum();
   simple_encode_2.StartEncode();
@@ -264,14 +288,15 @@
 }
 
 // Test the information stored in encoder is the same between two encode runs.
-TEST(SimpleEncode, EncodeConsistencyTest3) {
+TEST_F(SimpleEncodeTest, EncodeConsistencyTest3) {
   std::vector<int> quantize_index_list;
-  const int num_rows_4x4 = GetNumUnit4x4(w);
-  const int num_cols_4x4 = GetNumUnit4x4(h);
+  const int num_rows_4x4 = GetNumUnit4x4(width_);
+  const int num_cols_4x4 = GetNumUnit4x4(height_);
   const int num_units_4x4 = num_rows_4x4 * num_cols_4x4;
   // The first encode.
-  SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                             target_bitrate, num_frames, infile_path);
+  SimpleEncode simple_encode(width_, height_, frame_rate_num_, frame_rate_den_,
+                             target_bitrate_, num_frames_,
+                             in_file_path_str_.c_str());
   simple_encode.ComputeFirstPassStats();
   const int num_coding_frames = simple_encode.GetCodingFrameNum();
   std::vector<PartitionInfo> partition_info_list(num_units_4x4 *
@@ -288,8 +313,9 @@
   }
   simple_encode.EndEncode();
   // The second encode.
-  SimpleEncode simple_encode_2(w, h, frame_rate_num, frame_rate_den,
-                               target_bitrate, num_frames, infile_path);
+  SimpleEncode simple_encode_2(width_, height_, frame_rate_num_,
+                               frame_rate_den_, target_bitrate_, num_frames_,
+                               in_file_path_str_.c_str());
   simple_encode_2.ComputeFirstPassStats();
   const int num_coding_frames_2 = simple_encode_2.GetCodingFrameNum();
   simple_encode_2.StartEncode();
@@ -319,21 +345,22 @@
 // Get QPs and arf locations from the first encode.
 // Set external arfs and QPs for the second encode.
 // Expect to get matched results.
-TEST(SimpleEncode, EncodeConsistencySetExternalGroupOfPicturesMap) {
+TEST_F(SimpleEncodeTest, EncodeConsistencySetExternalGroupOfPicturesMap) {
   std::vector<int> quantize_index_list;
   std::vector<uint64_t> ref_sse_list;
   std::vector<double> ref_psnr_list;
   std::vector<size_t> ref_bit_size_list;
-  std::vector<int> gop_map(num_frames, 0);
+  std::vector<int> gop_map(num_frames_, 0);
   {
     // The first encode.
-    SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                               target_bitrate, num_frames, infile_path);
+    SimpleEncode simple_encode(width_, height_, frame_rate_num_,
+                               frame_rate_den_, target_bitrate_, num_frames_,
+                               in_file_path_str_.c_str());
     simple_encode.ComputeFirstPassStats();
     simple_encode.StartEncode();
 
     int coded_show_frame_count = 0;
-    while (coded_show_frame_count < num_frames) {
+    while (coded_show_frame_count < num_frames_) {
       const GroupOfPicture group_of_picture =
           simple_encode.ObserveGroupOfPicture();
       gop_map[coded_show_frame_count] |= kGopMapFlagStart;
@@ -358,8 +385,9 @@
   {
     // The second encode with quantize index got from the first encode.
     // The external arfs are the same as the first encode.
-    SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                               target_bitrate, num_frames, infile_path);
+    SimpleEncode simple_encode(width_, height_, frame_rate_num_,
+                               frame_rate_den_, target_bitrate_, num_frames_,
+                               in_file_path_str_.c_str());
     simple_encode.ComputeFirstPassStats();
     simple_encode.SetExternalGroupOfPicturesMap(gop_map);
     const int num_coding_frames = simple_encode.GetCodingFrameNum();
@@ -379,12 +407,13 @@
   }
 }
 
-TEST(SimpleEncode, SetExternalGroupOfPicturesMap) {
-  SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                             target_bitrate, num_frames, infile_path);
+TEST_F(SimpleEncodeTest, SetExternalGroupOfPicturesMap) {
+  SimpleEncode simple_encode(width_, height_, frame_rate_num_, frame_rate_den_,
+                             target_bitrate_, num_frames_,
+                             in_file_path_str_.c_str());
   simple_encode.ComputeFirstPassStats();
 
-  std::vector<int> gop_map(num_frames, 0);
+  std::vector<int> gop_map(num_frames_, 0);
 
   // Should be the first gop group.
   gop_map[0] = 0;
@@ -425,7 +454,7 @@
 
   simple_encode.StartEncode();
   int coded_show_frame_count = 0;
-  while (coded_show_frame_count < num_frames) {
+  while (coded_show_frame_count < num_frames_) {
     const GroupOfPicture group_of_picture =
         simple_encode.ObserveGroupOfPicture();
     const std::vector<EncodeFrameInfo> &encode_frame_list =
@@ -446,11 +475,12 @@
   simple_encode.EndEncode();
 }
 
-TEST(SimpleEncode, GetEncodeFrameInfo) {
+TEST_F(SimpleEncodeTest, GetEncodeFrameInfo) {
   // Makes sure that the encode_frame_info obtained from GetEncodeFrameInfo()
   // matches the counterpart in encode_frame_result obtained from EncodeFrame()
-  SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                             target_bitrate, num_frames, infile_path);
+  SimpleEncode simple_encode(width_, height_, frame_rate_num_, frame_rate_den_,
+                             target_bitrate_, num_frames_,
+                             in_file_path_str_.c_str());
   simple_encode.ComputeFirstPassStats();
   const int num_coding_frames = simple_encode.GetCodingFrameNum();
   simple_encode.StartEncode();
@@ -464,11 +494,12 @@
   simple_encode.EndEncode();
 }
 
-TEST(SimpleEncode, GetFramePixelCount) {
-  SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
-                             target_bitrate, num_frames, infile_path);
+TEST_F(SimpleEncodeTest, GetFramePixelCount) {
+  SimpleEncode simple_encode(width_, height_, frame_rate_num_, frame_rate_den_,
+                             target_bitrate_, num_frames_,
+                             in_file_path_str_.c_str());
   EXPECT_EQ(simple_encode.GetFramePixelCount(),
-            static_cast<uint64_t>(w * h * 3 / 2));
+            static_cast<uint64_t>(width_ * height_ * 3 / 2));
 }
 
 }  // namespace