Add command-line -enable-cdef option

Change-Id: Ic061eeab1563c249fd9d3738025aac40bc3ee542
diff --git a/aom/aomcx.h b/aom/aomcx.h
index 6544b9f..2f9bafd 100644
--- a/aom/aomcx.h
+++ b/aom/aomcx.h
@@ -427,6 +427,19 @@
    */
   AOME_SET_ENABLEAUTOBWDREF,
 
+  /*!\brief Codec control function to encode with CDEF.
+   *
+   * CDEF is the constrained directional enhancement filter which is an
+   * in-loop filter aiming to remove coding artifacts
+   *                          0 = do not apply CDEF
+   *                          1 = apply CDEF
+   *
+   *  By default, the encoder applies CDEF.
+   *
+   * Experiment: AOM_CDEF
+   */
+  AV1E_SET_ENABLE_CDEF,
+
   /*!\brief Codec control function to encode with quantisation matrices.
    *
    * AOM can operate with default quantisation matrices dependent on
@@ -726,6 +739,9 @@
 AOM_CTRL_USE_TYPE(AV1E_SET_LOSSLESS, unsigned int)
 #define AOM_CTRL_AV1E_SET_LOSSLESS
 
+AOM_CTRL_USE_TYPE(AV1E_SET_ENABLE_CDEF, unsigned int)
+#define AOM_CTRL_AV1E_SET_ENABLE_CDEF
+
 AOM_CTRL_USE_TYPE(AV1E_SET_ENABLE_QM, unsigned int)
 #define AOM_CTRL_AV1E_SET_ENABLE_QM
 
diff --git a/aomenc.c b/aomenc.c
index 43ad4bf..0ed4714 100644
--- a/aomenc.c
+++ b/aomenc.c
@@ -428,6 +428,12 @@
 #endif  // CONFIG_LOOPFILTERING_ACROSS_TILES
 static const arg_def_t lossless =
     ARG_DEF(NULL, "lossless", 1, "Lossless mode (0: false (default), 1: true)");
+#if CONFIG_CDEF
+static const arg_def_t enable_cdef =
+    ARG_DEF(NULL, "enable-cdef", 1,
+            "Enable the constrained directional enhancement filter (0: false, "
+            "1: true (default))");
+#endif
 #if CONFIG_AOM_QM
 static const arg_def_t enable_qm =
     ARG_DEF(NULL, "enable-qm", 1,
@@ -567,6 +573,9 @@
                                        &max_inter_rate_pct,
                                        &gf_cbr_boost_pct,
                                        &lossless,
+#if CONFIG_CDEF
+                                       &enable_cdef,
+#endif
 #if CONFIG_AOM_QM
                                        &enable_qm,
                                        &qm_min,
@@ -624,6 +633,9 @@
                                         AV1E_SET_MAX_INTER_BITRATE_PCT,
                                         AV1E_SET_GF_CBR_BOOST_PCT,
                                         AV1E_SET_LOSSLESS,
+#if CONFIG_CDEF
+                                        AV1E_SET_ENABLE_CDEF,
+#endif
 #if CONFIG_AOM_QM
                                         AV1E_SET_ENABLE_QM,
                                         AV1E_SET_QM_MIN,
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index c5a71b5..91e43db 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -54,6 +54,9 @@
   unsigned int rc_max_inter_bitrate_pct;
   unsigned int gf_cbr_boost_pct;
   unsigned int lossless;
+#if CONFIG_CDEF
+  unsigned int enable_cdef;
+#endif
 #if CONFIG_AOM_QM
   unsigned int enable_qm;
   unsigned int qm_min;
@@ -117,6 +120,9 @@
   0,              // rc_max_inter_bitrate_pct
   0,              // gf_cbr_boost_pct
   0,              // lossless
+#if CONFIG_CDEF
+  1,  // enable_cdef
+#endif
 #if CONFIG_AOM_QM
   0,                 // enable_qm
   DEFAULT_QM_FIRST,  // qm_min
@@ -495,6 +501,9 @@
   oxcf->cq_level = av1_quantizer_to_qindex(extra_cfg->cq_level);
   oxcf->fixed_q = -1;
 
+#if CONFIG_CDEF
+  oxcf->using_cdef = extra_cfg->enable_cdef;
+#endif
 #if CONFIG_AOM_QM
   oxcf->using_qm = extra_cfg->enable_qm;
   oxcf->qm_minlevel = extra_cfg->qm_min;
@@ -864,6 +873,15 @@
   return update_extra_cfg(ctx, &extra_cfg);
 }
 
+#if CONFIG_CDEF
+static aom_codec_err_t ctrl_set_enable_cdef(aom_codec_alg_priv_t *ctx,
+                                            va_list args) {
+  struct av1_extracfg extra_cfg = ctx->extra_cfg;
+  extra_cfg.enable_cdef = CAST(AV1E_SET_ENABLE_CDEF, args);
+  return update_extra_cfg(ctx, &extra_cfg);
+}
+#endif
+
 #if CONFIG_AOM_QM
 static aom_codec_err_t ctrl_set_enable_qm(aom_codec_alg_priv_t *ctx,
                                           va_list args) {
@@ -1589,6 +1607,9 @@
   { AV1E_SET_MAX_INTER_BITRATE_PCT, ctrl_set_rc_max_inter_bitrate_pct },
   { AV1E_SET_GF_CBR_BOOST_PCT, ctrl_set_rc_gf_cbr_boost_pct },
   { AV1E_SET_LOSSLESS, ctrl_set_lossless },
+#if CONFIG_CDEF
+  { AV1E_SET_ENABLE_CDEF, ctrl_set_enable_cdef },
+#endif
 #if CONFIG_AOM_QM
   { AV1E_SET_ENABLE_QM, ctrl_set_enable_qm },
   { AV1E_SET_QM_MIN, ctrl_set_qm_min },
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index bde117c..e3630b0 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -4615,20 +4615,25 @@
 #endif
 #if CONFIG_LOOP_RESTORATION
   int no_restoration = 0;
-#endif
   if (is_lossless_requested(&cpi->oxcf)
 #if CONFIG_EXT_TILE
       || cm->single_tile_decoding
 #endif
       ) {
     no_loopfilter = 1;
-#if CONFIG_CDEF
-    no_cdef = 1;
-#endif
-#if CONFIG_LOOP_RESTORATION
     no_restoration = 1;
-#endif
   }
+#endif
+
+#if CONFIG_CDEF
+  if (is_lossless_requested(&cpi->oxcf) || !cpi->oxcf.using_cdef
+#if CONFIG_EXT_TILE
+      || cm->single_tile_decoding
+#endif
+      ) {
+    no_cdef = 1;
+  }
+#endif
 
   if (no_loopfilter) {
 #if CONFIG_LOOPFILTER_LEVEL
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 32cb2f1..3584098 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -196,6 +196,9 @@
 #if CONFIG_EXT_DELTA_Q
   DELTAQ_MODE deltaq_mode;
 #endif
+#if CONFIG_CDEF
+  int using_cdef;
+#endif
 #if CONFIG_AOM_QM
   int using_qm;
   int qm_minlevel;