shithub: libvpx

Download patch

ref: 1b07aae9e47a5480f78025dc3434331faaa68e33
parent: 1c792f2991e632be26414ecb62835bf148c086b4
author: Cheng Chen <chengchen@google.com>
date: Tue Apr 13 07:59:40 EDT 2021

Set vizier rc parameters

If pass --use-vizier-rc-params=1, the rc parameters are overwittern
by pass in values. It --use-vizier-rc-params=0, the rc parameters
remain the default values.

Change-Id: I7a3e806e0918f49e8970997379a6e99af6bb7cac

--- a/vp8/vp8_cx_iface.c
+++ b/vp8/vp8_cx_iface.c
@@ -258,6 +258,7 @@
 #endif
 
   // The range below shall be further tuned.
+  RANGE_CHECK(cfg, use_vizier_rc_params, 0, 1);
   RANGE_CHECK(cfg, active_wq_factor.den, 1, 1000);
   RANGE_CHECK(cfg, base_err_per_mb.den, 1, 1000);
   RANGE_CHECK(cfg, sr_default_decay_limit.den, 1, 1000);
@@ -1302,6 +1303,7 @@
         { 0 },    /* ts_layer_id */
         { 0 },    /* layer_target_bitrate */
         0,        /* temporal_layering_mode */
+        0,        /* use_vizier_rc_params */
         { 0, 1 }, /* active_wq_factor */
         { 0, 1 }, /* base_err_per_mb */
         { 0, 1 }, /* sr_default_decay_limit */
--- a/vp9/encoder/vp9_firstpass.c
+++ b/vp9/encoder/vp9_firstpass.c
@@ -3493,109 +3493,32 @@
 // Configure image size specific vizier parameters.
 // Later these will be set via additional command line options
 static void init_vizier_params(TWO_PASS *const twopass, int screen_area) {
-  if (1) {
-    // Force defaults for now
-    twopass->active_wq_factor = AV_WQ_FACTOR;
-    twopass->base_err_per_mb = BASELINE_ERR_PER_MB;
-    twopass->sr_default_decay_limit = DEFAULT_DECAY_LIMIT;
-    twopass->sr_diff_factor = 1.0;
-    twopass->gf_frame_max_boost = GF_MAX_FRAME_BOOST;
-    twopass->gf_max_total_boost = MAX_GF_BOOST;
-    if (screen_area < 1280 * 720) {
-      twopass->kf_err_per_mb = 2000.0;
-    } else if (screen_area < 1920 * 1080) {
-      twopass->kf_err_per_mb = 500.0;
-    } else {
-      twopass->kf_err_per_mb = 250.0;
-    }
-    twopass->kf_frame_min_boost = KF_MIN_FRAME_BOOST;
-    twopass->kf_frame_max_boost_first = KF_MAX_FRAME_BOOST;
-    twopass->kf_frame_max_boost_subs = twopass->kf_frame_max_boost_first;
-    twopass->kf_max_total_boost = MAX_KF_TOT_BOOST;
-    twopass->zm_factor = 1.0;
+  // When |use_vizier_rc_params| is 1, we expect the rc parameters have been
+  // initialized by the pass in values.
+  // Be careful that parameters below are only initialized to 0, if we do not
+  // pass values to them. It is desired to take care of each parameter when
+  // using |use_vizier_rc_params|.
+  if (twopass->use_vizier_rc_params) return;
+
+  // When |use_vizier_rc_params| is 0, use defaults for now.
+  twopass->active_wq_factor = AV_WQ_FACTOR;
+  twopass->base_err_per_mb = BASELINE_ERR_PER_MB;
+  twopass->sr_default_decay_limit = DEFAULT_DECAY_LIMIT;
+  twopass->sr_diff_factor = 1.0;
+  twopass->gf_frame_max_boost = GF_MAX_FRAME_BOOST;
+  twopass->gf_max_total_boost = MAX_GF_BOOST;
+  if (screen_area < 1280 * 720) {
+    twopass->kf_err_per_mb = 2000.0;
+  } else if (screen_area < 1920 * 1080) {
+    twopass->kf_err_per_mb = 500.0;
   } else {
-    // Vizer experimental parameters from training.
-    // Later these will be set via the command line.
-    if (screen_area <= 176 * 144) {
-      twopass->active_wq_factor = 46.0;
-      twopass->base_err_per_mb = 37597.399760969536;
-      twopass->sr_default_decay_limit = 0.3905639800962774;
-      twopass->sr_diff_factor = 6.4;
-      twopass->gf_frame_max_boost = 87.27362648627846;
-      twopass->gf_max_total_boost = MAX_GF_BOOST;
-      twopass->kf_err_per_mb = 1854.8255436877148;
-      twopass->kf_frame_min_boost = KF_MIN_FRAME_BOOST;
-      twopass->kf_frame_max_boost_first = 25.5;
-      twopass->kf_frame_max_boost_subs = twopass->kf_frame_max_boost_first;
-      twopass->kf_max_total_boost = MAX_KF_TOT_BOOST;
-      twopass->zm_factor = 1.0;
-    } else if (screen_area <= 320 * 240) {
-      twopass->active_wq_factor = 55.0;
-      twopass->base_err_per_mb = 34525.33177195309;
-      twopass->sr_default_decay_limit = 0.23901360046804604;
-      twopass->sr_diff_factor = 5.73;
-      twopass->gf_frame_max_boost = 127.34978204980285;
-      twopass->gf_max_total_boost = MAX_GF_BOOST;
-      twopass->kf_err_per_mb = 723.8337508755031;
-      twopass->kf_frame_min_boost = KF_MIN_FRAME_BOOST;
-      twopass->kf_frame_max_boost_first = 185.0;
-      twopass->kf_frame_max_boost_subs = twopass->kf_frame_max_boost_first;
-      twopass->kf_max_total_boost = MAX_KF_TOT_BOOST;
-      twopass->zm_factor = 1.0;
-    } else if (screen_area <= 640 * 360) {
-      twopass->active_wq_factor = 12.5;
-      twopass->base_err_per_mb = 18823.978018028298;
-      twopass->sr_default_decay_limit = 0.6043527690301296;
-      twopass->sr_diff_factor = 2.28;
-      twopass->gf_frame_max_boost = 75.17672317013668;
-      twopass->gf_max_total_boost = MAX_GF_BOOST;
-      twopass->kf_err_per_mb = 422.2871502380377;
-      twopass->kf_frame_min_boost = KF_MIN_FRAME_BOOST;
-      twopass->kf_frame_max_boost_first = 224.5;
-      twopass->kf_frame_max_boost_subs = twopass->kf_frame_max_boost_first;
-      twopass->kf_max_total_boost = MAX_KF_TOT_BOOST;
-      twopass->zm_factor = 1.0;
-    } else if (screen_area <= 854 * 480) {
-      twopass->active_wq_factor = 51.5;
-      twopass->base_err_per_mb = 33718.98307662595;
-      twopass->sr_default_decay_limit = 0.33633414970713393;
-      twopass->sr_diff_factor = 5.8;
-      twopass->gf_frame_max_boost = 85.2868528581522;
-      twopass->gf_max_total_boost = MAX_GF_BOOST;
-      twopass->kf_err_per_mb = 1513.4883914008383;
-      twopass->kf_frame_min_boost = KF_MIN_FRAME_BOOST;
-      twopass->kf_frame_max_boost_first = 28.0;
-      twopass->kf_frame_max_boost_subs = twopass->kf_frame_max_boost_first;
-      twopass->kf_max_total_boost = MAX_KF_TOT_BOOST;
-      twopass->zm_factor = 1.0;
-    } else if (screen_area <= 1280 * 720) {
-      twopass->active_wq_factor = 41.5;
-      twopass->base_err_per_mb = 29527.46375825401;
-      twopass->sr_default_decay_limit = 0.5009117586299728;
-      twopass->sr_diff_factor = 3.33;
-      twopass->gf_frame_max_boost = 81.00472969483079;
-      twopass->gf_max_total_boost = MAX_GF_BOOST;
-      twopass->kf_err_per_mb = 998.6342911785146;
-      twopass->kf_frame_min_boost = KF_MIN_FRAME_BOOST;
-      twopass->kf_frame_max_boost_first = 53.0;
-      twopass->kf_frame_max_boost_subs = twopass->kf_frame_max_boost_first;
-      twopass->kf_max_total_boost = MAX_KF_TOT_BOOST;
-      twopass->zm_factor = 1.0;
-    } else {
-      twopass->active_wq_factor = 31.0;
-      twopass->base_err_per_mb = 34474.723463367416;
-      twopass->sr_default_decay_limit = 0.23346886902707745;
-      twopass->sr_diff_factor = 7.6;
-      twopass->gf_frame_max_boost = 213.2940230360479;
-      twopass->gf_max_total_boost = MAX_GF_BOOST;
-      twopass->kf_err_per_mb = 35931.25734431429;
-      twopass->kf_frame_min_boost = KF_MIN_FRAME_BOOST;
-      twopass->kf_frame_max_boost_first = 419.5;
-      twopass->kf_frame_max_boost_subs = twopass->kf_frame_max_boost_first;
-      twopass->kf_max_total_boost = MAX_KF_TOT_BOOST;
-      twopass->zm_factor = 1.0;
-    }
+    twopass->kf_err_per_mb = 250.0;
   }
+  twopass->kf_frame_min_boost = KF_MIN_FRAME_BOOST;
+  twopass->kf_frame_max_boost_first = KF_MAX_FRAME_BOOST;
+  twopass->kf_frame_max_boost_subs = twopass->kf_frame_max_boost_first;
+  twopass->kf_max_total_boost = MAX_KF_TOT_BOOST;
+  twopass->zm_factor = 1.0;
 }
 
 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
--- a/vp9/encoder/vp9_firstpass.h
+++ b/vp9/encoder/vp9_firstpass.h
@@ -223,6 +223,10 @@
   GF_GROUP gf_group;
 
   // Vizeir project experimental two pass rate control parameters.
+  // When |use_vizier_rc_params| is 1, the following parameters will
+  // be overwritten by pass in values. Otherwise, they are initialized
+  // by default values.
+  int use_vizier_rc_params;
   double active_wq_factor;
   double base_err_per_mb;
   double sr_default_decay_limit;
--- a/vp9/vp9_cx_iface.c
+++ b/vp9/vp9_cx_iface.c
@@ -350,6 +350,7 @@
   RANGE_CHECK(extra_cfg, color_range, VPX_CR_STUDIO_RANGE, VPX_CR_FULL_RANGE);
 
   // The range below shall be further tuned.
+  RANGE_CHECK(cfg, use_vizier_rc_params, 0, 1);
   RANGE_CHECK(cfg, active_wq_factor.den, 1, 1000);
   RANGE_CHECK(cfg, base_err_per_mb.den, 1, 1000);
   RANGE_CHECK(cfg, sr_default_decay_limit.den, 1, 1000);
@@ -654,8 +655,10 @@
 
 static vpx_codec_err_t set_twopass_params_from_config(
     const vpx_codec_enc_cfg_t *const cfg, struct VP9_COMP *cpi) {
+  if (!cfg->use_vizier_rc_params) return VPX_CODEC_OK;
   if (cpi == NULL) return VPX_CODEC_ERROR;
 
+  cpi->twopass.use_vizier_rc_params = cfg->use_vizier_rc_params;
   cpi->twopass.active_wq_factor =
       (double)cfg->active_wq_factor.num / (double)cfg->active_wq_factor.den;
   cpi->twopass.base_err_per_mb =
@@ -1943,6 +1946,7 @@
         { 0 },     // ts_layer_id
         { 0 },     // layer_taget_bitrate
         0,         // temporal_layering_mode
+        0,         // use_vizier_rc_params
         { 0, 1 },  // active_wq_factor
         { 0, 1 },  // base_err_per_mb
         { 0, 1 },  // sr_default_decay_limit
--- a/vpx/vpx_encoder.h
+++ b/vpx/vpx_encoder.h
@@ -694,9 +694,17 @@
    */
   int temporal_layering_mode;
 
+  /*!\brief A flag indicating whether to use external rate control parameters.
+   * By default is 0. If set to 1, the following parameters will be used in the
+   * rate control system.
+   */
+  int use_vizier_rc_params;
+
   /*!\brief Active worst quality factor.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t active_wq_factor;
@@ -704,6 +712,8 @@
   /*!\brief Base error per macroblock.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t base_err_per_mb;
@@ -711,6 +721,8 @@
   /*!\brief Second reference default decay limit.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t sr_default_decay_limit;
@@ -718,6 +730,8 @@
   /*!\brief Second reference difference factor.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t sr_diff_factor;
@@ -725,6 +739,8 @@
   /*!\brief Keyframe error per macroblock.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t kf_err_per_mb;
@@ -732,6 +748,8 @@
   /*!\brief Keyframe minimum boost.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t kf_frame_min_boost;
@@ -739,6 +757,8 @@
   /*!\brief Keyframe maximum boost, for the first keyframe in a chunk.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t kf_frame_max_boost_first;
@@ -746,6 +766,8 @@
   /*!\brief Keyframe maximum boost, for subsequent keyframes.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t kf_frame_max_boost_subs;
@@ -753,6 +775,8 @@
   /*!\brief Keyframe maximum total boost.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t kf_max_total_boost;
@@ -760,6 +784,8 @@
   /*!\brief Golden frame maximum total boost.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t gf_max_total_boost;
@@ -767,6 +793,8 @@
   /*!\brief Golden frame maximum boost.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t gf_frame_max_boost;
@@ -774,6 +802,8 @@
   /*!\brief Zero motion power factor.
    *
    * Rate control parameters, set from external experiment results.
+   * Only when |use_vizier_rc_params| is set to 1, the pass in value will be
+   * used. Otherwise, the default value is used.
    *
    */
   vpx_rational_t zm_factor;
--- a/vpxenc.c
+++ b/vpxenc.c
@@ -288,6 +288,8 @@
 };
 
 #if CONFIG_VP9_ENCODER
+static const arg_def_t use_vizier_rc_params =
+    ARG_DEF(NULL, "use-vizier-rc-params", 1, "Use vizier rc params");
 static const arg_def_t active_wq_factor =
     ARG_DEF(NULL, "active-wq-factor", 1, "Active worst quality factor");
 static const arg_def_t base_err_per_mb =
@@ -311,8 +313,9 @@
 static const arg_def_t gf_frame_max_boost =
     ARG_DEF(NULL, "gf-frame-max-boost", 1, "Golden frame max boost");
 static const arg_def_t zm_factor =
-    ARG_DEF(NULL, "zm-power-factor", 1, "Zero motion power factor");
-static const arg_def_t *vizier_rc_args[] = { &active_wq_factor,
+    ARG_DEF(NULL, "zm-factor", 1, "Zero motion power factor");
+static const arg_def_t *vizier_rc_args[] = { &use_vizier_rc_params,
+                                             &active_wq_factor,
                                              &base_err_per_mb,
                                              &sr_default_decay_limit,
                                              &sr_diff_factor,
@@ -1026,6 +1029,8 @@
     } else if (arg_match(&arg, &kf_disabled, argi)) {
       config->cfg.kf_mode = VPX_KF_DISABLED;
 #if CONFIG_VP9_ENCODER
+    } else if (arg_match(&arg, &use_vizier_rc_params, argi)) {
+      config->cfg.use_vizier_rc_params = arg_parse_int(&arg);
     } else if (arg_match(&arg, &active_wq_factor, argi)) {
       config->cfg.active_wq_factor = arg_parse_rational(&arg);
     } else if (arg_match(&arg, &base_err_per_mb, argi)) {
@@ -1246,6 +1251,7 @@
   SHOW(kf_min_dist);
   SHOW(kf_max_dist);
   // Temporary use for debug
+  SHOW(use_vizier_rc_params);
   SHOW(active_wq_factor.num);
   SHOW(active_wq_factor.den);
 }