]> granicus.if.org Git - libvpx/commitdiff
Add SetEncodeSpeed() to SimpleEncode
authorangiebird <angiebird@google.com>
Mon, 15 Jun 2020 22:09:01 +0000 (15:09 -0700)
committerangiebird <angiebird@google.com>
Wed, 15 Jul 2020 21:53:01 +0000 (14:53 -0700)
Change-Id: I2fcf37045a96bb101de3359e2e69dcc266c1dc10

vp9/simple_encode.cc
vp9/simple_encode.h
vp9/vp9_cx_iface.c
vp9/vp9_cx_iface.h

index 7bce91f7f0e3a0bfcd1aea5e36146a32336e503f..1d7214ce586f7838fa73a0dbe9e5ddd5e61e7489 100644 (file)
@@ -706,6 +706,7 @@ SimpleEncode::SimpleEncode(int frame_width, int frame_height,
   frame_rate_den_ = frame_rate_den;
   target_bitrate_ = target_bitrate;
   num_frames_ = num_frames;
+  encode_speed_ = 0;
 
   frame_coding_index_ = 0;
   show_frame_count_ = 0;
@@ -727,12 +728,16 @@ SimpleEncode::SimpleEncode(int frame_width, int frame_height,
   InitRefFrameInfo(&ref_frame_info_);
 }
 
+void SimpleEncode::SetEncodeSpeed(int encode_speed) {
+  encode_speed_ = encode_speed;
+}
+
 void SimpleEncode::ComputeFirstPassStats() {
   vpx_rational_t frame_rate =
       make_vpx_rational(frame_rate_num_, frame_rate_den_);
   const VP9EncoderConfig oxcf =
       vp9_get_encoder_config(frame_width_, frame_height_, frame_rate,
-                             target_bitrate_, VPX_RC_FIRST_PASS);
+                             target_bitrate_, encode_speed_, VPX_RC_FIRST_PASS);
   VP9_COMP *cpi = init_encoder(&oxcf, impl_ptr_->img_fmt);
   struct lookahead_ctx *lookahead = cpi->lookahead;
   int i;
@@ -881,7 +886,7 @@ void SimpleEncode::StartEncode() {
       make_vpx_rational(frame_rate_num_, frame_rate_den_);
   VP9EncoderConfig oxcf =
       vp9_get_encoder_config(frame_width_, frame_height_, frame_rate,
-                             target_bitrate_, VPX_RC_LAST_PASS);
+                             target_bitrate_, encode_speed_, VPX_RC_LAST_PASS);
   vpx_fixed_buf_t stats;
   stats.buf = GetVectorData(impl_ptr_->first_pass_stats);
   stats.sz = sizeof(impl_ptr_->first_pass_stats[0]) *
@@ -1091,7 +1096,7 @@ int SimpleEncode::GetCodingFrameNum() const {
       make_vpx_rational(frame_rate_num_, frame_rate_den_);
   const VP9EncoderConfig oxcf =
       vp9_get_encoder_config(frame_width_, frame_height_, frame_rate,
-                             target_bitrate_, VPX_RC_LAST_PASS);
+                             target_bitrate_, encode_speed_, VPX_RC_LAST_PASS);
   FRAME_INFO frame_info = vp9_get_frame_info(&oxcf);
   FIRST_PASS_INFO first_pass_info;
   fps_init_first_pass_info(&first_pass_info,
@@ -1108,7 +1113,7 @@ std::vector<int> SimpleEncode::ComputeKeyFrameMap() const {
       make_vpx_rational(frame_rate_num_, frame_rate_den_);
   const VP9EncoderConfig oxcf =
       vp9_get_encoder_config(frame_width_, frame_height_, frame_rate,
-                             target_bitrate_, VPX_RC_LAST_PASS);
+                             target_bitrate_, encode_speed_, VPX_RC_LAST_PASS);
   FRAME_INFO frame_info = vp9_get_frame_info(&oxcf);
   FIRST_PASS_INFO first_pass_info;
   fps_init_first_pass_info(&first_pass_info,
index b21732070164a961e5d3ac02f44a6ad64bfb303e..77197e7a239068a59462ce3c102ce9f6c242ecf6 100644 (file)
@@ -304,6 +304,15 @@ class SimpleEncode {
   SimpleEncode(SimpleEncode &) = delete;
   SimpleEncode &operator=(const SimpleEncode &) = delete;
 
+  // Adjusts the encoder's coding speed.
+  // If this function is not called, the encoder will use default encode_speed
+  // 0. Call this function before ComputeFirstPassStats() if needed.
+  // The encode_speed is equivalent to --cpu-used of the vpxenc command.
+  // The encode_speed's range should be [0, 9].
+  // Setting the encode_speed to a higher level will yield faster coding
+  // at the cost of lower compression efficiency.
+  void SetEncodeSpeed(int encode_speed);
+
   // Makes encoder compute the first pass stats and store it at
   // impl_ptr_->first_pass_stats. key_frame_map_ is also computed based on the
   // first pass stats.
@@ -405,6 +414,7 @@ class SimpleEncode {
   int frame_rate_den_;
   int target_bitrate_;
   int num_frames_;
+  int encode_speed_;
 
   std::FILE *in_file_;
   std::FILE *out_file_;
index 9074e1b4ecde2852e1be11c7f236660e6f51b209..6caa4f7390c6d03ff6e86c6c8db7cbfc95c8cf90 100644 (file)
@@ -1899,7 +1899,7 @@ static vp9_extracfg get_extra_cfg() {
 
 VP9EncoderConfig vp9_get_encoder_config(int frame_width, int frame_height,
                                         vpx_rational_t frame_rate,
-                                        int target_bitrate,
+                                        int target_bitrate, int encode_speed,
                                         vpx_enc_pass enc_pass) {
   /* This function will generate the same VP9EncoderConfig used by the
    * vpxenc command given below.
@@ -1910,6 +1910,7 @@ VP9EncoderConfig vp9_get_encoder_config(int frame_width, int frame_height,
    * HEIGHT:  frame_height
    * FPS:     frame_rate
    * BITRATE: target_bitrate
+   * CPU_USED:encode_speed
    *
    * INPUT, OUTPUT, LIMIT will not affect VP9EncoderConfig
    *
@@ -1921,9 +1922,10 @@ VP9EncoderConfig vp9_get_encoder_config(int frame_width, int frame_height,
    * BITRATE=600
    * FPS=30/1
    * LIMIT=150
+   * CPU_USED=0
    * ./vpxenc --limit=$LIMIT --width=$WIDTH --height=$HEIGHT --fps=$FPS
    * --lag-in-frames=25 \
-   *  --codec=vp9 --good --cpu-used=0 --threads=0 --profile=0 \
+   *  --codec=vp9 --good --cpu-used=CPU_USED --threads=0 --profile=0 \
    *  --min-q=0 --max-q=63 --auto-alt-ref=1 --passes=2 --kf-max-dist=150 \
    *  --kf-min-dist=0 --drop-frame=0 --static-thresh=0 --bias-pct=50 \
    *  --minsection-pct=0 --maxsection-pct=150 --arnr-maxframes=7 --psnr \
@@ -1946,6 +1948,7 @@ VP9EncoderConfig vp9_get_encoder_config(int frame_width, int frame_height,
   oxcf.tile_columns = 0;
   oxcf.frame_parallel_decoding_mode = 0;
   oxcf.two_pass_vbrmax_section = 150;
+  oxcf.speed = abs(encode_speed);
   return oxcf;
 }
 
index 08569fcc96ce240b7e05f3ae10a1dc231226ccde..3188575dd686d29ee28c451aabe40f71043552a8 100644 (file)
@@ -19,7 +19,7 @@ extern "C" {
 
 VP9EncoderConfig vp9_get_encoder_config(int frame_width, int frame_height,
                                         vpx_rational_t frame_rate,
-                                        int target_bitrate,
+                                        int target_bitrate, int encode_speed,
                                         vpx_enc_pass enc_pass);
 
 void vp9_dump_encoder_config(const VP9EncoderConfig *oxcf);