encoder.c: two helpers to refactor LR calls.

Introduce pick_and_apply_loop_restoration() and apply_loop_restoration()
functions.

Change-Id: I7deff790eab5e5bb3338bd8081ed1df850ba84e8
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index addec09..fa507a5 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -2013,6 +2013,31 @@
 }
 #endif  // CONFIG_CNN_RESTORATION
 
+/*!\brief Apply loop restoration filter with chosen parameters.
+ */
+static void apply_loop_restoration(AV1_COMP *cpi, AV1_COMMON *cm) {
+  if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
+      cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
+      cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
+    MultiThreadInfo *const mt_info = &cpi->mt_info;
+    const int num_workers = mt_info->num_workers;
+    if (num_workers > 1)
+      av1_loop_restoration_filter_frame_mt(
+          &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers,
+          &mt_info->lr_row_sync, &cpi->lr_ctxt);
+    else
+      av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
+                                        &cpi->lr_ctxt);
+  }
+}
+
+/*!\brief Pick best loop restoration filter parameters and apply that filter.
+ */
+static void pick_and_apply_loop_restoration(AV1_COMP *cpi, AV1_COMMON *cm) {
+  av1_pick_filter_restoration(cpi->source, cpi);
+  apply_loop_restoration(cpi, cm);
+}
+
 /*!\brief Select and apply cdef filters and switchable restoration filters
  *
  * \ingroup high_level_algo
@@ -2020,8 +2045,6 @@
 static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
                                    MACROBLOCKD *xd, bool use_restoration,
                                    bool use_cdef) {
-  MultiThreadInfo *const mt_info = &cpi->mt_info;
-  const int num_workers = mt_info->num_workers;
   if (use_restoration) {
     av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
   }
@@ -2096,19 +2119,7 @@
                            num_planes);
 
       // Try LR on all planes.
-      // TODO(urvang): Refactor.
-      av1_pick_filter_restoration(cpi->source, cpi);
-      if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
-          cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
-          cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
-        if (num_workers > 1)
-          av1_loop_restoration_filter_frame_mt(
-              &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers,
-              &mt_info->lr_row_sync, &cpi->lr_ctxt);
-        else
-          av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
-                                            &cpi->lr_ctxt);
-      }
+      pick_and_apply_loop_restoration(cpi, cm);
 
       // Calculate errors after LR from source.
       int64_t res_errors[MAX_MB_PLANE];
@@ -2147,17 +2158,7 @@
           cm->rst_info[plane].frame_restoration_type = RESTORE_NONE;
         }
       }
-      if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
-          cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
-          cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
-        if (num_workers > 1)
-          av1_loop_restoration_filter_frame_mt(
-              &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers,
-              &mt_info->lr_row_sync, &cpi->lr_ctxt);
-        else
-          av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
-                                            &cpi->lr_ctxt);
-      }
+      apply_loop_restoration(cpi, cm);
       /*
       fprintf(stderr,
               "\ngf_index = %d, use_cnn[0] = %d, use_cnn[1] = %d, "
@@ -2167,18 +2168,7 @@
       */
     } else {
 #endif  // CONFIG_CNN_RESTORATION
-      av1_pick_filter_restoration(cpi->source, cpi);
-      if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
-          cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
-          cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
-        if (num_workers > 1)
-          av1_loop_restoration_filter_frame_mt(
-              &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers,
-              &mt_info->lr_row_sync, &cpi->lr_ctxt);
-        else
-          av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
-                                            &cpi->lr_ctxt);
-      }
+      pick_and_apply_loop_restoration(cpi, cm);
 #if CONFIG_CNN_RESTORATION
     }
 #endif  // CONFIG_CNN_RESTORATION