Unify NRS and non-NRS global motion functions and DS

This removes code redundancies for NRS in global motion.

Data structures:
  global_motion_used
  params_cost
  num_ref_frames
  reference_frames
  ref_buf

Functions:
  av1_recode_loop_test_global_motion
  calculate_gm_ref_params_scaling_distance
  find_gm_ref_params
  av1_compute_gm_for_valid_ref_frames
  compute_global_motion_for_references
  update_valid_ref_frames_for_gm
  write_global_motion
  read_global_motion

Change-Id: I4616f7e38c0dda04083deaedebce8568f69183d4
diff --git a/av1/common/av1_common_int.h b/av1/common/av1_common_int.h
index d87ca9a..533857c 100644
--- a/av1/common/av1_common_int.h
+++ b/av1/common/av1_common_int.h
@@ -1448,42 +1448,6 @@
   }
 }
 
-#if CONFIG_NEW_REF_SIGNALING
-static INLINE int calculate_gm_ref_params_scaling_distance_nrs(
-    const AV1_COMMON *const cm, int frame) {
-  const RefCntBuffer *const buf = get_ref_frame_buf(cm, frame);
-  const int ref_order_hint = buf ? (int)buf->order_hint : -1;
-  if (ref_order_hint < 0) return 0;
-  return get_relative_dist(&cm->seq_params.order_hint_info, ref_order_hint,
-                           cm->cur_frame->order_hint);
-}
-
-static INLINE bool find_gm_ref_params_nrs(WarpedMotionParams *ref_params,
-                                          const AV1_COMMON *const cm,
-                                          int cur_frame, int base_frame) {
-  if (base_frame < 0) return false;
-
-  memcpy(ref_params, &cm->global_motion[base_frame],
-         sizeof(WarpedMotionParams));
-
-  double scale_factor;
-  const int distance =
-      calculate_gm_ref_params_scaling_distance_nrs(cm, cur_frame);
-  const int base = calculate_gm_ref_params_scaling_distance_nrs(cm, base_frame);
-  if (base != 0 && distance != 0)
-    scale_factor = (double)distance / base;
-  else
-    return 0;
-
-  for (int i = 0; i < 8; ++i) {
-    ref_params->wmmat[i] = (int32_t)(ref_params->wmmat[i] * scale_factor);
-  }
-  check_wmmat(ref_params);
-  return true;
-}
-
-#else
-
 static INLINE int calculate_gm_ref_params_scaling_distance(
     const AV1_COMMON *const cm, int frame) {
   const RefCntBuffer *const buf = get_ref_frame_buf(cm, frame);
@@ -1515,7 +1479,6 @@
   check_wmmat(ref_params);
   return true;
 }
-#endif  // CONFIG_NEW_REF_SIGNALING
 #endif  // CONFIG_GM_MODEL_CODING
 
 // Returns 1 if this frame might allow mvs from some reference frame.
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 9d62220..9137cc8 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -4837,58 +4837,17 @@
   return 1;
 }
 
-#if CONFIG_NEW_REF_SIGNALING
-static AOM_INLINE void read_global_motion_nrs(AV1_COMMON *cm,
-                                              struct aom_read_bit_buffer *rb) {
-#if CONFIG_GM_MODEL_CODING
-  int base_frame = -1;
-  int use_gm_k = 0;
-#endif  // CONFIG_GM_MODEL_CODING
-  for (int frame = 0; frame < cm->new_ref_frame_data.n_total_refs; ++frame) {
-    const WarpedMotionParams *ref_params;
-#if CONFIG_GM_MODEL_CODING
-    WarpedMotionParams params;
-    aom_clear_system_state();
-    const bool updated_params =
-        find_gm_ref_params_nrs(&params, cm, frame, base_frame);
-    if (updated_params) {
-      ref_params = &params;
-    } else {
-      ref_params = cm->prev_frame ? &cm->prev_frame->global_motion[frame]
-                                  : &default_warp_params;
-    }
-    use_gm_k = (base_frame != -1) ? 1 : 0;
-    if (ref_params->wmtype != IDENTITY) base_frame = frame;
-#else
-    ref_params = cm->prev_frame ? &cm->prev_frame->global_motion[frame]
-                                : &default_warp_params;
-#endif  // CONFIG_GM_MODEL_CODING
-    int good_params =
-        read_global_motion_params(&cm->global_motion[frame], ref_params,
-#if CONFIG_GM_MODEL_CODING
-                                  use_gm_k,
-#endif  // CONFIG_GM_MODEL_CODING
-                                  rb, cm->features.fr_mv_precision);
-    if (!good_params) {
-#if WARPED_MOTION_DEBUG
-      printf("Warning: unexpected global motion shear params from aomenc\n");
-#endif
-      cm->global_motion[frame].invalid = 1;
-    }
-  }
-  memcpy(cm->cur_frame->global_motion, cm->global_motion,
-         INTER_REFS_PER_FRAME_NRS * sizeof(WarpedMotionParams));
-}
-
-#else
-
 static AOM_INLINE void read_global_motion(AV1_COMMON *cm,
                                           struct aom_read_bit_buffer *rb) {
 #if CONFIG_GM_MODEL_CODING
   int base_frame = -1;
   int use_gm_k = 0;
 #endif  // CONFIG_GM_MODEL_CODING
+#if CONFIG_NEW_REF_SIGNALING
+  for (int frame = 0; frame < cm->new_ref_frame_data.n_total_refs; ++frame) {
+#else
   for (int frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
+#endif  // CONFIG_NEW_REF_SIGNALING
     const WarpedMotionParams *ref_params;
 #if CONFIG_GM_MODEL_CODING
     WarpedMotionParams params;
@@ -4943,10 +4902,14 @@
            cm->global_motion[frame].wmmat[3]);
            */
   }
+#if CONFIG_NEW_REF_SIGNALING
+  memcpy(cm->cur_frame->global_motion, cm->global_motion,
+         INTER_REFS_PER_FRAME_NRS * sizeof(WarpedMotionParams));
+#else
   memcpy(cm->cur_frame->global_motion, cm->global_motion,
          REF_FRAMES * sizeof(WarpedMotionParams));
-}
 #endif  // CONFIG_NEW_REF_SIGNALING
+}
 
 // Release the references to the frame buffers in cm->ref_frame_map and reset
 // all elements of cm->ref_frame_map to NULL.
@@ -5781,11 +5744,7 @@
                        "Frame wrongly requests reference frame MVs");
   }
 
-#if CONFIG_NEW_REF_SIGNALING
-  if (!frame_is_intra_only(cm)) read_global_motion_nrs(cm, rb);
-#else
   if (!frame_is_intra_only(cm)) read_global_motion(cm, rb);
-#endif  // CONFIG_NEW_REF_SIGNALING
 
   cm->cur_frame->film_grain_params_present =
       seq_params->film_grain_params_present;
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index bf5772b..12805f6 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -3304,45 +3304,6 @@
   }
 }
 
-#if CONFIG_NEW_REF_SIGNALING
-static AOM_INLINE void write_global_motion_nrs(
-    AV1_COMP *cpi, struct aom_write_bit_buffer *wb) {
-  AV1_COMMON *const cm = &cpi->common;
-  int frame;
-#if CONFIG_GM_MODEL_CODING
-  int base_frame = -1;
-  int use_gm_k = 0;
-#endif  // CONFIG_GM_MODEL_CODING
-  for (frame = 0; frame < cm->new_ref_frame_data.n_total_refs; ++frame) {
-    const WarpedMotionParams *ref_params;
-#if CONFIG_GM_MODEL_CODING
-    WarpedMotionParams params;
-    aom_clear_system_state();
-    const bool updated_params =
-        find_gm_ref_params_nrs(&params, cm, frame, base_frame);
-    if (updated_params) {
-      ref_params = &params;
-      use_gm_k = 1;
-    } else {
-      ref_params = cm->prev_frame ? &cm->prev_frame->global_motion[frame]
-                                  : &default_warp_params;
-      use_gm_k = 0;
-    }
-    if (ref_params->wmtype != IDENTITY) base_frame = frame;
-#else
-    ref_params = cm->prev_frame ? &cm->prev_frame->global_motion[frame]
-                                : &default_warp_params;
-#endif  // CONFIG_GM_MODEL_CODING
-    write_global_motion_params(&cm->global_motion[frame], ref_params,
-#if CONFIG_GM_MODEL_CODING
-                               use_gm_k,
-#endif  // CONFIG_GM_MODEL_CODING
-                               wb, cm->features.fr_mv_precision);
-  }
-}
-
-#else
-
 static AOM_INLINE void write_global_motion(AV1_COMP *cpi,
                                            struct aom_write_bit_buffer *wb) {
   AV1_COMMON *const cm = &cpi->common;
@@ -3351,7 +3312,11 @@
   int base_frame = -1;
   int use_gm_k = 0;
 #endif  // CONFIG_GM_MODEL_CODING
+#if CONFIG_NEW_REF_SIGNALING
+  for (frame = 0; frame < cm->new_ref_frame_data.n_total_refs; ++frame) {
+#else
   for (frame = LAST_FRAME; frame <= ALTREF_FRAME; ++frame) {
+#endif
     const WarpedMotionParams *ref_params;
 #if CONFIG_GM_MODEL_CODING
     WarpedMotionParams params;
@@ -3400,7 +3365,6 @@
            */
   }
 }
-#endif  // CONFIG_NEW_REF_SIGNALING
 
 #if !CONFIG_NEW_REF_SIGNALING
 static int check_frame_refs_short_signaling(AV1_COMMON *const cm) {
@@ -3807,11 +3771,7 @@
 
   aom_wb_write_bit(wb, features->reduced_tx_set_used);
 
-#if CONFIG_NEW_REF_SIGNALING
-  if (!frame_is_intra_only(cm)) write_global_motion_nrs(cpi, wb);
-#else
   if (!frame_is_intra_only(cm)) write_global_motion(cpi, wb);
-#endif  // CONFIG_NEW_REF_SIGNALING
 
   if (seq_params->film_grain_params_present &&
       (cm->show_frame || cm->showable_frame))
diff --git a/av1/encoder/encodeframe_utils.h b/av1/encoder/encodeframe_utils.h
index 078b113..44508b2 100644
--- a/av1/encoder/encodeframe_utils.h
+++ b/av1/encoder/encodeframe_utils.h
@@ -178,7 +178,7 @@
     int ref;
     for (ref = 0; ref < 1 + has_second_ref(mbmi); ++ref) {
 #if CONFIG_NEW_REF_SIGNALING
-      rdc->global_motion_used_nrs[mbmi->ref_frame_nrs[ref]] += num_4x4s;
+      rdc->global_motion_used[mbmi->ref_frame_nrs[ref]] += num_4x4s;
 #else
       rdc->global_motion_used[mbmi->ref_frame[ref]] += num_4x4s;
 #endif  // CONFIG_NEW_REF_SIGNALING
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 35c8f33..5456cab 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -2605,17 +2605,10 @@
       loop = 0;
     }
 
-#if CONFIG_NEW_REF_SIGNALING
-    if (allow_recode && !cpi->sf.gm_sf.gm_disable_recode &&
-        av1_recode_loop_test_global_motion_nrs(
-            cm->global_motion, cpi->td.rd_counts.global_motion_used,
-            gm_info->params_cost_nrs)) {
-#else
     if (allow_recode && !cpi->sf.gm_sf.gm_disable_recode &&
         av1_recode_loop_test_global_motion(cm->global_motion,
                                            cpi->td.rd_counts.global_motion_used,
                                            gm_info->params_cost)) {
-#endif  // CONFIG_NEW_REF_SIGNALING
       loop = 1;
     }
 
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 591dc2b..b6284ea 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -1417,9 +1417,10 @@
   int64_t comp_pred_diff[REFERENCE_MODES];
   // Stores number of 4x4 blocks using global motion per reference frame.
 #if CONFIG_NEW_REF_SIGNALING
-  int global_motion_used_nrs[INTER_REFS_PER_FRAME_NRS];
-#endif  // CONFIG_NEW_REF_SIGNALING
+  int global_motion_used[INTER_REFS_PER_FRAME_NRS];
+#else
   int global_motion_used[REF_FRAMES];
+#endif  // CONFIG_NEW_REF_SIGNALING
   int compound_ref_used_flag;
   int skip_mode_used_flag;
   int tx_type_used[TX_SIZES_ALL][TX_TYPES];
@@ -1735,65 +1736,49 @@
    */
   int type_cost[TRANS_TYPES];
 
+  /*!
+   * Array to store the cost for signalling a particular global motion model for
+   * each reference frame. gmparams_cost[i] stores the cost of signalling global
+   * motion for the ith reference frame.
+   */
 #if CONFIG_NEW_REF_SIGNALING
-  /*!
-   * Array to store the cost for signalling a particular global motion model for
-   * each reference frame. gmparams_cost[i] stores the cost of signalling global
-   * motion for the ith reference frame.
-   */
-  int params_cost_nrs[INTER_REFS_PER_FRAME_NRS];
-#endif  // CONFIG_NEW_REF_SIGNALING
-
-  /*!
-   * Array to store the cost for signalling a particular global motion model for
-   * each reference frame. gmparams_cost[i] stores the cost of signalling global
-   * motion for the ith reference frame.
-   */
+  int params_cost[INTER_REFS_PER_FRAME_NRS];
+#else
   int params_cost[REF_FRAMES];
+#endif  // CONFIG_NEW_REF_SIGNALING
 
   /*!
    * Flag to indicate if global motion search needs to be rerun.
    */
   bool search_done;
 
-#if CONFIG_NEW_REF_SIGNALING
   /*!
    * Array of pointers to the frame buffers holding the nrs reference frames.
    * ref_buf[i] stores the pointer to the reference frame of the ith
    * reference frame type.
    */
-  YV12_BUFFER_CONFIG *ref_buf_nrs[INTER_REFS_PER_FRAME_NRS];
-#endif  // CONFIG_NEW_REF_SIGNALING
-
-  /*!
-   * Array of pointers to the frame buffers holding the reference frames.
-   * ref_buf[i] stores the pointer to the reference frame of the ith
-   * reference frame type.
-   */
+#if CONFIG_NEW_REF_SIGNALING
+  YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS];
+#else
   YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES];
+#endif  // CONFIG_NEW_REF_SIGNALING
 
   /*!
    * Pointer to the source frame buffer.
    */
   unsigned char *src_buffer;
 
-#if CONFIG_NEW_REF_SIGNALING
   /*!
    * Array of structure which stores the valid reference frames from
    * new ref signaling experiment in past and
    * future directions and their corresponding distance from the source frame.
    * reference_frames[i][j] holds the jth valid reference frame type in the
-   * direction 'i' and its temporal distance from the source frame .
+   * direction 'i' and its temporal distance from the source frame.
    */
-  FrameDistPair reference_frames_nrs[MAX_DIRECTIONS][INTER_REFS_PER_FRAME_NRS];
-
-  /*!
-   * Holds the number of valid reference frames for new ref signaling expt
-   * in past and future directions
-   * w.r.t. the current frame. num_ref_frames_nrs[i] stores the total number of
-   * valid reference frames in 'i' direction.
-   */
-  int num_ref_frames_nrs[MAX_DIRECTIONS];
+#if CONFIG_NEW_REF_SIGNALING
+  FrameDistPair reference_frames[MAX_DIRECTIONS][INTER_REFS_PER_FRAME_NRS];
+#else
+  FrameDistPair reference_frames[MAX_DIRECTIONS][REF_FRAMES - 1];
 #endif  // CONFIG_NEW_REF_SIGNALING
 
   /*!
@@ -1803,14 +1788,6 @@
    */
   int num_ref_frames[MAX_DIRECTIONS];
 
-  /*!
-   * Array of structure which stores the valid reference frames in past and
-   * future directions and their corresponding distance from the source frame.
-   * reference_frames[i][j] holds the jth valid reference frame type in the
-   * direction 'i' and its temporal distance from the source frame .
-   */
-  FrameDistPair reference_frames[MAX_DIRECTIONS][REF_FRAMES - 1];
-
   /**
    * \name Dimensions for which segment map is allocated.
    */
diff --git a/av1/encoder/encoder_utils.c b/av1/encoder/encoder_utils.c
index 1e1dd09..af91143 100644
--- a/av1/encoder/encoder_utils.c
+++ b/av1/encoder/encoder_utils.c
@@ -881,35 +881,16 @@
 }
 
 #define GM_RECODE_LOOP_NUM4X4_FACTOR 192
-#if CONFIG_NEW_REF_SIGNALING
-int av1_recode_loop_test_global_motion_nrs(
-    WarpedMotionParams *const global_motion,
-    const int *const global_motion_used, int *const gm_params_cost) {
-  int i;
-  int recode = 0;
-  for (i = 0; i < INTER_REFS_PER_FRAME_NRS; ++i) {
-    if (global_motion[i].wmtype != IDENTITY &&
-        global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
-            gm_params_cost[i]) {
-      global_motion[i] = default_warp_params;
-      assert(global_motion[i].wmtype == IDENTITY);
-      gm_params_cost[i] = 0;
-      recode = 1;
-      // TODO(sarahparker): The earlier condition for recoding here was:
-      // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
-      // similar to that back to speed up global motion?
-    }
-  }
-  return recode;
-}
-#endif  // CONFIG_NEW_REF_SIGNALING
-
 int av1_recode_loop_test_global_motion(WarpedMotionParams *const global_motion,
                                        const int *const global_motion_used,
                                        int *const gm_params_cost) {
   int i;
   int recode = 0;
+#if CONFIG_NEW_REF_SIGNALING
+  for (i = 0; i < INTER_REFS_PER_FRAME_NRS; ++i) {
+#else
   for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
+#endif  // CONFIG_NEW_REF_SIGNALING
     if (global_motion[i].wmtype != IDENTITY &&
         global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
             gm_params_cost[i]) {
diff --git a/av1/encoder/encoder_utils.h b/av1/encoder/encoder_utils.h
index afa6bc4..5dbe86e 100644
--- a/av1/encoder/encoder_utils.h
+++ b/av1/encoder/encoder_utils.h
@@ -974,12 +974,6 @@
 
 void av1_determine_sc_tools_with_encoding(AV1_COMP *cpi, const int q_orig);
 
-#if CONFIG_NEW_REF_SIGNALING
-int av1_recode_loop_test_global_motion_nrs(
-    WarpedMotionParams *const global_motion,
-    const int *const global_motion_used, int *const gm_params_cost);
-#endif  // CONFIG_NEW_REF_SIGNALING
-
 int av1_recode_loop_test_global_motion(WarpedMotionParams *const global_motion,
                                        const int *const global_motion_used,
                                        int *const gm_params_cost);
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index fc25afe..713fc8c 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -28,7 +28,11 @@
   for (int i = 0; i < REFERENCE_MODES; i++)
     td->rd_counts.comp_pred_diff[i] += td_t->rd_counts.comp_pred_diff[i];
 
+#if CONFIG_NEW_REF_SIGNALING
+  for (int i = 0; i < INTER_REFS_PER_FRAME_NRS; i++)
+#else
   for (int i = 0; i < REF_FRAMES; i++)
+#endif  // CONFIG_NEW_REF_SIGNALING
     td->rd_counts.global_motion_used[i] +=
         td_t->rd_counts.global_motion_used[i];
 
@@ -1411,20 +1415,18 @@
 
     init_gm_thread_data(gm_info, gm_thread_data);
 
+#if CONFIG_GM_MODEL_CODING
+    int base_frame = -1;
+#endif  // CONFIG_GM_MODEL_CODING
     // Compute global motion for the given ref_buf_idx.
-#if CONFIG_NEW_REF_SIGNALING
-    av1_compute_gm_for_valid_ref_frames_nrs(
-        cpi, gm_info->ref_buf, ref_buf_idx, gm_info->num_src_corners,
-        gm_info->src_corners, gm_info->src_buffer,
-        gm_thread_data->params_by_motion, gm_thread_data->segment_map,
-        gm_info->segment_map_w, gm_info->segment_map_h);
-#else
     av1_compute_gm_for_valid_ref_frames(
-        cpi, gm_info->ref_buf, ref_buf_idx, gm_info->num_src_corners,
-        gm_info->src_corners, gm_info->src_buffer,
+        cpi, gm_info->ref_buf, ref_buf_idx,
+#if CONFIG_GM_MODEL_CODING
+        &base_frame,
+#endif  // CONFIG_GM_MODEL_CODING
+        gm_info->num_src_corners, gm_info->src_corners, gm_info->src_buffer,
         gm_thread_data->params_by_motion, gm_thread_data->segment_map,
         gm_info->segment_map_w, gm_info->segment_map_h);
-#endif  // CONFIG_NEW_REF_SIGNALING
 
 #if CONFIG_MULTITHREAD
     pthread_mutex_lock(gm_mt_mutex_);
diff --git a/av1/encoder/global_motion_facade.c b/av1/encoder/global_motion_facade.c
index 217505f..8193339 100644
--- a/av1/encoder/global_motion_facade.c
+++ b/av1/encoder/global_motion_facade.c
@@ -84,143 +84,16 @@
   return (int64_t)(ref_frame_error * erroradv_tr + 0.5);
 }
 
-#if CONFIG_NEW_REF_SIGNALING
-// For the given reference frame, computes the global motion parameters for
-// different motion models and finds the best.
-static AOM_INLINE void compute_global_motion_for_ref_frame_nrs(
-    AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS],
-    int frame,
-#if CONFIG_GM_MODEL_CODING
-    int use_gm_k,
-#endif  // CONFIG_GM_MODEL_CODING
-    int num_src_corners, int *src_corners, unsigned char *src_buffer,
-    MotionModel *params_by_motion, uint8_t *segment_map,
-    const int segment_map_w, const int segment_map_h,
-    const WarpedMotionParams *ref_params) {
-  ThreadData *const td = &cpi->td;
-  MACROBLOCK *const x = &td->mb;
-  AV1_COMMON *const cm = &cpi->common;
-  MACROBLOCKD *const xd = &x->e_mbd;
-  int i;
-  int src_width = cpi->source->y_width;
-  int src_height = cpi->source->y_height;
-  int src_stride = cpi->source->y_stride;
-  // clang-format off
-  static const double kIdentityParams[MAX_PARAMDIM - 1] = {
-     0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0
-  };
-  // clang-format on
-  WarpedMotionParams tmp_wm_params;
-  const double *params_this_motion;
-  int inliers_by_motion[RANSAC_NUM_MOTIONS];
-  assert(ref_buf[frame] != NULL);
-  TransformationType model;
-
-  aom_clear_system_state();
-
-  // TODO(sarahparker, debargha): Explore do_adaptive_gm_estimation = 1
-  const int do_adaptive_gm_estimation = 0;
-
-  const int ref_frame_dist = get_relative_dist(
-      &cm->seq_params.order_hint_info, cm->current_frame.order_hint,
-      cm->cur_frame->ref_order_hints[frame]);
-  const GlobalMotionEstimationType gm_estimation_type =
-      cm->seq_params.order_hint_info.enable_order_hint &&
-              abs(ref_frame_dist) <= 2 && do_adaptive_gm_estimation
-          ? GLOBAL_MOTION_DISFLOW_BASED
-          : GLOBAL_MOTION_FEATURE_BASED;
-  for (model = ROTZOOM; model < GLOBAL_TRANS_TYPES_ENC; ++model) {
-    int64_t best_warp_error = INT64_MAX;
-    // Initially set all params to identity.
-    for (i = 0; i < RANSAC_NUM_MOTIONS; ++i) {
-      memcpy(params_by_motion[i].params, kIdentityParams,
-             (MAX_PARAMDIM - 1) * sizeof(*(params_by_motion[i].params)));
-      params_by_motion[i].num_inliers = 0;
-    }
-
-    av1_compute_global_motion(model, src_buffer, src_width, src_height,
-                              src_stride, src_corners, num_src_corners,
-                              ref_buf[frame], cpi->common.seq_params.bit_depth,
-                              gm_estimation_type, inliers_by_motion,
-                              params_by_motion, RANSAC_NUM_MOTIONS);
-    int64_t ref_frame_error = 0;
-    for (i = 0; i < RANSAC_NUM_MOTIONS; ++i) {
-      if (inliers_by_motion[i] == 0) continue;
-
-      params_this_motion = params_by_motion[i].params;
-      av1_convert_model_to_params(params_this_motion, &tmp_wm_params);
-
-      if (tmp_wm_params.wmtype != IDENTITY) {
-        av1_compute_feature_segmentation_map(
-            segment_map, segment_map_w, segment_map_h,
-            params_by_motion[i].inliers, params_by_motion[i].num_inliers);
-
-        ref_frame_error = av1_segmented_frame_error(
-            is_cur_buf_hbd(xd), xd->bd, ref_buf[frame]->y_buffer,
-            ref_buf[frame]->y_stride, cpi->source->y_buffer, src_width,
-            src_height, src_stride, segment_map, segment_map_w);
-
-        const int64_t erroradv_threshold =
-            calc_erroradv_threshold(ref_frame_error);
-
-        const int64_t warp_error = av1_refine_integerized_param(
-            &tmp_wm_params, tmp_wm_params.wmtype, is_cur_buf_hbd(xd), xd->bd,
-            ref_buf[frame]->y_buffer, ref_buf[frame]->y_width,
-            ref_buf[frame]->y_height, ref_buf[frame]->y_stride,
-            cpi->source->y_buffer, src_width, src_height, src_stride,
-            GM_REFINEMENT_COUNT, best_warp_error, segment_map, segment_map_w,
-            erroradv_threshold);
-
-        if (warp_error < best_warp_error) {
-          best_warp_error = warp_error;
-          // Save the wm_params modified by
-          // av1_refine_integerized_param() rather than motion index to
-          // avoid rerunning refine() below.
-          memcpy(&(cm->global_motion[frame]), &tmp_wm_params,
-                 sizeof(WarpedMotionParams));
-        }
-      }
-    }
-    if (cm->global_motion[frame].wmtype <= AFFINE)
-      if (!av1_get_shear_params(&cm->global_motion[frame]))
-        cm->global_motion[frame] = default_warp_params;
-
-    if (cm->global_motion[frame].wmtype == TRANSLATION) {
-      cm->global_motion[frame].wmmat[0] =
-          convert_to_trans_prec(cm->features.fr_mv_precision,
-                                cm->global_motion[frame].wmmat[0]) *
-          GM_TRANS_ONLY_DECODE_FACTOR;
-      cm->global_motion[frame].wmmat[1] =
-          convert_to_trans_prec(cm->features.fr_mv_precision,
-                                cm->global_motion[frame].wmmat[1]) *
-          GM_TRANS_ONLY_DECODE_FACTOR;
-    }
-
-    if (cm->global_motion[frame].wmtype == IDENTITY) continue;
-
-    if (ref_frame_error == 0) continue;
-
-    // If the best error advantage found doesn't meet the threshold for
-    // this motion type, revert to IDENTITY.
-    if (!av1_is_enough_erroradvantage(
-            (double)best_warp_error / ref_frame_error,
-            gm_get_params_cost(&cm->global_motion[frame], ref_params,
-#if CONFIG_GM_MODEL_CODING
-                               use_gm_k,
-#endif  // CONFIG_GM_MODEL_CODING
-                               cm->features.fr_mv_precision))) {
-      cm->global_motion[frame] = default_warp_params;
-    }
-    if (cm->global_motion[frame].wmtype != IDENTITY) break;
-  }
-
-  aom_clear_system_state();
-}
-#else
 // For the given reference frame, computes the global motion parameters for
 // different motion models and finds the best.
 static AOM_INLINE void compute_global_motion_for_ref_frame(
-    AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES], int frame,
+    AV1_COMP *cpi,
+#if CONFIG_NEW_REF_SIGNALING
+    YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS],
+#else
+    YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES],
+#endif  // CONFIG_NEW_REF_SIGNALING
+    int frame,
 #if CONFIG_GM_MODEL_CODING
     int use_gm_k,
 #endif  // CONFIG_GM_MODEL_CODING
@@ -252,9 +125,15 @@
   // TODO(sarahparker, debargha): Explore do_adaptive_gm_estimation = 1
   const int do_adaptive_gm_estimation = 0;
 
+#if CONFIG_NEW_REF_SIGNALING
+  const int ref_frame_dist = get_relative_dist(
+      &cm->seq_params.order_hint_info, cm->current_frame.order_hint,
+      cm->cur_frame->ref_order_hints[frame]);
+#else
   const int ref_frame_dist = get_relative_dist(
       &cm->seq_params.order_hint_info, cm->current_frame.order_hint,
       cm->cur_frame->ref_order_hints[frame - LAST_FRAME]);
+#endif  // CONFIG_NEW_REF_SIGNALING
   const GlobalMotionEstimationType gm_estimation_type =
       cm->seq_params.order_hint_info.enable_order_hint &&
               abs(ref_frame_dist) <= 2 && do_adaptive_gm_estimation
@@ -347,62 +226,16 @@
 
   aom_clear_system_state();
 }
-#endif  // CONFIG_NEW_REF_SIGNALING
 
-#if CONFIG_NEW_REF_SIGNALING
-// Computes global motion for the given reference frame.
-void av1_compute_gm_for_valid_ref_frames_nrs(
-    AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS],
-    int frame,
-#if CONFIG_GM_MODEL_CODING
-    int *base_frame,
-#endif  // CONFIG_GM_MODEL_CODING
-    int num_src_corners, int *src_corners, unsigned char *src_buffer,
-    MotionModel *params_by_motion, uint8_t *segment_map, int segment_map_w,
-    int segment_map_h) {
-  AV1_COMMON *const cm = &cpi->common;
-  GlobalMotionInfo *const gm_info = &cpi->gm_info;
-  const WarpedMotionParams *ref_params;
-#if CONFIG_GM_MODEL_CODING
-  int use_gm_k = 0;
-  WarpedMotionParams params;
-  aom_clear_system_state();
-  const bool updated_params =
-      find_gm_ref_params_nrs(&params, cm, frame, *base_frame);
-  if (updated_params) {
-    ref_params = &params;
-    use_gm_k = 1;
-  } else {
-    ref_params = cm->prev_frame ? &cm->prev_frame->global_motion[frame]
-                                : &default_warp_params;
-    use_gm_k = 0;
-  }
-  if (ref_params->wmtype != IDENTITY) *base_frame = frame;
-#else
-  ref_params = cm->prev_frame ? &cm->prev_frame->global_motion[frame]
-                              : &default_warp_params;
-#endif  // CONFIG_GM_MODEL_CODING
-  compute_global_motion_for_ref_frame_nrs(
-      cpi, ref_buf, frame,
-#if CONFIG_GM_MODEL_CODING
-      use_gm_k,
-#endif  // CONFIG_GM_MODEL_CODING
-      num_src_corners, src_corners, src_buffer, params_by_motion, segment_map,
-      segment_map_w, segment_map_h, ref_params);
-
-  gm_info->params_cost_nrs[frame] =
-      gm_get_params_cost(&cm->global_motion[frame], ref_params,
-#if CONFIG_GM_MODEL_CODING
-                         use_gm_k,
-#endif  // CONFIG_GM_MODEL_CODING
-                         cm->features.fr_mv_precision) +
-      gm_info->type_cost[cm->global_motion[frame].wmtype] -
-      gm_info->type_cost[IDENTITY];
-}
-#else
 // Computes global motion for the given reference frame.
 void av1_compute_gm_for_valid_ref_frames(
-    AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES], int frame,
+    AV1_COMP *cpi,
+#if CONFIG_NEW_REF_SIGNALING
+    YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS],
+#else
+    YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES],
+#endif  // CONFIG_NEW_REF_SIGNALING
+    int frame,
 #if CONFIG_GM_MODEL_CODING
     int *base_frame,
 #endif  // CONFIG_GM_MODEL_CODING
@@ -448,52 +281,20 @@
       gm_info->type_cost[cm->global_motion[frame].wmtype] -
       gm_info->type_cost[IDENTITY];
 }
-#endif  // CONFIG_NEW_REF_SIGNALING
 
-#if CONFIG_NEW_REF_SIGNALING
-// Loops over valid reference frames and computes global motion estimation.
-static AOM_INLINE void compute_global_motion_for_references_nrs(
-    AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS],
-    FrameDistPair reference_frame[INTER_REFS_PER_FRAME_NRS], int num_ref_frames,
-    int num_src_corners, int *src_corners, unsigned char *src_buffer,
-    MotionModel *params_by_motion, uint8_t *segment_map,
-    const int segment_map_w, const int segment_map_h) {
-  // Computation of frame corners for the source frame will be done already.
-  assert(num_src_corners != -1);
-  AV1_COMMON *const cm = &cpi->common;
-#if CONFIG_GM_MODEL_CODING
-  int base_frame = -1;
-#endif  // CONFIG_GM_MODEL_CODING
-  // Compute global motion w.r.t. reference frames starting from the nearest ref
-  // frame in a given direction.
-  for (int frame = 0; frame < num_ref_frames; frame++) {
-    int ref_frame = reference_frame[frame].frame;
-    av1_compute_gm_for_valid_ref_frames_nrs(
-        cpi, ref_buf, ref_frame,
-#if CONFIG_GM_MODEL_CODING
-        &base_frame,
-#endif  // CONFIG_GM_MODEL_CODING
-        num_src_corners, src_corners, src_buffer, params_by_motion, segment_map,
-        segment_map_w, segment_map_h);
-    // If global motion w.r.t. current ref frame is
-    // INVALID/TRANSLATION/IDENTITY, skip the evaluation of global motion w.r.t
-    // the remaining ref frames in that direction. The below exit is disabled
-    // when ref frame distance w.r.t. current frame is zero. E.g.:
-    // source_alt_ref_frame w.r.t. ARF frames.
-    if (cpi->sf.gm_sf.prune_ref_frame_for_gm_search &&
-        reference_frame[frame].distance != 0 &&
-        cm->global_motion[ref_frame].wmtype != ROTZOOM)
-      break;
-  }
-}
-#else
 // Loops over valid reference frames and computes global motion estimation.
 static AOM_INLINE void compute_global_motion_for_references(
-    AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES],
-    FrameDistPair reference_frame[REF_FRAMES - 1], int num_ref_frames,
-    int num_src_corners, int *src_corners, unsigned char *src_buffer,
-    MotionModel *params_by_motion, uint8_t *segment_map,
-    const int segment_map_w, const int segment_map_h) {
+    AV1_COMP *cpi,
+#if CONFIG_NEW_REF_SIGNALING
+    YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS],
+    FrameDistPair reference_frame[INTER_REFS_PER_FRAME_NRS],
+#else
+    YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES],
+    FrameDistPair reference_frame[REF_FRAMES - 1],
+#endif  // CONFIG_NEW_REF_SIGNALING
+    int num_ref_frames, int num_src_corners, int *src_corners,
+    unsigned char *src_buffer, MotionModel *params_by_motion,
+    uint8_t *segment_map, const int segment_map_w, const int segment_map_h) {
   // Computation of frame corners for the source frame will be done already.
   assert(num_src_corners != -1);
   AV1_COMMON *const cm = &cpi->common;
@@ -522,7 +323,6 @@
       break;
   }
 }
-#endif  // CONFIG_NEW_REF_SIGNALING
 
 // Compares the distance in 'a' and 'b'. Returns 1 if the frame corresponding to
 // 'a' is farther, -1 if the frame corresponding to 'b' is farther, 0 otherwise.
@@ -567,75 +367,7 @@
   }
   return 1;
 }
-
-// Populates valid reference frames in past/future directions in
-// 'reference_frames' and their count in 'num_ref_frames'.
-static AOM_INLINE void update_valid_ref_frames_for_gm_nrs(
-    AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS],
-    FrameDistPair reference_frames[MAX_DIRECTIONS][INTER_REFS_PER_FRAME_NRS],
-    int *num_ref_frames) {
-  AV1_COMMON *const cm = &cpi->common;
-  int *num_past_ref_frames = &num_ref_frames[0];
-  int *num_future_ref_frames = &num_ref_frames[1];
-  const GF_GROUP *gf_group = &cpi->gf_group;
-  int ref_pruning_enabled = is_frame_eligible_for_ref_pruning(
-      gf_group, cpi->sf.inter_sf.selective_ref_frame, 1, gf_group->index);
-
-  for (int frame = cm->new_ref_frame_data.n_total_refs - 1; frame >= 0;
-       --frame) {
-    const MV_REFERENCE_FRAME ref_frame[2] = { frame, INVALID_IDX };
-    // TODO(sarahparker) Get ref indices from old reference system to pass
-    // into functions that have not been converted.
-    // Remove this once supporting functions are converted
-    // to use new indexing.
-    ref_buf[frame] = NULL;
-    cm->global_motion[frame] = default_warp_params;
-    RefCntBuffer *buf = get_ref_frame_buf(cm, frame);
-    if (buf == NULL) {
-      cpi->gm_info.params_cost_nrs[frame] = 0;
-      continue;
-    }
-    const int ref_disabled = !(cpi->common.ref_frame_flags & (1 << frame));
-    // Skip global motion estimation for invalid ref frames
-    if ((ref_disabled && cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE)) {
-      cpi->gm_info.params_cost_nrs[frame] = 0;
-      continue;
-    } else {
-      ref_buf[frame] = &buf->buf;
-    }
-
-    int prune_ref_frames =
-        ref_pruning_enabled &&
-        prune_ref_by_selective_ref_frame_nrs(cpi, NULL, ref_frame);
-
-    if (ref_buf[frame]->y_crop_width == cpi->source->y_crop_width &&
-        ref_buf[frame]->y_crop_height == cpi->source->y_crop_height &&
-        do_gm_search_logic_nrs(&cpi->sf, ref_frame[0]) && !prune_ref_frames &&
-        !(cpi->sf.gm_sf.selective_ref_gm &&
-          skip_gm_frame_nrs(cm, ref_frame[0]))) {
-      assert(ref_buf[frame] != NULL);
-      const int relative_frame_dist = av1_encoder_get_relative_dist(
-          buf->display_order_hint, cm->cur_frame->display_order_hint);
-      // Populate past and future ref frames.
-      // reference_frames[0][] indicates past direction and
-      // reference_frames[1][] indicates future direction.
-      if (relative_frame_dist <= 0) {
-        reference_frames[0][*num_past_ref_frames].distance =
-            abs(relative_frame_dist);
-        reference_frames[0][*num_past_ref_frames].frame = frame;
-        (*num_past_ref_frames)++;
-      } else {
-        reference_frames[1][*num_future_ref_frames].distance =
-            abs(relative_frame_dist);
-        reference_frames[1][*num_future_ref_frames].frame = frame;
-        (*num_future_ref_frames)++;
-      }
-    }
-  }
-}
-
 #else
-
 // Function to decide if we can skip the global motion parameter computation
 // for a particular ref frame.
 static AOM_INLINE int skip_gm_frame(AV1_COMMON *const cm, int ref_frame) {
@@ -665,12 +397,19 @@
   }
   return 1;
 }
+#endif  // CONFIG_NEW_REF_SIGNALING
 
 // Populates valid reference frames in past/future directions in
 // 'reference_frames' and their count in 'num_ref_frames'.
 static AOM_INLINE void update_valid_ref_frames_for_gm(
-    AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES],
+    AV1_COMP *cpi,
+#if CONFIG_NEW_REF_SIGNALING
+    YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS],
+    FrameDistPair reference_frames[MAX_DIRECTIONS][INTER_REFS_PER_FRAME_NRS],
+#else
+    YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES],
     FrameDistPair reference_frames[MAX_DIRECTIONS][REF_FRAMES - 1],
+#endif  // CONFIG_NEW_REF_SIGNALING
     int *num_ref_frames) {
   AV1_COMMON *const cm = &cpi->common;
   int *num_past_ref_frames = &num_ref_frames[0];
@@ -679,13 +418,20 @@
   int ref_pruning_enabled = is_frame_eligible_for_ref_pruning(
       gf_group, cpi->sf.inter_sf.selective_ref_frame, 1, gf_group->index);
 
+#if CONFIG_NEW_REF_SIGNALING
+  for (int frame = cm->new_ref_frame_data.n_total_refs - 1; frame >= 0;
+       --frame) {
+    const MV_REFERENCE_FRAME ref_frame[2] = { frame, INVALID_IDX };
+    const int ref_disabled = !(cpi->common.ref_frame_flags & (1 << frame));
+#else
   for (int frame = ALTREF_FRAME; frame >= LAST_FRAME; --frame) {
     const MV_REFERENCE_FRAME ref_frame[2] = { frame, NONE_FRAME };
-    RefCntBuffer *buf = get_ref_frame_buf(cm, frame);
     const int ref_disabled =
         !(cpi->common.ref_frame_flags & av1_ref_frame_flag_list[frame]);
+#endif  // CONFIG_NEW_REF_SIGNALING
     ref_buf[frame] = NULL;
     cm->global_motion[frame] = default_warp_params;
+    RefCntBuffer *buf = get_ref_frame_buf(cm, frame);
     // Skip global motion estimation for invalid ref frames
     if (buf == NULL ||
         (ref_disabled && cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE)) {
@@ -695,15 +441,28 @@
       ref_buf[frame] = &buf->buf;
     }
 
+#if CONFIG_NEW_REF_SIGNALING
+    int prune_ref_frames =
+        ref_pruning_enabled &&
+        prune_ref_by_selective_ref_frame_nrs(cpi, NULL, ref_frame);
+#else
     int prune_ref_frames =
         ref_pruning_enabled &&
         prune_ref_by_selective_ref_frame(cpi, NULL, ref_frame,
                                          cm->cur_frame->ref_display_order_hint);
+#endif  // CONFIG_NEW_REF_SIGNALING
 
     if (ref_buf[frame]->y_crop_width == cpi->source->y_crop_width &&
         ref_buf[frame]->y_crop_height == cpi->source->y_crop_height &&
-        do_gm_search_logic(&cpi->sf, frame) && !prune_ref_frames &&
-        !(cpi->sf.gm_sf.selective_ref_gm && skip_gm_frame(cm, frame))) {
+#if CONFIG_NEW_REF_SIGNALING
+        do_gm_search_logic_nrs(&cpi->sf, ref_frame[0]) &&
+        !(cpi->sf.gm_sf.selective_ref_gm &&
+          skip_gm_frame_nrs(cm, ref_frame[0])) &&
+#else
+        do_gm_search_logic(&cpi->sf, frame) &&
+        !(cpi->sf.gm_sf.selective_ref_gm && skip_gm_frame(cm, frame)) &&
+#endif  // CONFIG_NEW_REF_SIGNALING
+        !prune_ref_frames) {
       assert(ref_buf[frame] != NULL);
       const int relative_frame_dist = av1_encoder_get_relative_dist(
           buf->display_order_hint, cm->cur_frame->display_order_hint);
@@ -724,7 +483,6 @@
     }
   }
 }
-#endif  // CONFIG_NEW_REF_SIGNALING
 
 // Allocates and initializes memory for segment_map and MotionModel.
 static AOM_INLINE void alloc_global_motion_data(MotionModel *params_by_motion,
@@ -771,37 +529,14 @@
       (source->y_height + WARP_ERROR_BLOCK) >> WARP_ERROR_BLOCK_LOG;
 
 #if CONFIG_NEW_REF_SIGNALING
-  memset(gm_info->reference_frames_nrs, -1,
-         sizeof(gm_info->reference_frames_nrs[0][0]) * MAX_DIRECTIONS *
+  memset(gm_info->reference_frames, -1,
+         sizeof(gm_info->reference_frames[0][0]) * MAX_DIRECTIONS *
              (INTER_REFS_PER_FRAME_NRS));
-  av1_zero(gm_info->num_ref_frames_nrs);
-
-  // Populate ref_buf for valid ref frames in global motion
-  update_valid_ref_frames_for_gm_nrs(cpi, gm_info->ref_buf_nrs,
-                                     gm_info->reference_frames_nrs,
-                                     gm_info->num_ref_frames_nrs);
-
-  // Sort the past and future ref frames in the ascending order of their
-  // distance from the current frame. reference_frames[0] => past direction
-  // and reference_frames[1] => future direction.
-  qsort(gm_info->reference_frames_nrs[0], gm_info->num_ref_frames_nrs[0],
-        sizeof(gm_info->reference_frames_nrs[0][0]), compare_distance);
-  qsort(gm_info->reference_frames_nrs[1], gm_info->num_ref_frames_nrs[1],
-        sizeof(gm_info->reference_frames_nrs[1][0]), compare_distance);
-
-  gm_info->num_src_corners = -1;
-  // If atleast one valid reference frame exists in past/future directions,
-  // compute interest points of source frame using FAST features.
-  if (gm_info->num_ref_frames_nrs[0] > 0 ||
-      gm_info->num_ref_frames_nrs[1] > 0) {
-    gm_info->num_src_corners = av1_fast_corner_detect(
-        gm_info->src_buffer, source->y_width, source->y_height,
-        source->y_stride, gm_info->src_corners, MAX_CORNERS);
-  }
 #else
   memset(gm_info->reference_frames, -1,
          sizeof(gm_info->reference_frames[0][0]) * MAX_DIRECTIONS *
              (REF_FRAMES - 1));
+#endif  // CONFIG_NEW_REF_SIGNALING
   av1_zero(gm_info->num_ref_frames);
 
   // Populate ref_buf for valid ref frames in global motion
@@ -825,38 +560,18 @@
         gm_info->src_buffer, source->y_width, source->y_height,
         source->y_stride, gm_info->src_corners, MAX_CORNERS);
   }
-#endif  // CONFIG_NEW_REF_SIGNALING
 }
 
 // Computes global motion w.r.t. valid reference frames.
 static AOM_INLINE void global_motion_estimation(AV1_COMP *cpi) {
   GlobalMotionInfo *const gm_info = &cpi->gm_info;
 
-#if CONFIG_NEW_REF_SIGNALING
-  MotionModel params_by_motion_nrs[RANSAC_NUM_MOTIONS];
-  uint8_t *segment_map_nrs = NULL;
-  alloc_global_motion_data(params_by_motion_nrs, &segment_map_nrs,
-                           gm_info->segment_map_w, gm_info->segment_map_h);
-#else
   MotionModel params_by_motion[RANSAC_NUM_MOTIONS];
   uint8_t *segment_map = NULL;
   alloc_global_motion_data(params_by_motion, &segment_map,
                            gm_info->segment_map_w, gm_info->segment_map_h);
-#endif  // CONFIG_NEW_REF_SIGNALING
   // Compute global motion w.r.t. past reference frames and future reference
   // frames
-#if CONFIG_NEW_REF_SIGNALING
-  for (int dir = 0; dir < MAX_DIRECTIONS; dir++) {
-    if (gm_info->num_ref_frames_nrs[dir] > 0) {
-      compute_global_motion_for_references_nrs(
-          cpi, gm_info->ref_buf_nrs, gm_info->reference_frames_nrs[dir],
-          gm_info->num_ref_frames_nrs[dir], gm_info->num_src_corners,
-          gm_info->src_corners, gm_info->src_buffer, params_by_motion_nrs,
-          segment_map_nrs, gm_info->segment_map_w, gm_info->segment_map_h);
-    }
-  }
-  dealloc_global_motion_data(params_by_motion_nrs, segment_map_nrs);
-#else
   for (int dir = 0; dir < MAX_DIRECTIONS; dir++) {
     if (gm_info->num_ref_frames[dir] > 0) {
       compute_global_motion_for_references(
@@ -867,7 +582,6 @@
     }
   }
   dealloc_global_motion_data(params_by_motion, segment_map);
-#endif  // CONFIG_NEW_REF_SIGNALING
 }
 
 // Global motion estimation for the current frame is computed.This computation
diff --git a/av1/encoder/global_motion_facade.h b/av1/encoder/global_motion_facade.h
index 63331a4..0e50801 100644
--- a/av1/encoder/global_motion_facade.h
+++ b/av1/encoder/global_motion_facade.h
@@ -18,9 +18,13 @@
 struct yv12_buffer_config;
 struct AV1_COMP;
 
+void av1_compute_gm_for_valid_ref_frames(
+    AV1_COMP *cpi,
 #if CONFIG_NEW_REF_SIGNALING
-void av1_compute_gm_for_valid_ref_frames_nrs(
-    AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS],
+    YV12_BUFFER_CONFIG *ref_buf[INTER_REFS_PER_FRAME_NRS],
+#else
+    YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES],
+#endif  // CONFIG_NEW_REF_SIGNALING
     int frame,
 #if CONFIG_GM_MODEL_CODING
     int *base_frame,
@@ -28,13 +32,7 @@
     int num_src_corners, int *src_corners, unsigned char *src_buffer,
     MotionModel *params_by_motion, uint8_t *segment_map, int segment_map_w,
     int segment_map_h);
-#endif  // CONFIG_NEW_REF_SIGNALING
 
-void av1_compute_gm_for_valid_ref_frames(
-    struct AV1_COMP *cpi, YV12_BUFFER_CONFIG *ref_buf[REF_FRAMES], int frame,
-    int num_src_corners, int *src_corners, unsigned char *src_buffer,
-    MotionModel *params_by_motion, uint8_t *segment_map, int segment_map_w,
-    int segment_map_h);
 void av1_compute_global_motion_facade(struct AV1_COMP *cpi);
 #ifdef __cplusplus
 }  // extern "C"
diff --git a/av1/encoder/rdopt.h b/av1/encoder/rdopt.h
index de81d28..650e302 100644
--- a/av1/encoder/rdopt.h
+++ b/av1/encoder/rdopt.h
@@ -264,49 +264,6 @@
   return 0;
 }
 
-static INLINE int prune_ref_by_selective_ref_frame(
-    const AV1_COMP *const cpi, const MACROBLOCK *const x,
-    const MV_REFERENCE_FRAME *const ref_frame,
-    const unsigned int *const ref_display_order_hint) {
-  const SPEED_FEATURES *const sf = &cpi->sf;
-  if (!sf->inter_sf.selective_ref_frame) return 0;
-
-  const int comp_pred = ref_frame[1] > INTRA_FRAME;
-
-  if (sf->inter_sf.selective_ref_frame >= 2 ||
-      (sf->inter_sf.selective_ref_frame == 1 && comp_pred)) {
-    int ref_frame_list[2] = { LAST3_FRAME, LAST2_FRAME };
-
-    if (x != NULL) {
-      if (x->tpl_keep_ref_frame[LAST3_FRAME]) ref_frame_list[0] = NONE_FRAME;
-      if (x->tpl_keep_ref_frame[LAST2_FRAME]) ref_frame_list[1] = NONE_FRAME;
-    }
-
-    if (prune_ref(ref_frame, ref_display_order_hint,
-                  ref_display_order_hint[GOLDEN_FRAME - LAST_FRAME],
-                  ref_frame_list)) {
-      return 1;
-    }
-  }
-
-  if (sf->inter_sf.selective_ref_frame >= 3) {
-    int ref_frame_list[2] = { ALTREF2_FRAME, BWDREF_FRAME };
-
-    if (x != NULL) {
-      if (x->tpl_keep_ref_frame[ALTREF2_FRAME]) ref_frame_list[0] = NONE_FRAME;
-      if (x->tpl_keep_ref_frame[BWDREF_FRAME]) ref_frame_list[1] = NONE_FRAME;
-    }
-
-    if (prune_ref(ref_frame, ref_display_order_hint,
-                  ref_display_order_hint[LAST_FRAME - LAST_FRAME],
-                  ref_frame_list)) {
-      return 1;
-    }
-  }
-
-  return 0;
-}
-
 #if CONFIG_NEW_REF_SIGNALING
 static INLINE int prune_ref_by_selective_ref_frame_nrs(
     const AV1_COMP *const cpi, const MACROBLOCK *const x,
@@ -392,6 +349,50 @@
   }
   return 0;
 }
+#else
+static INLINE int prune_ref_by_selective_ref_frame(
+    const AV1_COMP *const cpi, const MACROBLOCK *const x,
+    const MV_REFERENCE_FRAME *const ref_frame,
+    const unsigned int *const ref_display_order_hint) {
+  const SPEED_FEATURES *const sf = &cpi->sf;
+  if (!sf->inter_sf.selective_ref_frame) return 0;
+
+  const int comp_pred = ref_frame[1] > INTRA_FRAME;
+
+  if (sf->inter_sf.selective_ref_frame >= 2 ||
+      (sf->inter_sf.selective_ref_frame == 1 && comp_pred)) {
+    int ref_frame_list[2] = { LAST3_FRAME, LAST2_FRAME };
+
+    if (x != NULL) {
+      if (x->tpl_keep_ref_frame[LAST3_FRAME]) ref_frame_list[0] = NONE_FRAME;
+      if (x->tpl_keep_ref_frame[LAST2_FRAME]) ref_frame_list[1] = NONE_FRAME;
+    }
+
+    if (prune_ref(ref_frame, ref_display_order_hint,
+                  ref_display_order_hint[GOLDEN_FRAME - LAST_FRAME],
+                  ref_frame_list)) {
+      return 1;
+    }
+  }
+
+  if (sf->inter_sf.selective_ref_frame >= 3) {
+    int ref_frame_list[2] = { ALTREF2_FRAME, BWDREF_FRAME };
+
+    if (x != NULL) {
+      if (x->tpl_keep_ref_frame[ALTREF2_FRAME]) ref_frame_list[0] = NONE_FRAME;
+      if (x->tpl_keep_ref_frame[BWDREF_FRAME]) ref_frame_list[1] = NONE_FRAME;
+    }
+
+    if (prune_ref(ref_frame, ref_display_order_hint,
+                  ref_display_order_hint[LAST_FRAME - LAST_FRAME],
+                  ref_frame_list)) {
+      return 1;
+    }
+  }
+
+  return 0;
+}
+
 #endif  // CONFIG_NEW_REF_SIGNALING
 
 // This function will copy the best reference mode information from