Complete refactor of compound search loop

Now how to split the compound modes can be set only at
one place in the code.

Experiments to continue on optimal split of the compound modes
in two loop search case, or use of single loop search.

Change-Id: If201b501fb36454accc35d79d9d2fc21f8a080e7
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index ef079bf..b847038 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -9794,7 +9794,8 @@
     if (!is_interinter_compound_used(cur_type, bsize)) continue;
     if (cur_type == COMPOUND_DISTWTD &&
         (!cm->seq_params.order_hint_info.enable_dist_wtd_comp ||
-         cpi->sf.use_dist_wtd_comp_flag == DIST_WTD_COMP_DISABLED))
+         cpi->sf.use_dist_wtd_comp_flag == DIST_WTD_COMP_DISABLED ||
+         mbmi->mode == GLOBAL_GLOBALMV))
       continue;
     if (((1 << cur_type) & mode_search_mask) == 0) continue;
     tmp_rate_mv = *rate_mv;
@@ -9998,12 +9999,21 @@
   int backup_rate_mv = 0;
   inter_mode_info mode_info[MAX_REF_MV_SERCH];
 
-  int comp_idx;
-  const int search_dist_wtd_comp =
-      is_comp_pred & cm->seq_params.order_hint_info.enable_dist_wtd_comp &
-      (mbmi->mode != GLOBAL_GLOBALMV) &
-      (cpi->sf.use_dist_wtd_comp_flag != DIST_WTD_COMP_DISABLED) &
-      cpi->sf.two_loop_comp_search;
+  int mode_search_mask[2];
+  const int do_two_loop_comp_search =
+      is_comp_pred && cpi->sf.two_loop_comp_search;
+  if (do_two_loop_comp_search) {
+    // TODO(debargha): Change this to try alternate ways of splitting
+    // modes while doing two pass compound_mode search.
+    mode_search_mask[0] = (1 << COMPOUND_AVERAGE) | (1 << COMPOUND_WEDGE) |
+                          (1 << COMPOUND_DIFFWTD);
+  } else {
+    mode_search_mask[0] = (1 << COMPOUND_AVERAGE) | (1 << COMPOUND_DISTWTD) |
+                          (1 << COMPOUND_WEDGE) | (1 << COMPOUND_DIFFWTD);
+  }
+  mode_search_mask[1] = ((1 << COMPOUND_AVERAGE) | (1 << COMPOUND_DISTWTD) |
+                         (1 << COMPOUND_WEDGE) | (1 << COMPOUND_DIFFWTD)) -
+                        mode_search_mask[0];
 
   // TODO(jingning): This should be deprecated shortly.
   const int has_nearmv = have_nearmv_in_inter_mode(mbmi->mode) ? 1 : 0;
@@ -10059,22 +10069,21 @@
       continue;
     }
 
-    mbmi->comp_group_idx = 0;
-    mbmi->compound_idx = 1;
     const RD_STATS backup_rd_stats = *rd_stats;
-    // If !search_dist_wtd_comp, we need to force mbmi->compound_idx = 1.
-    for (comp_idx = 1; comp_idx >= !search_dist_wtd_comp; --comp_idx) {
+
+    for (int comp_loop_idx = 0; comp_loop_idx <= do_two_loop_comp_search;
+         ++comp_loop_idx) {
       int rs = 0;
       int compmode_interinter_cost = 0;
 
-      if (is_comp_pred && comp_idx == 0) *rd_stats = backup_rd_stats;
+      if (is_comp_pred && comp_loop_idx == 1) *rd_stats = backup_rd_stats;
 
       int_mv cur_mv[2];
       if (!build_cur_mv(cur_mv, this_mode, cm, x)) {
         continue;
       }
       if (have_newmv_in_inter_mode(this_mode)) {
-        if (comp_idx == 0) {
+        if (comp_loop_idx == 1) {
           cur_mv[0] = backup_mv[0];
           cur_mv[1] = backup_mv[1];
           rate_mv = backup_rate_mv;
@@ -10086,7 +10095,7 @@
           newmv_ret_val = args->single_newmv_valid[ref_mv_idx][ref0] ? 0 : 1;
           cur_mv[0] = args->single_newmv[ref_mv_idx][ref0];
           rate_mv = args->single_newmv_rate[ref_mv_idx][ref0];
-        } else if (comp_idx == 1) {
+        } else if (comp_loop_idx == 0) {
           newmv_ret_val = handle_newmv(cpi, x, bsize, cur_mv, mi_row, mi_col,
                                        &rate_mv, args);
 
@@ -10192,26 +10201,27 @@
 
       int skip_build_pred = 0;
       if (is_comp_pred) {
-        int mode_search_mask;
-        if (cpi->sf.two_loop_comp_search) {
-          mode_search_mask = comp_idx ? (1 << COMPOUND_AVERAGE) |
-                                            (1 << COMPOUND_WEDGE) |
-                                            (1 << COMPOUND_DIFFWTD)
-                                      : (1 << COMPOUND_DISTWTD);
-        } else {
-          mode_search_mask = (1 << COMPOUND_AVERAGE) | (1 << COMPOUND_WEDGE) |
-                             (1 << COMPOUND_DIFFWTD) | (1 << COMPOUND_DISTWTD);
-        }
-        if (mode_search_mask == (1 << COMPOUND_DISTWTD) ||
-            mode_search_mask == (1 << COMPOUND_AVERAGE)) {
-          mbmi->interinter_comp.type =
-              (mode_search_mask == (1 << COMPOUND_AVERAGE)) ? COMPOUND_AVERAGE
-                                                            : COMPOUND_DISTWTD;
+        if (mode_search_mask[comp_loop_idx] == (1 << COMPOUND_AVERAGE)) {
+          // Only compound_average
+          mbmi->interinter_comp.type = COMPOUND_AVERAGE;
           mbmi->num_proj_ref = 0;
           mbmi->motion_mode = SIMPLE_TRANSLATION;
           mbmi->comp_group_idx = 0;
-          mbmi->compound_idx = (mode_search_mask == (1 << COMPOUND_AVERAGE));
-
+          mbmi->compound_idx = 1;
+          const int comp_index_ctx = get_comp_index_context(cm, xd);
+          compmode_interinter_cost +=
+              x->comp_idx_cost[comp_index_ctx][mbmi->compound_idx];
+        } else if (mode_search_mask[comp_loop_idx] == (1 << COMPOUND_DISTWTD)) {
+          // Only compound_distwtd
+          if (!cm->seq_params.order_hint_info.enable_dist_wtd_comp ||
+              cpi->sf.use_dist_wtd_comp_flag == DIST_WTD_COMP_DISABLED ||
+              mbmi->mode == GLOBAL_GLOBALMV)
+            continue;
+          mbmi->interinter_comp.type = COMPOUND_DISTWTD;
+          mbmi->num_proj_ref = 0;
+          mbmi->motion_mode = SIMPLE_TRANSLATION;
+          mbmi->comp_group_idx = 0;
+          mbmi->compound_idx = 0;
           const int comp_index_ctx = get_comp_index_context(cm, xd);
           compmode_interinter_cost +=
               x->comp_idx_cost[comp_index_ctx][mbmi->compound_idx];
@@ -10231,9 +10241,10 @@
 
           int64_t best_rd_compound;
           compmode_interinter_cost = compound_type_rd(
-              cpi, x, bsize, mi_col, mi_row, cur_mv, mode_search_mask,
-              masked_compound_used, &orig_dst, &tmp_dst, rd_buffers, &rate_mv,
-              &best_rd_compound, rd_stats, ref_best_rd, &is_luma_interp_done);
+              cpi, x, bsize, mi_col, mi_row, cur_mv,
+              mode_search_mask[comp_loop_idx], masked_compound_used, &orig_dst,
+              &tmp_dst, rd_buffers, &rate_mv, &best_rd_compound, rd_stats,
+              ref_best_rd, &is_luma_interp_done);
           if (ref_best_rd < INT64_MAX &&
               (best_rd_compound >> 4) *
                       (11 + 2 * cpi->sf.two_loop_comp_search) >
@@ -10296,7 +10307,7 @@
                                       0, av1_num_planes(cm) - 1);
       }
 
-      if (cpi->sf.second_loop_comp_fast_tx_search && comp_idx == 0) {
+      if (cpi->sf.second_loop_comp_fast_tx_search && comp_loop_idx == 1) {
         // TODO(chengchen): this speed feature introduces big loss.
         // Need better estimation of rate distortion.
         int dummy_rate;