shithub: libvpx

Download patch

ref: 6a950fee925c341352b6b0d2aef87293ac66f69c
parent: 21e93eb8140c832ce3bc5f6d40ba6eec507886ee
author: angiebird <[email protected]>
date: Tue Nov 12 13:57:39 EST 2019

Add coding_data_bit_size to EncodeFrameResult

Change-Id: I32ab6829083c896ab2c6234e191939a000dea6e5

--- a/test/simple_encode_test.cc
+++ b/test/simple_encode_test.cc
@@ -1,3 +1,4 @@
+#include <math.h>
 #include <memory>
 #include <vector>
 #include "third_party/googletest/src/include/gtest/gtest.h"
@@ -13,6 +14,12 @@
 // TODO(angiebird): Figure out how to upload test video to our codebase
 const char infile_path[] = "bus_352x288_420_f20_b8.yuv";
 
+static double get_bit_rate_in_kpbs(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 /
+         frame_rate_den / 1000.;
+}
+
 TEST(SimpleEncode, ComputeFirstPassStats) {
   SimpleEncode simple_encode(w, h, frame_rate_num, frame_rate_den,
                              target_bitrate, num_frames, infile_path);
@@ -52,6 +59,7 @@
   int ref_num_alternate_refereces = num_coding_frames - num_frames;
   int num_alternate_refereces = 0;
   simple_encode.StartEncode();
+  size_t total_data_bit_size = 0;
   for (int i = 0; i < num_coding_frames; ++i) {
     EncodeFrameResult encode_frame_result;
     if (i == 0) {
@@ -72,8 +80,12 @@
     EXPECT_GE(encode_frame_result.psnr, 34)
         << "The psnr is supposed to be greater than 34 given the "
            "target_bitrate 1000 kbps";
+    total_data_bit_size += encode_frame_result.coding_data_bit_size;
   }
   EXPECT_EQ(num_alternate_refereces, ref_num_alternate_refereces);
+  double bitrate = get_bit_rate_in_kpbs(total_data_bit_size, num_frames,
+                                        frame_rate_num, frame_rate_den);
+  EXPECT_LE(fabs(target_bitrate - bitrate), 150);
   simple_encode.EndEncode();
 }
 
--- a/vp9/simple_encode.cc
+++ b/vp9/simple_encode.cc
@@ -87,6 +87,8 @@
 static void update_encode_frame_result(
     EncodeFrameResult *encode_frame_result,
     const ENCODE_FRAME_RESULT *encode_frame_info) {
+  encode_frame_result->coding_data_bit_size =
+      encode_frame_result->coding_data_byte_size * 8;
   encode_frame_result->show_idx = encode_frame_info->show_idx;
   encode_frame_result->frame_type =
       get_frame_type_from_update_type(encode_frame_info->update_type);
@@ -236,9 +238,9 @@
     }
   }
   assert(encode_frame_result->coding_data.get() == nullptr);
-  const size_t max_coding_data_size = frame_width * frame_height * 3;
-  encode_frame_result->coding_data =
-      std::move(std::unique_ptr<uint8_t[]>(new uint8_t[max_coding_data_size]));
+  const size_t max_coding_data_byte_size = frame_width * frame_height * 3;
+  encode_frame_result->coding_data = std::move(
+      std::unique_ptr<uint8_t[]>(new uint8_t[max_coding_data_byte_size]));
   int64_t time_stamp;
   int64_t time_end;
   int flush = 1;  // Make vp9_get_compressed_data encode a frame
@@ -245,13 +247,14 @@
   unsigned int frame_flags = 0;
   ENCODE_FRAME_RESULT encode_frame_info;
   vp9_get_compressed_data(cpi, &frame_flags,
-                          &encode_frame_result->coding_data_size,
+                          &encode_frame_result->coding_data_byte_size,
                           encode_frame_result->coding_data.get(), &time_stamp,
                           &time_end, flush, &encode_frame_info);
   // vp9_get_compressed_data is expected to encode a frame every time, so the
   // data size should be greater than zero.
-  assert(encode_frame_result->coding_data_size > 0);
-  assert(encode_frame_result->coding_data_size < max_coding_data_size);
+  assert(encode_frame_result->coding_data_byte_size > 0);
+  assert(encode_frame_result->coding_data_byte_size <
+         max_coding_data_byte_size);
 
   update_encode_frame_result(encode_frame_result, &encode_frame_info);
 }
--- a/vp9/simple_encode.h
+++ b/vp9/simple_encode.h
@@ -10,7 +10,8 @@
 struct EncodeFrameResult {
   int show_idx;
   FrameType frame_type;
-  size_t coding_data_size;
+  size_t coding_data_bit_size;
+  size_t coding_data_byte_size;
   // The EncodeFrame will allocate a buffer, write the coding data into the
   // buffer and give the ownership of the buffer to coding_data
   std::unique_ptr<unsigned char[]> coding_data;