Convert Vizier RD parameters to normalized factors
authorPaul Wilkins <paulwilkins@google.com>
Mon, 22 Mar 2021 19:45:10 +0000 (19:45 +0000)
committerPaul Wilkins <paulwilkins@google.com>
Tue, 23 Mar 2021 15:05:32 +0000 (15:05 +0000)
This patch converts the Vizier custom RD multipliers, to factors
that adjust each RD multiplier either side of its default value, where
a factor of 1.0 will give the previous default  behavior.

Ultimately I would like to replace the multiple RD multipliers
triggered at different Q thresholds (eg, low, medium, high q)
with a function that adjusts the rd behavior smoothly as Q
changes.

Vizier could then be presented with a single adjustment control
for each of key frame and inter frame rd.

The current behavior is problematic.

Firstly having hard threshold Q values at which rd behavior changes
may cause anomalies in the rate distortion curve, where in some
situations, raising  Q, for example,  may not cause the expected drop
in rate and rise in distortion, because we have crossed a threshold
where the rate distortion multiplier changes sharply and this alters
the balance of bits spent in the prediction and residual parts of the
signal.

Having a single  value that is used for a range of Q index values
(eg 0-64), (65-128)  may also cause problems and over-fitting in
the context of the Vizier ML project. This project tries to optimize
the values for each Q range, for various YT formats, but does so
by analyzing the results of single point encodes on a set of clips.
For a given format all the clips are encoded with the same parameters
(target rate etc) so  there is likely to be clustering in regards to the
Q values used. For example the training set may give a new value
for the Q  range 0-64 but most of the data points used may have Q
close 64.

It will likely require several iterations working with the Vizier team
to get this right. This patch just gives an initial framework for
testing.

Change-Id: Iaa4cd5561b95a202bcae7a1d876c4f40ef444fa2

vp9/encoder/vp9_rd.c
vp9/encoder/vp9_rd.h

index d5d668f..d3bf3d6 100644 (file)
@@ -205,65 +205,74 @@ void vp9_init_rd_parameters(VP9_COMP *cpi) {
   // Make sure this function is floating point safe.
   vpx_clear_system_state();
 
-  rdc->rd_mult_q_sq_key_high_qp = 7.5;  // No defined Vizer values yet
+  rdc->rd_mult_key_high_qp_fac = 1.0;  // Default: no Vizer values yet
 
   if (0) {
     unsigned int screen_area = (cpi->common.width * cpi->common.height);
 
     if (screen_area <= 176 * 144) {
-      rdc->rd_mult_q_sq_inter_low_qp = 4.0718581295922025;
-      rdc->rd_mult_q_sq_inter_mid_qp = 4.031435609256739;
-      rdc->rd_mult_q_sq_inter_high_qp = 4.295745965132044;
-      rdc->rd_mult_q_sq_key_ultralow_qp = 4.290774097327333;
-      rdc->rd_mult_q_sq_key_low_qp = 5.7037775720838155;
-      rdc->rd_mult_q_sq_key_mid_qp = 4.72424015517201;
+      rdc->rd_mult_inter_low_qp_fac = 1.018;
+      rdc->rd_mult_inter_mid_qp_fac = 0.896;
+      rdc->rd_mult_inter_high_qp_fac = 1.432;
+      rdc->rd_mult_key_ultralow_qp_fac = 1.073;
+      rdc->rd_mult_key_low_qp_fac = 1.630;
+      rdc->rd_mult_key_mid_qp_fac = 1.050;
     } else if (screen_area <= 320 * 240) {
-      rdc->rd_mult_q_sq_inter_low_qp = 4.506676356706102;
-      rdc->rd_mult_q_sq_inter_mid_qp = 4.489349899621181;
-      rdc->rd_mult_q_sq_inter_high_qp = 4.388244213131458;
-      rdc->rd_mult_q_sq_key_ultralow_qp = 4.217074424696166;
-      rdc->rd_mult_q_sq_key_low_qp = 4.497000582319771;
-      rdc->rd_mult_q_sq_key_mid_qp = 4.2825894884789735;
+      rdc->rd_mult_inter_low_qp_fac = 1.127;
+      rdc->rd_mult_inter_mid_qp_fac = 0.998;
+      rdc->rd_mult_inter_high_qp_fac = 1.463;
+      rdc->rd_mult_key_ultralow_qp_fac = 1.054;
+      rdc->rd_mult_key_low_qp_fac = 1.285;
+      rdc->rd_mult_key_mid_qp_fac = 0.952;
     } else if (screen_area <= 640 * 360) {
-      rdc->rd_mult_q_sq_inter_low_qp = 4.730644123689013;
-      rdc->rd_mult_q_sq_inter_mid_qp = 4.314589509578551;
-      rdc->rd_mult_q_sq_inter_high_qp = 4.3702861603380025;
-      rdc->rd_mult_q_sq_key_ultralow_qp = 4.576902541873747;
-      rdc->rd_mult_q_sq_key_low_qp = 6.068652999601526;
-      rdc->rd_mult_q_sq_key_mid_qp = 4.817707474077241;
+      rdc->rd_mult_inter_low_qp_fac = 1.183;
+      rdc->rd_mult_inter_mid_qp_fac = 0.959;
+      rdc->rd_mult_inter_high_qp_fac = 1.457;
+      rdc->rd_mult_key_ultralow_qp_fac = 1.144;
+      rdc->rd_mult_key_low_qp_fac = 1.734;
+      rdc->rd_mult_key_mid_qp_fac = 1.071;
     } else if (screen_area <= 854 * 480) {
-      rdc->rd_mult_q_sq_inter_low_qp = 4.811470143416073;
-      rdc->rd_mult_q_sq_inter_mid_qp = 4.621618127750201;
-      rdc->rd_mult_q_sq_inter_high_qp = 3.969083125219539;
-      rdc->rd_mult_q_sq_key_ultralow_qp = 4.9854544277222566;
-      rdc->rd_mult_q_sq_key_low_qp = 5.073157238799473;
-      rdc->rd_mult_q_sq_key_mid_qp = 5.7587672849242635;
+      rdc->rd_mult_inter_low_qp_fac = 1.203;
+      rdc->rd_mult_inter_mid_qp_fac = 1.027;
+      rdc->rd_mult_inter_high_qp_fac = 1.027;
+      rdc->rd_mult_key_ultralow_qp_fac = 1.246;
+      rdc->rd_mult_key_low_qp_fac = 1.246;
+      rdc->rd_mult_key_mid_qp_fac = 1.280;
     } else if (screen_area <= 1280 * 720) {
-      rdc->rd_mult_q_sq_inter_low_qp = 5.119381136011107;
-      rdc->rd_mult_q_sq_inter_mid_qp = 4.518613675766538;
-      rdc->rd_mult_q_sq_inter_high_qp = 4.410712348825541;
-      rdc->rd_mult_q_sq_key_ultralow_qp = 3.9468491666607326;
-      rdc->rd_mult_q_sq_key_low_qp = 5.848703119971484;
-      rdc->rd_mult_q_sq_key_mid_qp = 5.368947246228739;
+      rdc->rd_mult_inter_low_qp_fac = 1.280;
+      rdc->rd_mult_inter_mid_qp_fac = 1.004;
+      rdc->rd_mult_inter_high_qp_fac = 1.470;
+      rdc->rd_mult_key_ultralow_qp_fac = 0.987;
+      rdc->rd_mult_key_low_qp_fac = 1.671;
+      rdc->rd_mult_key_mid_qp_fac = 1.193;
     } else {
-      rdc->rd_mult_q_sq_inter_low_qp = 6.00569815296199;
-      rdc->rd_mult_q_sq_inter_mid_qp = 3.932565684947023;
-      rdc->rd_mult_q_sq_inter_high_qp = 3.2141187537667797;
-      rdc->rd_mult_q_sq_key_ultralow_qp = 4.399795006320089;
-      rdc->rd_mult_q_sq_key_low_qp = 10.582906599488298;
-      rdc->rd_mult_q_sq_key_mid_qp = 6.274162346360692;
+      rdc->rd_mult_inter_low_qp_fac = 1.50;
+      rdc->rd_mult_inter_mid_qp_fac = 0.874;
+      rdc->rd_mult_inter_high_qp_fac = 1.07;
+      rdc->rd_mult_key_ultralow_qp_fac = 1.1;
+      rdc->rd_mult_key_low_qp_fac = 2.35;
+      rdc->rd_mult_key_mid_qp_fac = 0.837;
     }
   } else {
     // For now force defaults unless testing
-    rdc->rd_mult_q_sq_inter_low_qp = 4.0;
-    rdc->rd_mult_q_sq_inter_mid_qp = 4.5;
-    rdc->rd_mult_q_sq_inter_high_qp = 3.0;
-    rdc->rd_mult_q_sq_key_ultralow_qp = 4.0;
-    rdc->rd_mult_q_sq_key_low_qp = 3.5;
-    rdc->rd_mult_q_sq_key_mid_qp = 4.5;
+    rdc->rd_mult_inter_low_qp_fac = 1.0;
+    rdc->rd_mult_inter_mid_qp_fac = 1.0;
+    rdc->rd_mult_inter_high_qp_fac = 1.0;
+    rdc->rd_mult_key_ultralow_qp_fac = 1.0;
+    rdc->rd_mult_key_low_qp_fac = 1.0;
+    rdc->rd_mult_key_mid_qp_fac = 1.0;
   }
 }
 
+// Default Rd multiplier values for Q ranges
+#define INTER_LOW_QP_RDM 4.0
+#define INTER_MID_QP_RDM 4.5
+#define INTER_HIGH_QP_RDM 3.0
+#define KEY_ULOW_QP_RDM 4.0
+#define KEY_LOW_QP_RDM 3.5
+#define KEY_MID_QP_RDM 4.5
+#define KEY_HIGH_QP_RDM 7.5
+
 int vp9_compute_rd_mult_based_on_qindex(const VP9_COMP *cpi, int qindex) {
   const RD_CONTROL *rdc = &cpi->rd_ctrl;
   const int q = vp9_dc_quant(qindex, 0, cpi->common.bit_depth);
@@ -275,22 +284,28 @@ int vp9_compute_rd_mult_based_on_qindex(const VP9_COMP *cpi, int qindex) {
 
   if (cpi->common.frame_type != KEY_FRAME) {
     if (qindex < 128) {
-      rdmult = (int)((double)rdmult * rdc->rd_mult_q_sq_inter_low_qp);
+      rdmult = (int)((double)rdmult * INTER_LOW_QP_RDM *
+                     rdc->rd_mult_inter_low_qp_fac);
     } else if (qindex < 190) {
-      rdmult = (int)((double)rdmult * rdc->rd_mult_q_sq_inter_mid_qp);
+      rdmult = (int)((double)rdmult * INTER_MID_QP_RDM *
+                     rdc->rd_mult_inter_mid_qp_fac);
     } else {
-      rdmult = (int)((double)rdmult * rdc->rd_mult_q_sq_inter_high_qp);
+      rdmult = (int)((double)rdmult * INTER_HIGH_QP_RDM *
+                     rdc->rd_mult_inter_high_qp_fac);
     }
   } else {
     if (qindex < 64) {
-      rdmult = (int)((double)rdmult * rdc->rd_mult_q_sq_key_ultralow_qp);
+      rdmult = (int)((double)rdmult * KEY_ULOW_QP_RDM *
+                     rdc->rd_mult_key_ultralow_qp_fac);
     } else if (qindex <= 128) {
-      rdmult = (int)((double)rdmult * rdc->rd_mult_q_sq_key_low_qp);
+      rdmult =
+          (int)((double)rdmult * KEY_LOW_QP_RDM * rdc->rd_mult_key_low_qp_fac);
     } else if (qindex < 190) {
-      rdmult = (int)((double)rdmult * rdc->rd_mult_q_sq_key_mid_qp);
-
+      rdmult =
+          (int)((double)rdmult * KEY_MID_QP_RDM * rdc->rd_mult_key_mid_qp_fac);
     } else {
-      rdmult = (int)((double)rdmult * rdc->rd_mult_q_sq_key_high_qp);
+      rdmult = (int)((double)rdmult * KEY_HIGH_QP_RDM *
+                     rdc->rd_mult_key_high_qp_fac);
     }
   }
 
index 2c9f5e7..4899e1a 100644 (file)
@@ -102,15 +102,14 @@ typedef enum {
 } THR_MODES_SUB8X8;
 
 typedef struct {
-  // RD control parameters
-  // Added for Vizier project.
-  double rd_mult_q_sq_inter_low_qp;
-  double rd_mult_q_sq_inter_mid_qp;
-  double rd_mult_q_sq_inter_high_qp;
-  double rd_mult_q_sq_key_ultralow_qp;
-  double rd_mult_q_sq_key_low_qp;
-  double rd_mult_q_sq_key_mid_qp;
-  double rd_mult_q_sq_key_high_qp;
+  // RD multiplier control factors added for Vizier project.
+  double rd_mult_inter_low_qp_fac;
+  double rd_mult_inter_mid_qp_fac;
+  double rd_mult_inter_high_qp_fac;
+  double rd_mult_key_ultralow_qp_fac;
+  double rd_mult_key_low_qp_fac;
+  double rd_mult_key_mid_qp_fac;
+  double rd_mult_key_high_qp_fac;
 } RD_CONTROL;
 
 typedef struct RD_OPT {