shithub: libvpx

Download patch

ref: 20bca1350a16af4c3346cfbf7f8f6c181e21a9ca
parent: 6dba0d0a058c42ace95aee8048eb043c890531e8
author: angiebird <angiebird@google.com>
date: Wed Oct 7 11:29:51 EDT 2020

Add vp9_extrc_init/create/delete

Change-Id: I9fcb9f4cc5c565794229593fadde87286fcf0ffd

--- a/vp9/encoder/vp9_encoder.c
+++ b/vp9/encoder/vp9_encoder.c
@@ -2664,6 +2664,7 @@
   motion_vector_info_init(cpi);
   fp_motion_vector_info_init(cpi);
 #endif
+  vp9_extrc_init(&cpi->ext_ratectrl);
 
   return cpi;
 }
@@ -2833,6 +2834,8 @@
     cpi->twopass.frame_mb_stats_buf = NULL;
   }
 #endif
+
+  vp9_extrc_delete(&cpi->ext_ratectrl);
 
   vp9_remove_common(cm);
   vp9_free_ref_frame_buffers(cm->buffer_pool);
--- 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"
@@ -660,12 +661,6 @@
 // round it up. For example, size is 17, return 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;
--- /dev/null
+++ b/vp9/encoder/vp9_ext_ratectrl.c
@@ -1,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);
+}
--- /dev/null
+++ b/vp9/encoder/vp9_ext_ratectrl.h
@@ -1,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_
--- a/vp9/vp9_cx_iface.c
+++ b/vp9/vp9_cx_iface.c
@@ -1735,7 +1735,24 @@
 
 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;
 }
 
--- a/vp9/vp9cx.mk
+++ b/vp9/vp9cx.mk
@@ -96,6 +96,8 @@
 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
--- a/vpx/vpx_ext_ratectrl.h
+++ b/vpx/vpx_ext_ratectrl.h
@@ -45,6 +45,8 @@
   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