Refactor update_stats()

Organize the code such that update_stats() is dedicated to CDF update
only.

Borg tests show no coding stats changes with either --cdf-update-mode=0
or --cdf-update-mode=1.

Change-Id: Icc6bcb0eb139d519b4bf47fb639e5dcf5fa09444
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 5532ad8..7ef2197 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -976,8 +976,8 @@
     update_palette_cdf(xd, mbmi, counts, allow_update_cdf);
 }
 
-static void update_stats(const AV1_COMMON *const cm, TileDataEnc *tile_data,
-                         ThreadData *td, int mi_row, int mi_col) {
+static void update_stats(const AV1_COMMON *const cm, ThreadData *td, int mi_row,
+                         int mi_col) {
   MACROBLOCK *x = &td->mb;
   MACROBLOCKD *const xd = &x->e_mbd;
   const MB_MODE_INFO *const mbmi = xd->mi[0];
@@ -985,13 +985,6 @@
   const CurrentFrame *const current_frame = &cm->current_frame;
   const BLOCK_SIZE bsize = mbmi->sb_type;
   FRAME_CONTEXT *fc = xd->tile_ctx;
-  const uint8_t allow_update_cdf = tile_data->allow_update_cdf;
-
-  // delta quant applies to both intra and inter
-  const int super_block_upper_left =
-      ((mi_row & (cm->seq_params.mib_size - 1)) == 0) &&
-      ((mi_col & (cm->seq_params.mib_size - 1)) == 0);
-
   const int seg_ref_active =
       segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_REF_FRAME);
 
@@ -1001,25 +994,26 @@
 #if CONFIG_ENTROPY_STATS
     td->counts->skip_mode[skip_mode_ctx][mbmi->skip_mode]++;
 #endif
-    if (allow_update_cdf)
-      update_cdf(fc->skip_mode_cdfs[skip_mode_ctx], mbmi->skip_mode, 2);
+    update_cdf(fc->skip_mode_cdfs[skip_mode_ctx], mbmi->skip_mode, 2);
   }
 
-  if (!mbmi->skip_mode) {
-    if (!seg_ref_active) {
-      const int skip_ctx = av1_get_skip_context(xd);
+  if (!mbmi->skip_mode && !seg_ref_active) {
+    const int skip_ctx = av1_get_skip_context(xd);
 #if CONFIG_ENTROPY_STATS
-      td->counts->skip[skip_ctx][mbmi->skip]++;
+    td->counts->skip[skip_ctx][mbmi->skip]++;
 #endif
-      if (allow_update_cdf) update_cdf(fc->skip_cdfs[skip_ctx], mbmi->skip, 2);
-    }
+    update_cdf(fc->skip_cdfs[skip_ctx], mbmi->skip, 2);
   }
 
+#if CONFIG_ENTROPY_STATS
+  // delta quant applies to both intra and inter
+  const int super_block_upper_left =
+      ((mi_row & (cm->seq_params.mib_size - 1)) == 0) &&
+      ((mi_col & (cm->seq_params.mib_size - 1)) == 0);
   const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
   if (delta_q_info->delta_q_present_flag &&
       (bsize != cm->seq_params.sb_size || !mbmi->skip) &&
       super_block_upper_left) {
-#if CONFIG_ENTROPY_STATS
     const int dq =
         (mbmi->current_qindex - xd->current_qindex) / delta_q_info->delta_q_res;
     const int absdq = abs(dq);
@@ -1027,14 +1021,11 @@
       td->counts->delta_q[i][1]++;
     }
     if (absdq < DELTA_Q_SMALL) td->counts->delta_q[absdq][0]++;
-#endif
-    xd->current_qindex = mbmi->current_qindex;
     if (delta_q_info->delta_lf_present_flag) {
       if (delta_q_info->delta_lf_multi) {
         const int frame_lf_count =
             av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
         for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
-#if CONFIG_ENTROPY_STATS
           const int delta_lf = (mbmi->delta_lf[lf_id] - xd->delta_lf[lf_id]) /
                                delta_q_info->delta_lf_res;
           const int abs_delta_lf = abs(delta_lf);
@@ -1043,11 +1034,8 @@
           }
           if (abs_delta_lf < DELTA_LF_SMALL)
             td->counts->delta_lf_multi[lf_id][abs_delta_lf][0]++;
-#endif
-          xd->delta_lf[lf_id] = mbmi->delta_lf[lf_id];
         }
       } else {
-#if CONFIG_ENTROPY_STATS
         const int delta_lf =
             (mbmi->delta_lf_from_base - xd->delta_lf_from_base) /
             delta_q_info->delta_lf_res;
@@ -1057,394 +1045,311 @@
         }
         if (abs_delta_lf < DELTA_LF_SMALL)
           td->counts->delta_lf[abs_delta_lf][0]++;
-#endif
-        xd->delta_lf_from_base = mbmi->delta_lf_from_base;
       }
     }
   }
+#endif
 
   if (!is_inter_block(mbmi)) {
     sum_intra_stats(cm, td->counts, xd, mbmi, xd->above_mbmi, xd->left_mbmi,
-                    frame_is_intra_only(cm), mi_row, mi_col,
-                    tile_data->allow_update_cdf);
+                    frame_is_intra_only(cm), mi_row, mi_col, 1);
   }
 
   if (av1_allow_intrabc(cm)) {
-    if (allow_update_cdf)
-      update_cdf(fc->intrabc_cdf, is_intrabc_block(mbmi), 2);
+    update_cdf(fc->intrabc_cdf, is_intrabc_block(mbmi), 2);
 #if CONFIG_ENTROPY_STATS
     ++td->counts->intrabc[is_intrabc_block(mbmi)];
 #endif  // CONFIG_ENTROPY_STATS
   }
 
-  if (!frame_is_intra_only(cm)) {
-    RD_COUNTS *rdc = &td->rd_counts;
+  if (frame_is_intra_only(cm) || mbmi->skip_mode) return;
 
-    FRAME_COUNTS *const counts = td->counts;
+  FRAME_COUNTS *const counts = td->counts;
+  const int inter_block = is_inter_block(mbmi);
 
-    if (mbmi->skip_mode) {
-      rdc->skip_mode_used_flag = 1;
-      if (current_frame->reference_mode == REFERENCE_MODE_SELECT) {
-        assert(has_second_ref(mbmi));
-        rdc->compound_ref_used_flag = 1;
-      }
-      set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
-      return;
-    }
-
-    const int inter_block = is_inter_block(mbmi);
-
-    if (!seg_ref_active) {
+  if (!seg_ref_active) {
 #if CONFIG_ENTROPY_STATS
-      counts->intra_inter[av1_get_intra_inter_context(xd)][inter_block]++;
+    counts->intra_inter[av1_get_intra_inter_context(xd)][inter_block]++;
 #endif
-      if (allow_update_cdf) {
-        update_cdf(fc->intra_inter_cdf[av1_get_intra_inter_context(xd)],
-                   inter_block, 2);
-      }
-      // If the segment reference feature is enabled we have only a single
-      // reference frame allowed for the segment so exclude it from
-      // the reference frame counts used to work out probabilities.
-      if (inter_block) {
-        const MV_REFERENCE_FRAME ref0 = mbmi->ref_frame[0];
-        const MV_REFERENCE_FRAME ref1 = mbmi->ref_frame[1];
-
-        av1_collect_neighbors_ref_counts(xd);
-
-        if (current_frame->reference_mode == REFERENCE_MODE_SELECT) {
-          if (has_second_ref(mbmi))
-            // This flag is also updated for 4x4 blocks
-            rdc->compound_ref_used_flag = 1;
-          if (is_comp_ref_allowed(bsize)) {
+    update_cdf(fc->intra_inter_cdf[av1_get_intra_inter_context(xd)],
+               inter_block, 2);
+    // If the segment reference feature is enabled we have only a single
+    // reference frame allowed for the segment so exclude it from
+    // the reference frame counts used to work out probabilities.
+    if (inter_block) {
+      const MV_REFERENCE_FRAME ref0 = mbmi->ref_frame[0];
+      const MV_REFERENCE_FRAME ref1 = mbmi->ref_frame[1];
+      if (current_frame->reference_mode == REFERENCE_MODE_SELECT) {
+        if (is_comp_ref_allowed(bsize)) {
 #if CONFIG_ENTROPY_STATS
-            counts->comp_inter[av1_get_reference_mode_context(xd)]
-                              [has_second_ref(mbmi)]++;
+          counts->comp_inter[av1_get_reference_mode_context(xd)]
+                            [has_second_ref(mbmi)]++;
 #endif  // CONFIG_ENTROPY_STATS
-            if (allow_update_cdf) {
-              update_cdf(av1_get_reference_mode_cdf(xd), has_second_ref(mbmi),
-                         2);
-            }
-          }
+          update_cdf(av1_get_reference_mode_cdf(xd), has_second_ref(mbmi), 2);
         }
+      }
 
-        if (has_second_ref(mbmi)) {
-          const COMP_REFERENCE_TYPE comp_ref_type = has_uni_comp_refs(mbmi)
-                                                        ? UNIDIR_COMP_REFERENCE
-                                                        : BIDIR_COMP_REFERENCE;
-          if (allow_update_cdf) {
-            update_cdf(av1_get_comp_reference_type_cdf(xd), comp_ref_type,
-                       COMP_REFERENCE_TYPES);
-          }
+      if (has_second_ref(mbmi)) {
+        const COMP_REFERENCE_TYPE comp_ref_type = has_uni_comp_refs(mbmi)
+                                                      ? UNIDIR_COMP_REFERENCE
+                                                      : BIDIR_COMP_REFERENCE;
+        update_cdf(av1_get_comp_reference_type_cdf(xd), comp_ref_type,
+                   COMP_REFERENCE_TYPES);
 #if CONFIG_ENTROPY_STATS
-          counts->comp_ref_type[av1_get_comp_reference_type_context(xd)]
-                               [comp_ref_type]++;
+        counts->comp_ref_type[av1_get_comp_reference_type_context(xd)]
+                             [comp_ref_type]++;
 #endif  // CONFIG_ENTROPY_STATS
 
-          if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
-            const int bit = (ref0 == BWDREF_FRAME);
-            if (allow_update_cdf)
-              update_cdf(av1_get_pred_cdf_uni_comp_ref_p(xd), bit, 2);
+        if (comp_ref_type == UNIDIR_COMP_REFERENCE) {
+          const int bit = (ref0 == BWDREF_FRAME);
+          update_cdf(av1_get_pred_cdf_uni_comp_ref_p(xd), bit, 2);
 #if CONFIG_ENTROPY_STATS
-            counts->uni_comp_ref[av1_get_pred_context_uni_comp_ref_p(xd)][0]
-                                [bit]++;
+          counts
+              ->uni_comp_ref[av1_get_pred_context_uni_comp_ref_p(xd)][0][bit]++;
 #endif  // CONFIG_ENTROPY_STATS
-            if (!bit) {
-              const int bit1 = (ref1 == LAST3_FRAME || ref1 == GOLDEN_FRAME);
-              if (allow_update_cdf)
-                update_cdf(av1_get_pred_cdf_uni_comp_ref_p1(xd), bit1, 2);
+          if (!bit) {
+            const int bit1 = (ref1 == LAST3_FRAME || ref1 == GOLDEN_FRAME);
+            update_cdf(av1_get_pred_cdf_uni_comp_ref_p1(xd), bit1, 2);
 #if CONFIG_ENTROPY_STATS
-              counts->uni_comp_ref[av1_get_pred_context_uni_comp_ref_p1(xd)][1]
-                                  [bit1]++;
+            counts->uni_comp_ref[av1_get_pred_context_uni_comp_ref_p1(xd)][1]
+                                [bit1]++;
 #endif  // CONFIG_ENTROPY_STATS
-              if (bit1) {
-                if (allow_update_cdf) {
-                  update_cdf(av1_get_pred_cdf_uni_comp_ref_p2(xd),
-                             ref1 == GOLDEN_FRAME, 2);
-                }
+            if (bit1) {
+              update_cdf(av1_get_pred_cdf_uni_comp_ref_p2(xd),
+                         ref1 == GOLDEN_FRAME, 2);
 #if CONFIG_ENTROPY_STATS
-                counts->uni_comp_ref[av1_get_pred_context_uni_comp_ref_p2(xd)]
-                                    [2][ref1 == GOLDEN_FRAME]++;
-#endif  // CONFIG_ENTROPY_STATS
-              }
-            }
-          } else {
-            const int bit = (ref0 == GOLDEN_FRAME || ref0 == LAST3_FRAME);
-            if (allow_update_cdf)
-              update_cdf(av1_get_pred_cdf_comp_ref_p(xd), bit, 2);
-#if CONFIG_ENTROPY_STATS
-            counts->comp_ref[av1_get_pred_context_comp_ref_p(xd)][0][bit]++;
-#endif  // CONFIG_ENTROPY_STATS
-            if (!bit) {
-              if (allow_update_cdf) {
-                update_cdf(av1_get_pred_cdf_comp_ref_p1(xd),
-                           ref0 == LAST2_FRAME, 2);
-              }
-#if CONFIG_ENTROPY_STATS
-              counts->comp_ref[av1_get_pred_context_comp_ref_p1(xd)][1]
-                              [ref0 == LAST2_FRAME]++;
-#endif  // CONFIG_ENTROPY_STATS
-            } else {
-              if (allow_update_cdf) {
-                update_cdf(av1_get_pred_cdf_comp_ref_p2(xd),
-                           ref0 == GOLDEN_FRAME, 2);
-              }
-#if CONFIG_ENTROPY_STATS
-              counts->comp_ref[av1_get_pred_context_comp_ref_p2(xd)][2]
-                              [ref0 == GOLDEN_FRAME]++;
-#endif  // CONFIG_ENTROPY_STATS
-            }
-            if (allow_update_cdf) {
-              update_cdf(av1_get_pred_cdf_comp_bwdref_p(xd),
-                         ref1 == ALTREF_FRAME, 2);
-            }
-#if CONFIG_ENTROPY_STATS
-            counts->comp_bwdref[av1_get_pred_context_comp_bwdref_p(xd)][0]
-                               [ref1 == ALTREF_FRAME]++;
-#endif  // CONFIG_ENTROPY_STATS
-            if (ref1 != ALTREF_FRAME) {
-              if (allow_update_cdf) {
-                update_cdf(av1_get_pred_cdf_comp_bwdref_p1(xd),
-                           ref1 == ALTREF2_FRAME, 2);
-              }
-#if CONFIG_ENTROPY_STATS
-              counts->comp_bwdref[av1_get_pred_context_comp_bwdref_p1(xd)][1]
-                                 [ref1 == ALTREF2_FRAME]++;
+              counts->uni_comp_ref[av1_get_pred_context_uni_comp_ref_p2(xd)][2]
+                                  [ref1 == GOLDEN_FRAME]++;
 #endif  // CONFIG_ENTROPY_STATS
             }
           }
         } else {
-          const int bit = (ref0 >= BWDREF_FRAME);
-          if (allow_update_cdf)
-            update_cdf(av1_get_pred_cdf_single_ref_p1(xd), bit, 2);
+          const int bit = (ref0 == GOLDEN_FRAME || ref0 == LAST3_FRAME);
+          update_cdf(av1_get_pred_cdf_comp_ref_p(xd), bit, 2);
 #if CONFIG_ENTROPY_STATS
-          counts->single_ref[av1_get_pred_context_single_ref_p1(xd)][0][bit]++;
+          counts->comp_ref[av1_get_pred_context_comp_ref_p(xd)][0][bit]++;
 #endif  // CONFIG_ENTROPY_STATS
-          if (bit) {
-            assert(ref0 <= ALTREF_FRAME);
-            if (allow_update_cdf) {
-              update_cdf(av1_get_pred_cdf_single_ref_p2(xd),
-                         ref0 == ALTREF_FRAME, 2);
-            }
+          if (!bit) {
+            update_cdf(av1_get_pred_cdf_comp_ref_p1(xd), ref0 == LAST2_FRAME,
+                       2);
 #if CONFIG_ENTROPY_STATS
-            counts->single_ref[av1_get_pred_context_single_ref_p2(xd)][1]
-                              [ref0 == ALTREF_FRAME]++;
+            counts->comp_ref[av1_get_pred_context_comp_ref_p1(xd)][1]
+                            [ref0 == LAST2_FRAME]++;
 #endif  // CONFIG_ENTROPY_STATS
-            if (ref0 != ALTREF_FRAME) {
-              if (allow_update_cdf) {
-                update_cdf(av1_get_pred_cdf_single_ref_p6(xd),
-                           ref0 == ALTREF2_FRAME, 2);
-              }
-#if CONFIG_ENTROPY_STATS
-              counts->single_ref[av1_get_pred_context_single_ref_p6(xd)][5]
-                                [ref0 == ALTREF2_FRAME]++;
-#endif  // CONFIG_ENTROPY_STATS
-            }
           } else {
-            const int bit1 = !(ref0 == LAST2_FRAME || ref0 == LAST_FRAME);
-            if (allow_update_cdf)
-              update_cdf(av1_get_pred_cdf_single_ref_p3(xd), bit1, 2);
+            update_cdf(av1_get_pred_cdf_comp_ref_p2(xd), ref0 == GOLDEN_FRAME,
+                       2);
 #if CONFIG_ENTROPY_STATS
-            counts
-                ->single_ref[av1_get_pred_context_single_ref_p3(xd)][2][bit1]++;
+            counts->comp_ref[av1_get_pred_context_comp_ref_p2(xd)][2]
+                            [ref0 == GOLDEN_FRAME]++;
 #endif  // CONFIG_ENTROPY_STATS
-            if (!bit1) {
-              if (allow_update_cdf) {
-                update_cdf(av1_get_pred_cdf_single_ref_p4(xd),
-                           ref0 != LAST_FRAME, 2);
-              }
+          }
+          update_cdf(av1_get_pred_cdf_comp_bwdref_p(xd), ref1 == ALTREF_FRAME,
+                     2);
 #if CONFIG_ENTROPY_STATS
-              counts->single_ref[av1_get_pred_context_single_ref_p4(xd)][3]
-                                [ref0 != LAST_FRAME]++;
+          counts->comp_bwdref[av1_get_pred_context_comp_bwdref_p(xd)][0]
+                             [ref1 == ALTREF_FRAME]++;
 #endif  // CONFIG_ENTROPY_STATS
-            } else {
-              if (allow_update_cdf) {
-                update_cdf(av1_get_pred_cdf_single_ref_p5(xd),
-                           ref0 != LAST3_FRAME, 2);
-              }
+          if (ref1 != ALTREF_FRAME) {
+            update_cdf(av1_get_pred_cdf_comp_bwdref_p1(xd),
+                       ref1 == ALTREF2_FRAME, 2);
 #if CONFIG_ENTROPY_STATS
-              counts->single_ref[av1_get_pred_context_single_ref_p5(xd)][4]
-                                [ref0 != LAST3_FRAME]++;
+            counts->comp_bwdref[av1_get_pred_context_comp_bwdref_p1(xd)][1]
+                               [ref1 == ALTREF2_FRAME]++;
 #endif  // CONFIG_ENTROPY_STATS
-            }
           }
         }
-
-        if (cm->seq_params.enable_interintra_compound &&
-            is_interintra_allowed(mbmi)) {
-          const int bsize_group = size_group_lookup[bsize];
-          if (mbmi->ref_frame[1] == INTRA_FRAME) {
+      } else {
+        const int bit = (ref0 >= BWDREF_FRAME);
+        update_cdf(av1_get_pred_cdf_single_ref_p1(xd), bit, 2);
 #if CONFIG_ENTROPY_STATS
-            counts->interintra[bsize_group][1]++;
-#endif
-            if (allow_update_cdf)
-              update_cdf(fc->interintra_cdf[bsize_group], 1, 2);
+        counts->single_ref[av1_get_pred_context_single_ref_p1(xd)][0][bit]++;
+#endif  // CONFIG_ENTROPY_STATS
+        if (bit) {
+          assert(ref0 <= ALTREF_FRAME);
+          update_cdf(av1_get_pred_cdf_single_ref_p2(xd), ref0 == ALTREF_FRAME,
+                     2);
 #if CONFIG_ENTROPY_STATS
-            counts->interintra_mode[bsize_group][mbmi->interintra_mode]++;
-#endif
-            if (allow_update_cdf) {
-              update_cdf(fc->interintra_mode_cdf[bsize_group],
-                         mbmi->interintra_mode, INTERINTRA_MODES);
-            }
-            if (is_interintra_wedge_used(bsize)) {
+          counts->single_ref[av1_get_pred_context_single_ref_p2(xd)][1]
+                            [ref0 == ALTREF_FRAME]++;
+#endif  // CONFIG_ENTROPY_STATS
+          if (ref0 != ALTREF_FRAME) {
+            update_cdf(av1_get_pred_cdf_single_ref_p6(xd),
+                       ref0 == ALTREF2_FRAME, 2);
 #if CONFIG_ENTROPY_STATS
-              counts->wedge_interintra[bsize][mbmi->use_wedge_interintra]++;
-#endif
-              if (allow_update_cdf) {
-                update_cdf(fc->wedge_interintra_cdf[bsize],
-                           mbmi->use_wedge_interintra, 2);
-              }
-              if (mbmi->use_wedge_interintra) {
+            counts->single_ref[av1_get_pred_context_single_ref_p6(xd)][5]
+                              [ref0 == ALTREF2_FRAME]++;
+#endif  // CONFIG_ENTROPY_STATS
+          }
+        } else {
+          const int bit1 = !(ref0 == LAST2_FRAME || ref0 == LAST_FRAME);
+          update_cdf(av1_get_pred_cdf_single_ref_p3(xd), bit1, 2);
 #if CONFIG_ENTROPY_STATS
-                counts->wedge_idx[bsize][mbmi->interintra_wedge_index]++;
-#endif
-                if (allow_update_cdf) {
-                  update_cdf(fc->wedge_idx_cdf[bsize],
-                             mbmi->interintra_wedge_index, 16);
-                }
-              }
-            }
+          counts->single_ref[av1_get_pred_context_single_ref_p3(xd)][2][bit1]++;
+#endif  // CONFIG_ENTROPY_STATS
+          if (!bit1) {
+            update_cdf(av1_get_pred_cdf_single_ref_p4(xd), ref0 != LAST_FRAME,
+                       2);
+#if CONFIG_ENTROPY_STATS
+            counts->single_ref[av1_get_pred_context_single_ref_p4(xd)][3]
+                              [ref0 != LAST_FRAME]++;
+#endif  // CONFIG_ENTROPY_STATS
           } else {
+            update_cdf(av1_get_pred_cdf_single_ref_p5(xd), ref0 != LAST3_FRAME,
+                       2);
 #if CONFIG_ENTROPY_STATS
-            counts->interintra[bsize_group][0]++;
-#endif
-            if (allow_update_cdf)
-              update_cdf(fc->interintra_cdf[bsize_group], 0, 2);
+            counts->single_ref[av1_get_pred_context_single_ref_p5(xd)][4]
+                              [ref0 != LAST3_FRAME]++;
+#endif  // CONFIG_ENTROPY_STATS
           }
         }
+      }
 
-        set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
-        const MOTION_MODE motion_allowed =
-            cm->switchable_motion_mode
-                ? motion_mode_allowed(xd->global_motion, xd, mbmi,
-                                      cm->allow_warped_motion)
-                : SIMPLE_TRANSLATION;
-        if (mbmi->ref_frame[1] != INTRA_FRAME) {
-          if (motion_allowed == WARPED_CAUSAL) {
+      if (cm->seq_params.enable_interintra_compound &&
+          is_interintra_allowed(mbmi)) {
+        const int bsize_group = size_group_lookup[bsize];
+        if (mbmi->ref_frame[1] == INTRA_FRAME) {
 #if CONFIG_ENTROPY_STATS
-            counts->motion_mode[bsize][mbmi->motion_mode]++;
+          counts->interintra[bsize_group][1]++;
 #endif
-            if (allow_update_cdf) {
-              update_cdf(fc->motion_mode_cdf[bsize], mbmi->motion_mode,
-                         MOTION_MODES);
-            }
-          } else if (motion_allowed == OBMC_CAUSAL) {
+          update_cdf(fc->interintra_cdf[bsize_group], 1, 2);
 #if CONFIG_ENTROPY_STATS
-            counts->obmc[bsize][mbmi->motion_mode == OBMC_CAUSAL]++;
+          counts->interintra_mode[bsize_group][mbmi->interintra_mode]++;
 #endif
-            if (allow_update_cdf) {
-              update_cdf(fc->obmc_cdf[bsize], mbmi->motion_mode == OBMC_CAUSAL,
-                         2);
+          update_cdf(fc->interintra_mode_cdf[bsize_group],
+                     mbmi->interintra_mode, INTERINTRA_MODES);
+          if (is_interintra_wedge_used(bsize)) {
+#if CONFIG_ENTROPY_STATS
+            counts->wedge_interintra[bsize][mbmi->use_wedge_interintra]++;
+#endif
+            update_cdf(fc->wedge_interintra_cdf[bsize],
+                       mbmi->use_wedge_interintra, 2);
+            if (mbmi->use_wedge_interintra) {
+#if CONFIG_ENTROPY_STATS
+              counts->wedge_idx[bsize][mbmi->interintra_wedge_index]++;
+#endif
+              update_cdf(fc->wedge_idx_cdf[bsize], mbmi->interintra_wedge_index,
+                         16);
             }
           }
+        } else {
+#if CONFIG_ENTROPY_STATS
+          counts->interintra[bsize_group][0]++;
+#endif
+          update_cdf(fc->interintra_cdf[bsize_group], 0, 2);
+        }
+      }
+
+      const MOTION_MODE motion_allowed =
+          cm->switchable_motion_mode
+              ? motion_mode_allowed(xd->global_motion, xd, mbmi,
+                                    cm->allow_warped_motion)
+              : SIMPLE_TRANSLATION;
+      if (mbmi->ref_frame[1] != INTRA_FRAME) {
+        if (motion_allowed == WARPED_CAUSAL) {
+#if CONFIG_ENTROPY_STATS
+          counts->motion_mode[bsize][mbmi->motion_mode]++;
+#endif
+          update_cdf(fc->motion_mode_cdf[bsize], mbmi->motion_mode,
+                     MOTION_MODES);
+        } else if (motion_allowed == OBMC_CAUSAL) {
+#if CONFIG_ENTROPY_STATS
+          counts->obmc[bsize][mbmi->motion_mode == OBMC_CAUSAL]++;
+#endif
+          update_cdf(fc->obmc_cdf[bsize], mbmi->motion_mode == OBMC_CAUSAL, 2);
+        }
+      }
+
+      if (has_second_ref(mbmi)) {
+        assert(current_frame->reference_mode != SINGLE_REFERENCE &&
+               is_inter_compound_mode(mbmi->mode) &&
+               mbmi->motion_mode == SIMPLE_TRANSLATION);
+
+        const int masked_compound_used = is_any_masked_compound_used(bsize) &&
+                                         cm->seq_params.enable_masked_compound;
+        if (masked_compound_used) {
+          const int comp_group_idx_ctx = get_comp_group_idx_context(xd);
+#if CONFIG_ENTROPY_STATS
+          ++counts->comp_group_idx[comp_group_idx_ctx][mbmi->comp_group_idx];
+#endif
+          update_cdf(fc->comp_group_idx_cdf[comp_group_idx_ctx],
+                     mbmi->comp_group_idx, 2);
         }
 
-        if (has_second_ref(mbmi)) {
-          assert(current_frame->reference_mode != SINGLE_REFERENCE &&
-                 is_inter_compound_mode(mbmi->mode) &&
-                 mbmi->motion_mode == SIMPLE_TRANSLATION);
-
-          const int masked_compound_used =
-              is_any_masked_compound_used(bsize) &&
-              cm->seq_params.enable_masked_compound;
-          if (masked_compound_used) {
-            const int comp_group_idx_ctx = get_comp_group_idx_context(xd);
+        if (mbmi->comp_group_idx == 0) {
+          const int comp_index_ctx = get_comp_index_context(cm, xd);
 #if CONFIG_ENTROPY_STATS
-            ++counts->comp_group_idx[comp_group_idx_ctx][mbmi->comp_group_idx];
+          ++counts->compound_index[comp_index_ctx][mbmi->compound_idx];
 #endif
-            if (allow_update_cdf) {
-              update_cdf(fc->comp_group_idx_cdf[comp_group_idx_ctx],
-                         mbmi->comp_group_idx, 2);
-            }
-          }
-
-          if (mbmi->comp_group_idx == 0) {
-            const int comp_index_ctx = get_comp_index_context(cm, xd);
-#if CONFIG_ENTROPY_STATS
-            ++counts->compound_index[comp_index_ctx][mbmi->compound_idx];
-#endif
-            if (allow_update_cdf) {
-              update_cdf(fc->compound_index_cdf[comp_index_ctx],
-                         mbmi->compound_idx, 2);
-            }
-          } else {
-            assert(masked_compound_used);
-            if (is_interinter_compound_used(COMPOUND_WEDGE, bsize)) {
-#if CONFIG_ENTROPY_STATS
-              ++counts->compound_type[bsize][mbmi->interinter_comp.type -
-                                             COMPOUND_WEDGE];
-#endif
-              if (allow_update_cdf) {
-                update_cdf(fc->compound_type_cdf[bsize],
-                           mbmi->interinter_comp.type - COMPOUND_WEDGE,
-                           MASKED_COMPOUND_TYPES);
-              }
-            }
-          }
-        }
-        if (mbmi->interinter_comp.type == COMPOUND_WEDGE) {
+          update_cdf(fc->compound_index_cdf[comp_index_ctx], mbmi->compound_idx,
+                     2);
+        } else {
+          assert(masked_compound_used);
           if (is_interinter_compound_used(COMPOUND_WEDGE, bsize)) {
 #if CONFIG_ENTROPY_STATS
-            counts->wedge_idx[bsize][mbmi->interinter_comp.wedge_index]++;
+            ++counts->compound_type[bsize][mbmi->interinter_comp.type -
+                                           COMPOUND_WEDGE];
 #endif
-            if (allow_update_cdf) {
-              update_cdf(fc->wedge_idx_cdf[bsize],
-                         mbmi->interinter_comp.wedge_index, 16);
-            }
+            update_cdf(fc->compound_type_cdf[bsize],
+                       mbmi->interinter_comp.type - COMPOUND_WEDGE,
+                       MASKED_COMPOUND_TYPES);
           }
         }
       }
+      if (mbmi->interinter_comp.type == COMPOUND_WEDGE) {
+        if (is_interinter_compound_used(COMPOUND_WEDGE, bsize)) {
+#if CONFIG_ENTROPY_STATS
+          counts->wedge_idx[bsize][mbmi->interinter_comp.wedge_index]++;
+#endif
+          update_cdf(fc->wedge_idx_cdf[bsize],
+                     mbmi->interinter_comp.wedge_index, 16);
+        }
+      }
+    }
+  }
+
+  if (inter_block &&
+      !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
+    const PREDICTION_MODE mode = mbmi->mode;
+    const int16_t mode_ctx =
+        av1_mode_context_analyzer(mbmi_ext->mode_context, mbmi->ref_frame);
+    if (has_second_ref(mbmi)) {
+#if CONFIG_ENTROPY_STATS
+      ++counts->inter_compound_mode[mode_ctx][INTER_COMPOUND_OFFSET(mode)];
+#endif
+      update_cdf(fc->inter_compound_mode_cdf[mode_ctx],
+                 INTER_COMPOUND_OFFSET(mode), INTER_COMPOUND_MODES);
+    } else {
+      update_inter_mode_stats(fc, counts, mode, mode_ctx, 1);
+    }
+
+    const int new_mv = mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV;
+    if (new_mv) {
+      const uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
+      for (int idx = 0; idx < 2; ++idx) {
+        if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
+          const uint8_t drl_ctx =
+              av1_drl_ctx(mbmi_ext->weight[ref_frame_type], idx);
+          update_cdf(fc->drl_cdf[drl_ctx], mbmi->ref_mv_idx != idx, 2);
+#if CONFIG_ENTROPY_STATS
+          ++counts->drl_mode[drl_ctx][mbmi->ref_mv_idx != idx];
+#endif
+          if (mbmi->ref_mv_idx == idx) break;
+        }
+      }
     }
 
-    if (inter_block &&
-        !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP)) {
-      int16_t mode_ctx;
-      const PREDICTION_MODE mode = mbmi->mode;
-
-      mode_ctx =
-          av1_mode_context_analyzer(mbmi_ext->mode_context, mbmi->ref_frame);
-      if (has_second_ref(mbmi)) {
+    if (have_nearmv_in_inter_mode(mbmi->mode)) {
+      const uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
+      for (int idx = 1; idx < 3; ++idx) {
+        if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
+          const uint8_t drl_ctx =
+              av1_drl_ctx(mbmi_ext->weight[ref_frame_type], idx);
+          update_cdf(fc->drl_cdf[drl_ctx], mbmi->ref_mv_idx != idx - 1, 2);
 #if CONFIG_ENTROPY_STATS
-        ++counts->inter_compound_mode[mode_ctx][INTER_COMPOUND_OFFSET(mode)];
+          ++counts->drl_mode[drl_ctx][mbmi->ref_mv_idx != idx - 1];
 #endif
-        if (allow_update_cdf)
-          update_cdf(fc->inter_compound_mode_cdf[mode_ctx],
-                     INTER_COMPOUND_OFFSET(mode), INTER_COMPOUND_MODES);
-      } else {
-        update_inter_mode_stats(fc, counts, mode, mode_ctx, allow_update_cdf);
-      }
-
-      const int new_mv = mbmi->mode == NEWMV || mbmi->mode == NEW_NEWMV;
-      if (new_mv) {
-        const uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
-        for (int idx = 0; idx < 2; ++idx) {
-          if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
-            const uint8_t drl_ctx =
-                av1_drl_ctx(mbmi_ext->weight[ref_frame_type], idx);
-            if (allow_update_cdf) {
-              update_cdf(fc->drl_cdf[drl_ctx], mbmi->ref_mv_idx != idx, 2);
-            }
-#if CONFIG_ENTROPY_STATS
-            ++counts->drl_mode[drl_ctx][mbmi->ref_mv_idx != idx];
-#endif
-            if (mbmi->ref_mv_idx == idx) break;
-          }
-        }
-      }
-
-      if (have_nearmv_in_inter_mode(mbmi->mode)) {
-        const uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
-        for (int idx = 1; idx < 3; ++idx) {
-          if (mbmi_ext->ref_mv_count[ref_frame_type] > idx + 1) {
-            const uint8_t drl_ctx =
-                av1_drl_ctx(mbmi_ext->weight[ref_frame_type], idx);
-            if (allow_update_cdf) {
-              update_cdf(fc->drl_cdf[drl_ctx], mbmi->ref_mv_idx != idx - 1, 2);
-            }
-#if CONFIG_ENTROPY_STATS
-            ++counts->drl_mode[drl_ctx][mbmi->ref_mv_idx != idx - 1];
-#endif
-            if (mbmi->ref_mv_idx == idx - 1) break;
-          }
+          if (mbmi->ref_mv_idx == idx - 1) break;
         }
       }
     }
@@ -1563,12 +1468,12 @@
                     rate);
 
   if (!dry_run) {
+    const AV1_COMMON *const cm = &cpi->common;
     x->cb_offset += block_size_wide[bsize] * block_size_high[bsize];
     if (bsize == cpi->common.seq_params.sb_size && mbmi->skip == 1 &&
-        cpi->common.delta_q_info.delta_lf_present_flag) {
-      const int frame_lf_count = av1_num_planes(&cpi->common) > 1
-                                     ? FRAME_LF_COUNT
-                                     : FRAME_LF_COUNT - 2;
+        cm->delta_q_info.delta_lf_present_flag) {
+      const int frame_lf_count =
+          av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
       for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id)
         mbmi->delta_lf[lf_id] = xd->delta_lf[lf_id];
       mbmi->delta_lf_from_base = xd->delta_lf_from_base;
@@ -1580,8 +1485,63 @@
       else
         mbmi->comp_group_idx = 1;
     }
-    update_stats(&cpi->common, tile_data, td, mi_row, mi_col);
+
+    // delta quant applies to both intra and inter
+    const int super_block_upper_left =
+        ((mi_row & (cm->seq_params.mib_size - 1)) == 0) &&
+        ((mi_col & (cm->seq_params.mib_size - 1)) == 0);
+    const DeltaQInfo *const delta_q_info = &cm->delta_q_info;
+    if (delta_q_info->delta_q_present_flag &&
+        (bsize != cm->seq_params.sb_size || !mbmi->skip) &&
+        super_block_upper_left) {
+      xd->current_qindex = mbmi->current_qindex;
+      if (delta_q_info->delta_lf_present_flag) {
+        if (delta_q_info->delta_lf_multi) {
+          const int frame_lf_count =
+              av1_num_planes(cm) > 1 ? FRAME_LF_COUNT : FRAME_LF_COUNT - 2;
+          for (int lf_id = 0; lf_id < frame_lf_count; ++lf_id) {
+            xd->delta_lf[lf_id] = mbmi->delta_lf[lf_id];
+          }
+        } else {
+          xd->delta_lf_from_base = mbmi->delta_lf_from_base;
+        }
+      }
+    }
+
+    RD_COUNTS *rdc = &td->rd_counts;
+    if (mbmi->skip_mode) {
+      assert(!frame_is_intra_only(cm));
+      rdc->skip_mode_used_flag = 1;
+      if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT) {
+        assert(has_second_ref(mbmi));
+        rdc->compound_ref_used_flag = 1;
+      }
+      set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
+    } else {
+      const int seg_ref_active =
+          segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_REF_FRAME);
+      if (!seg_ref_active) {
+        // If the segment reference feature is enabled we have only a single
+        // reference frame allowed for the segment so exclude it from
+        // the reference frame counts used to work out probabilities.
+        if (is_inter_block(mbmi)) {
+          av1_collect_neighbors_ref_counts(xd);
+          if (cm->current_frame.reference_mode == REFERENCE_MODE_SELECT) {
+            if (has_second_ref(mbmi)) {
+              // This flag is also updated for 4x4 blocks
+              rdc->compound_ref_used_flag = 1;
+            }
+          }
+          set_ref_ptrs(cm, xd, mbmi->ref_frame[0], mbmi->ref_frame[1]);
+        }
+      }
+    }
+
+    if (tile_data->allow_update_cdf) {
+      update_stats(&cpi->common, td, mi_row, mi_col);
+    }
   }
+
   x->rdmult = origin_mult;
 }
 
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 9b51d4c..a2b51c1 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -10955,6 +10955,7 @@
   mbmi->ref_frame[1] = NONE_FRAME;
   mbmi->use_intrabc = 0;
   mbmi->mv[0].as_int = 0;
+  mbmi->skip_mode = 0;
 
   const int64_t intra_yrd =
       rd_pick_intra_sby_mode(cpi, x, mi_row, mi_col, &rate_y, &rate_y_tokenonly,