Add --enable-dist-8x8, rename existing '--tune' as '--tune-metric'

Added 'cdef-dist' and 'daala-dist' options to --tune-metric'.
(Actual bodies of the options will be added in later separate commits)

Also, if --tune=cdef-dist or --tune=daala-dist,
enable-dist-8x8 is automatically turned on.

This commit is a part of prep-work to remove DIST_8X8, CDEF_DIST,
and DAALA_DIST experimental flags.

Change-Id: I0790b9537dfaf166cb59741997ae27da10697a33
diff --git a/aom/aomcx.h b/aom/aomcx.h
index 2b87a71..a465525 100644
--- a/aom/aomcx.h
+++ b/aom/aomcx.h
@@ -458,6 +458,21 @@
    */
   AV1E_SET_QM_MAX,
 
+  /*!\brief Codec control function to encode with dist_8x8.
+   *
+   *  The dist_8x8 is enabled automatically for model tuning parameters that
+   *  require measuring distortion at the 8x8 level. This control also allows
+   *  measuring distortion at the 8x8 level for other tuning options
+   *  (e.g., PSNR), for testing purposes.
+   *                          0 = do not use dist_8x8
+   *                          1 = use dist_8x8
+   *
+   *  By default, the encoder does not use dist_8x8
+   *
+   * Experiment: DIST_8X8
+   */
+  AV1E_SET_ENABLE_DIST_8X8,
+
   /*!\brief Codec control function to set a maximum number of tile groups.
    *
    * This will set the maximum number of tile groups. This will be
@@ -622,7 +637,14 @@
  * Changes the encoder to tune for certain types of input material.
  *
  */
-typedef enum { AOM_TUNE_PSNR, AOM_TUNE_SSIM } aom_tune_metric;
+typedef enum {
+  AOM_TUNE_PSNR,
+  AOM_TUNE_SSIM,
+#ifdef CONFIG_DIST_8X8
+  AOM_TUNE_CDEF_DIST,
+  AOM_TUNE_DAALA_DIST
+#endif
+} aom_tune_metric;
 
 /*!\cond */
 /*!\brief Encoder control function parameter type
@@ -693,6 +715,9 @@
 AOM_CTRL_USE_TYPE(AV1E_SET_ENABLE_QM, unsigned int)
 #define AOM_CTRL_AV1E_SET_ENABLE_QM
 
+AOM_CTRL_USE_TYPE(AV1E_SET_ENABLE_DIST_8X8, unsigned int)
+#define AOM_CTRL_AV1E_SET_ENABLE_DIST_8X8
+
 AOM_CTRL_USE_TYPE(AV1E_SET_QM_MIN, unsigned int)
 #define AOM_CTRL_AV1E_SET_QM_MIN
 
diff --git a/aomenc.c b/aomenc.c
index 026f7ef..57dee38 100644
--- a/aomenc.c
+++ b/aomenc.c
@@ -377,10 +377,16 @@
 static const arg_def_t arnr_strength =
     ARG_DEF(NULL, "arnr-strength", 1, "AltRef filter strength (0..6)");
 static const struct arg_enum_list tuning_enum[] = {
-  { "psnr", AOM_TUNE_PSNR }, { "ssim", AOM_TUNE_SSIM }, { NULL, 0 }
+  { "psnr", AOM_TUNE_PSNR },
+  { "ssim", AOM_TUNE_SSIM },
+#ifdef CONFIG_DIST_8X8
+  { "cdef-dist", AOM_TUNE_CDEF_DIST },
+  { "daala-dist", AOM_TUNE_DAALA_DIST },
+#endif
+  { NULL, 0 }
 };
-static const arg_def_t tune_ssim =
-    ARG_DEF_ENUM(NULL, "tune", 1, "Material to favor", tuning_enum);
+static const arg_def_t tune_metric =
+    ARG_DEF_ENUM(NULL, "tune", 1, "Distortion metric tuned with", tuning_enum);
 static const arg_def_t cq_level =
     ARG_DEF(NULL, "cq-level", 1, "Constant/Constrained Quality level");
 static const arg_def_t max_intra_rate_pct =
@@ -418,6 +424,11 @@
 static const arg_def_t qm_max = ARG_DEF(
     NULL, "qm-max", 1, "Max quant matrix flatness (0..15), default is 16");
 #endif
+#if CONFIG_DIST_8X8
+static const arg_def_t enable_dist_8x8 =
+    ARG_DEF(NULL, "enable-dist-8x8", 1,
+            "Enable dist-8x8 (0: false (default), 1: true)");
+#endif  // CONFIG_DIST_8X8
 static const arg_def_t num_tg = ARG_DEF(
     NULL, "num-tile-groups", 1, "Maximum number of tile groups, default is 1");
 static const arg_def_t mtu_size =
@@ -550,7 +561,7 @@
 #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
                                        &arnr_maxframes,
                                        &arnr_strength,
-                                       &tune_ssim,
+                                       &tune_metric,
                                        &cq_level,
                                        &max_intra_rate_pct,
                                        &max_inter_rate_pct,
@@ -561,6 +572,9 @@
                                        &qm_min,
                                        &qm_max,
 #endif
+#if CONFIG_DIST_8X8
+                                       &enable_dist_8x8,
+#endif
                                        &frame_parallel_decoding,
                                        &aq_mode,
 #if CONFIG_EXT_DELTA_Q
@@ -617,6 +631,9 @@
                                         AV1E_SET_QM_MIN,
                                         AV1E_SET_QM_MAX,
 #endif
+#if CONFIG_DIST_8X8
+                                        AV1E_SET_ENABLE_DIST_8X8,
+#endif
                                         AV1E_SET_FRAME_PARALLEL_DECODING,
                                         AV1E_SET_AQ_MODE,
 #if CONFIG_EXT_DELTA_Q
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index f670762..ddca3d5 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -58,6 +58,9 @@
   unsigned int qm_min;
   unsigned int qm_max;
 #endif
+#if CONFIG_DIST_8X8
+  unsigned int enable_dist_8x8;
+#endif
   unsigned int num_tg;
   unsigned int mtu_size;
 #if CONFIG_TEMPMV_SIGNALING
@@ -122,6 +125,9 @@
   DEFAULT_QM_FIRST,  // qm_min
   DEFAULT_QM_LAST,   // qm_max
 #endif
+#if CONFIG_DIST_8X8
+  0,
+#endif
   1,  // max number of tile groups
   0,  // mtu_size
 #if CONFIG_TEMPMV_SIGNALING
@@ -326,6 +332,14 @@
   if (extra_cfg->tuning == AOM_TUNE_SSIM)
     ERROR("Option --tune=ssim is not currently supported in AV1.");
 
+#if CONFIG_DIST_8X8
+  if (extra_cfg->tuning == AOM_TUNE_CDEF_DIST)
+    ERROR("Option --tune=cdef-dist is not currently supported in AV1.");
+
+  if (extra_cfg->tuning == AOM_TUNE_DAALA_DIST)
+    ERROR("Option --tune=daala-dist is not currently supported in AV1.");
+#endif  // CONFIG_DIST_8X8
+
   if (cfg->g_pass == AOM_RC_LAST_PASS) {
 #if !CONFIG_XIPHRC
     const size_t packet_sz = sizeof(FIRSTPASS_STATS);
@@ -480,7 +494,12 @@
   oxcf->qm_minlevel = extra_cfg->qm_min;
   oxcf->qm_maxlevel = extra_cfg->qm_max;
 #endif
-
+#if CONFIG_DIST_8X8
+  oxcf->using_dist_8x8 = extra_cfg->enable_dist_8x8;
+  if (extra_cfg->tuning == AOM_TUNE_CDEF_DIST ||
+      extra_cfg->tuning == AOM_TUNE_DAALA_DIST)
+    oxcf->using_dist_8x8 = 1;
+#endif
   oxcf->num_tile_groups = extra_cfg->num_tg;
 #if CONFIG_EXT_TILE
   // In large-scale tile encoding mode, num_tile_groups is always 1.
@@ -865,7 +884,14 @@
   return update_extra_cfg(ctx, &extra_cfg);
 }
 #endif
-
+#if CONFIG_DIST_8X8
+static aom_codec_err_t ctrl_set_enable_dist_8x8(aom_codec_alg_priv_t *ctx,
+                                                va_list args) {
+  struct av1_extracfg extra_cfg = ctx->extra_cfg;
+  extra_cfg.enable_dist_8x8 = CAST(AV1E_SET_ENABLE_DIST_8X8, args);
+  return update_extra_cfg(ctx, &extra_cfg);
+}
+#endif
 static aom_codec_err_t ctrl_set_num_tg(aom_codec_alg_priv_t *ctx,
                                        va_list args) {
   struct av1_extracfg extra_cfg = ctx->extra_cfg;
@@ -1547,6 +1573,9 @@
   { AV1E_SET_QM_MIN, ctrl_set_qm_min },
   { AV1E_SET_QM_MAX, ctrl_set_qm_max },
 #endif
+#if CONFIG_DIST_8X8
+  { AV1E_SET_ENABLE_DIST_8X8, ctrl_set_enable_dist_8x8 },
+#endif
   { AV1E_SET_NUM_TG, ctrl_set_num_tg },
   { AV1E_SET_MTU, ctrl_set_mtu },
 #if CONFIG_TEMPMV_SIGNALING
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index ce9d779..0c2fccd 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -207,6 +207,9 @@
   int qm_minlevel;
   int qm_maxlevel;
 #endif
+#if CONFIG_DIST_8X8
+  int using_dist_8x8;
+#endif
   unsigned int num_tile_groups;
   unsigned int mtu;