[tcq] frame adaptive TCQ

--enable-tcq=3: use 4-state TCQ on key/altref only
diff --git a/aom/aom_encoder.h b/aom/aom_encoder.h
index c37d5f6..617c3e9 100644
--- a/aom/aom_encoder.h
+++ b/aom/aom_encoder.h
@@ -1081,8 +1081,8 @@
    * 0 = disable, use scalar quantization.
    * 1 = use 4-state trellis
    * 2 = use 8-state trellis (TBD)
-   * 3 = use 4-state trellis (frame adaptive)
-   * 4 = use 8-state trellis (TBD frame adaptive)
+   * 3 = use 4-state trellis (frame adaptive (key/altref))
+   * 4 = use 8-state trellis (TBD frame adaptive (key/altref))
    */
   unsigned int enable_tcq;
 #endif
diff --git a/apps/aomenc.c b/apps/aomenc.c
index 7cd28ed..b85ac54 100644
--- a/apps/aomenc.c
+++ b/apps/aomenc.c
@@ -754,11 +754,7 @@
   config->enable_reduced_reference_set = 0;
   config->reduced_tx_type_set = 0;
   config->enable_refmvbank = 1;
-#if CONFIG_DQ
-  config->enable_parity_hiding = 0;
-#else
   config->enable_parity_hiding = 1;
-#endif
 #if CONFIG_MRSSE
   config->enable_mrsse = 0;
 #endif  // CONFIG_MRSSE
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 632e86c..b054e22 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -596,11 +596,7 @@
   0,    // max_drl_refbvs
 #endif  // CONFIG_IBC_BV_IMPROVEMENT && CONFIG_IBC_MAX_DRL
   1,    // enable_refmvbank
-#if CONFIG_DQ
-  0,  // enable_parity_hiding
-#else
   1,
-#endif
 #if CONFIG_MRSSE
   0,
 #endif  // CONFIG_MRSSE
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 8078a18..da65c73 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -6911,7 +6911,8 @@
 #else
     seq_params->enable_tcq = 1;
 #endif
-  if (seq_params->enable_tcq == 0) {
+  if (seq_params->enable_tcq == TCQ_DISABLE ||
+      seq_params->enable_tcq >= TCQ_4ST_FR) {
     seq_params->enable_parity_hiding = aom_rb_read_bit(rb);
   } else {
     seq_params->enable_parity_hiding = 0;
@@ -8393,7 +8394,11 @@
   }
 #endif
 
-  if (features->coded_lossless || !cm->seq_params.enable_parity_hiding)
+  if (features->coded_lossless || !cm->seq_params.enable_parity_hiding
+#if CONFIG_DQ
+      || features->tcq_mode
+#endif
+  )
     features->allow_parity_hiding = false;
   else
     features->allow_parity_hiding = aom_rb_read_bit(rb);
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index ddb63c6..8c12174 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -5560,7 +5560,9 @@
   if (enable_tcq) {
     aom_wb_write_literal(wb, enable_tcq - 1, 2);
   }
-  if (enable_tcq == 0) {
+  if (enable_tcq == TCQ_DISABLE || enable_tcq >= TCQ_4ST_FR) {
+    // Signal whether parity hiding is used if TCQ is
+    // disabled, or enabled/disabled at frame level.
     aom_wb_write_bit(wb, seq_params->enable_parity_hiding);
   }
 #else
@@ -6276,7 +6278,11 @@
 #endif
   }
 
-  if (features->coded_lossless || !cm->seq_params.enable_parity_hiding) {
+  if (features->coded_lossless || !cm->seq_params.enable_parity_hiding
+#if CONFIG_DQ
+      || features->tcq_mode
+#endif
+  ) {
     assert(features->allow_parity_hiding == false);
   } else {
     aom_wb_write_bit(wb, features->allow_parity_hiding);
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 978dea1..d8e222b 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1486,7 +1486,11 @@
 
 static void av1_enc_setup_ph_frame(AV1_COMP *cpi) {
   AV1_COMMON *const cm = &cpi->common;
-  if (cm->features.coded_lossless || !cm->seq_params.enable_parity_hiding)
+  if (cm->features.coded_lossless || !cm->seq_params.enable_parity_hiding
+#if CONFIG_DQ
+      || cm->features.tcq_mode
+#endif
+  )
     cm->features.allow_parity_hiding = false;
   else
     cm->features.allow_parity_hiding = true;
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 0467a31..b7cf279 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -377,9 +377,6 @@
     seq->force_screen_content_tools = 2;
     seq->force_integer_mv = 2;
   }
-#if CONFIG_DQ
-  seq->enable_tcq = tool_cfg->enable_tcq;
-#endif
   seq->order_hint_info.order_hint_bits_minus_1 =
       seq->order_hint_info.enable_order_hint
           ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
@@ -519,7 +516,16 @@
   }
 
   seq->enable_refmvbank = tool_cfg->enable_refmvbank;
+#if CONFIG_DQ
+  seq->enable_tcq = tool_cfg->enable_tcq;
+  if (seq->enable_tcq == TCQ_DISABLE || seq->enable_tcq >= TCQ_4ST_FR) {
+    seq->enable_parity_hiding = tool_cfg->enable_parity_hiding;
+  } else {
+    seq->enable_parity_hiding = 0;
+  }
+#else
   seq->enable_parity_hiding = tool_cfg->enable_parity_hiding;
+#endif
 #if CONFIG_IMPROVED_GLOBAL_MOTION
   // TODO(rachelbarker): Check if cpi->sf.gm_sf.gm_search_type is set by this
   // point, and set to 0 if cpi->sf.gm_sf.gm_search_type == GM_DISABLE_SEARCH
@@ -4115,7 +4121,7 @@
 
 #if CONFIG_DQ
   if (cm->seq_params.enable_tcq >= TCQ_4ST_FR) {
-    int use_tcq = frame_is_intra_only(cm);
+    int use_tcq = frame_is_intra_only(cm) || current_frame->pyramid_level <= 1;
     features->tcq_mode = use_tcq ? cm->seq_params.enable_tcq - 2 : 0;
   } else {
     features->tcq_mode = cm->seq_params.enable_tcq;