From: angiebird <angiebird@google.com>
Date: Wed, 7 Oct 2020 22:29:51 +0000 (-0700)
Subject: Add vp9_extrc_init/create/delete
X-Git-Tag: v1.10.0-rc1~57
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=20bca1350a16af4c3346cfbf7f8f6c181e21a9ca;p=libvpx

Add vp9_extrc_init/create/delete

Change-Id: I9fcb9f4cc5c565794229593fadde87286fcf0ffd
---

diff --git a/vp9/encoder/vp9_encoder.c b/vp9/encoder/vp9_encoder.c
index e513cef84..80bc4355b 100644
--- a/vp9/encoder/vp9_encoder.c
+++ b/vp9/encoder/vp9_encoder.c
@@ -2664,6 +2664,7 @@ VP9_COMP *vp9_create_compressor(const VP9EncoderConfig *oxcf,
   motion_vector_info_init(cpi);
   fp_motion_vector_info_init(cpi);
 #endif
+  vp9_extrc_init(&cpi->ext_ratectrl);
 
   return cpi;
 }
@@ -2834,6 +2835,8 @@ void vp9_remove_compressor(VP9_COMP *cpi) {
   }
 #endif
 
+  vp9_extrc_delete(&cpi->ext_ratectrl);
+
   vp9_remove_common(cm);
   vp9_free_ref_frame_buffers(cm->buffer_pool);
 #if CONFIG_VP9_POSTPROC
diff --git a/vp9/encoder/vp9_encoder.h b/vp9/encoder/vp9_encoder.h
index 28e00299b..b35d56608 100644
--- a/vp9/encoder/vp9_encoder.h
+++ b/vp9/encoder/vp9_encoder.h
@@ -39,6 +39,7 @@
 #include "vp9/encoder/vp9_context_tree.h"
 #include "vp9/encoder/vp9_encodemb.h"
 #include "vp9/encoder/vp9_ethread.h"
+#include "vp9/encoder/vp9_ext_ratectrl.h"
 #include "vp9/encoder/vp9_firstpass.h"
 #include "vp9/encoder/vp9_job_queue.h"
 #include "vp9/encoder/vp9_lookahead.h"
@@ -661,12 +662,6 @@ static INLINE int get_num_unit_4x4(int size) { return (size + 3) >> 2; }
 static INLINE int get_num_unit_16x16(int size) { return (size + 15) >> 4; }
 #endif  // CONFIG_RATE_CTRL
 
-typedef struct EXT_RATECTRL {
-  int ready;
-  vpx_rc_model_t model;
-  vpx_rc_funcs_t funcs;
-} EXT_RATECTRL;
-
 typedef struct VP9_COMP {
   FRAME_INFO frame_info;
   QUANTS quants;
diff --git a/vp9/encoder/vp9_ext_ratectrl.c b/vp9/encoder/vp9_ext_ratectrl.c
new file mode 100644
index 000000000..5a1263bd8
--- /dev/null
+++ b/vp9/encoder/vp9_ext_ratectrl.c
@@ -0,0 +1,32 @@
+/*
+ *  Copyright (c) 2020 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "vp9/encoder/vp9_ext_ratectrl.h"
+#include "vp9/common/vp9_common.h"
+
+void vp9_extrc_init(EXT_RATECTRL *ext_ratectrl) { vp9_zero(*ext_ratectrl); }
+
+void vp9_extrc_create(vpx_rc_funcs_t funcs, vpx_rc_config_t ratectrl_config,
+                      EXT_RATECTRL *ext_ratectrl) {
+  vp9_extrc_delete(ext_ratectrl);
+  ext_ratectrl->funcs = funcs;
+  ext_ratectrl->ratectrl_config = ratectrl_config;
+  ext_ratectrl->funcs.create_model(ext_ratectrl->funcs.priv,
+                                   &ext_ratectrl->ratectrl_config,
+                                   ext_ratectrl->model);
+  ext_ratectrl->ready = 1;
+}
+
+void vp9_extrc_delete(EXT_RATECTRL *ext_ratectrl) {
+  if (ext_ratectrl->ready) {
+    ext_ratectrl->funcs.delete_model(ext_ratectrl->model);
+  }
+  vp9_extrc_init(ext_ratectrl);
+}
diff --git a/vp9/encoder/vp9_ext_ratectrl.h b/vp9/encoder/vp9_ext_ratectrl.h
new file mode 100644
index 000000000..4505b0fd6
--- /dev/null
+++ b/vp9/encoder/vp9_ext_ratectrl.h
@@ -0,0 +1,30 @@
+/*
+ *  Copyright (c) 2020 The WebM project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VPX_VP9_ENCODER_VP9_EXT_RATECTRL_H_
+#define VPX_VP9_ENCODER_VP9_EXT_RATECTRL_H_
+
+#include "vpx/vpx_ext_ratectrl.h"
+
+typedef struct EXT_RATECTRL {
+  int ready;
+  vpx_rc_model_t model;
+  vpx_rc_funcs_t funcs;
+  vpx_rc_config_t ratectrl_config;
+} EXT_RATECTRL;
+
+void vp9_extrc_init(EXT_RATECTRL *ext_ratectrl);
+
+void vp9_extrc_create(vpx_rc_funcs_t funcs, vpx_rc_config_t ratectrl_config,
+                      EXT_RATECTRL *ext_ratectrl);
+
+void vp9_extrc_delete(EXT_RATECTRL *ext_ratectrl);
+
+#endif  // VPX_VP9_ENCODER_VP9_EXT_RATECTRL_H_
diff --git a/vp9/vp9_cx_iface.c b/vp9/vp9_cx_iface.c
index 42847edee..aa13fc9cf 100644
--- a/vp9/vp9_cx_iface.c
+++ b/vp9/vp9_cx_iface.c
@@ -1735,7 +1735,24 @@ static vpx_codec_err_t ctrl_set_disable_loopfilter(vpx_codec_alg_priv_t *ctx,
 
 static vpx_codec_err_t ctrl_set_external_rate_control(vpx_codec_alg_priv_t *ctx,
                                                       va_list args) {
-  ctx->cpi->ext_ratectrl.funcs = *CAST(VP9E_SET_EXTERNAL_RATE_CONTROL, args);
+  vpx_rc_funcs_t funcs = *CAST(VP9E_SET_EXTERNAL_RATE_CONTROL, args);
+  VP9_COMP *cpi = ctx->cpi;
+  EXT_RATECTRL *ext_ratectrl = &cpi->ext_ratectrl;
+  const VP9EncoderConfig *oxcf = &cpi->oxcf;
+  const FRAME_INFO *frame_info = &cpi->frame_info;
+  vpx_rc_config_t ratectrl_config;
+
+  ratectrl_config.frame_width = frame_info->frame_width;
+  ratectrl_config.frame_height = frame_info->frame_height;
+  ratectrl_config.show_frame_count = cpi->twopass.first_pass_info.num_frames;
+
+  // TODO(angiebird): Double check whether this is the proper way to set up
+  // target_bitrate and frame_rate.
+  ratectrl_config.target_bitrate_kbps = (int)(oxcf->target_bandwidth / 1000);
+  ratectrl_config.frame_rate_num = oxcf->g_timebase.den;
+  ratectrl_config.frame_rate_den = oxcf->g_timebase.num;
+
+  vp9_extrc_create(funcs, ratectrl_config, ext_ratectrl);
   return VPX_CODEC_OK;
 }
 
diff --git a/vp9/vp9cx.mk b/vp9/vp9cx.mk
index 666f22882..38e99165a 100644
--- a/vp9/vp9cx.mk
+++ b/vp9/vp9cx.mk
@@ -96,6 +96,8 @@ VP9_CX_SRCS-yes += encoder/vp9_skin_detection.c
 VP9_CX_SRCS-yes += encoder/vp9_skin_detection.h
 VP9_CX_SRCS-yes += encoder/vp9_noise_estimate.c
 VP9_CX_SRCS-yes += encoder/vp9_noise_estimate.h
+VP9_CX_SRCS-yes += encoder/vp9_ext_ratectrl.c
+VP9_CX_SRCS-yes += encoder/vp9_ext_ratectrl.h
 ifeq ($(CONFIG_VP9_POSTPROC),yes)
 VP9_CX_SRCS-$(CONFIG_INTERNAL_STATS) += common/vp9_postproc.h
 VP9_CX_SRCS-$(CONFIG_INTERNAL_STATS) += common/vp9_postproc.c
diff --git a/vpx/vpx_ext_ratectrl.h b/vpx/vpx_ext_ratectrl.h
index 08e8470dd..3b00e1181 100644
--- a/vpx/vpx_ext_ratectrl.h
+++ b/vpx/vpx_ext_ratectrl.h
@@ -45,6 +45,8 @@ typedef struct vpx_rc_config {
   int frame_height;
   int show_frame_count;
   int target_bitrate_kbps;
+  int frame_rate_num;
+  int frame_rate_den;
 } vpx_rc_config_t;
 
 /*!\brief Create an external rate control model callback prototype