Refactor vmaf preprocessing functions

Change-Id: If7440e4df08833bb5b8b58481ef797960dbdf6f2
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 860d05a..c0fc1d7 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -6431,7 +6431,12 @@
 #if CONFIG_TUNE_VMAF
   if (!is_stat_generation_stage(cpi) &&
       cpi->oxcf.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) {
-    av1_vmaf_preprocessing(cpi, sd, false);
+    const bool use_block_based_methods = false;
+    if (use_block_based_methods) {
+      av1_vmaf_blk_preprocessing(cpi, sd);
+    } else {
+      av1_vmaf_frame_preprocessing(cpi, sd);
+    }
   }
 #endif
 
diff --git a/av1/encoder/tune_vmaf.c b/av1/encoder/tune_vmaf.c
index c0ccb6a..c060d8a 100644
--- a/av1/encoder/tune_vmaf.c
+++ b/av1/encoder/tune_vmaf.c
@@ -143,9 +143,44 @@
   return unsharp_amount - step_size;
 }
 
-void av1_vmaf_preprocessing(const AV1_COMP *const cpi,
-                            YV12_BUFFER_CONFIG *const source,
-                            bool use_block_based_method) {
+void av1_vmaf_frame_preprocessing(const AV1_COMP *const cpi,
+                                  YV12_BUFFER_CONFIG *const source) {
+  const int use_hbd = source->flags & YV12_FLAG_HIGHBITDEPTH;
+  // TODO(sdeng): Add high bit depth support.
+  if (use_hbd) {
+    printf(
+        "VMAF preprocessing for high bit depth videos is unsupported yet.\n");
+    exit(0);
+  }
+
+  aom_clear_system_state();
+  const AV1_COMMON *const cm = &cpi->common;
+  const int width = source->y_width;
+  const int height = source->y_height;
+  YV12_BUFFER_CONFIG source_extended, blurred;
+  memset(&source_extended, 0, sizeof(source_extended));
+  memset(&blurred, 0, sizeof(blurred));
+  aom_alloc_frame_buffer(&source_extended, width, height, 1, 1,
+                         cm->seq_params.use_highbitdepth,
+                         cpi->oxcf.border_in_pixels, cm->byte_alignment);
+  aom_alloc_frame_buffer(&blurred, width, height, 1, 1,
+                         cm->seq_params.use_highbitdepth,
+                         cpi->oxcf.border_in_pixels, cm->byte_alignment);
+
+  av1_copy_and_extend_frame(source, &source_extended);
+
+  gaussian_blur(cpi, &source_extended, &blurred);
+  aom_free_frame_buffer(&source_extended);
+  const double best_frame_unsharp_amount =
+      find_best_frame_unsharp_amount(cpi, source, &blurred);
+
+  unsharp(source, &blurred, source, best_frame_unsharp_amount);
+  aom_free_frame_buffer(&blurred);
+  aom_clear_system_state();
+}
+
+void av1_vmaf_blk_preprocessing(const AV1_COMP *const cpi,
+                                YV12_BUFFER_CONFIG *const source) {
   const int use_hbd = source->flags & YV12_FLAG_HIGHBITDEPTH;
   // TODO(sdeng): Add high bit depth support.
   if (use_hbd) {
@@ -180,14 +215,6 @@
   const double best_frame_unsharp_amount =
       find_best_frame_unsharp_amount(cpi, source, &blurred);
 
-  if (!use_block_based_method) {
-    unsharp(source, &blurred, source, best_frame_unsharp_amount);
-    aom_free_frame_buffer(&sharpened);
-    aom_free_frame_buffer(&blurred);
-    aom_clear_system_state();
-    return;
-  }
-
   const int block_size = BLOCK_128X128;
   const int num_mi_w = mi_size_wide[block_size];
   const int num_mi_h = mi_size_high[block_size];
diff --git a/av1/encoder/tune_vmaf.h b/av1/encoder/tune_vmaf.h
index 123ca3d..df744e9 100644
--- a/av1/encoder/tune_vmaf.h
+++ b/av1/encoder/tune_vmaf.h
@@ -15,8 +15,11 @@
 #include "aom_scale/yv12config.h"
 #include "av1/encoder/encoder.h"
 
-void av1_vmaf_preprocessing(const AV1_COMP *cpi, YV12_BUFFER_CONFIG *source,
-                            bool use_block_based_method);
+void av1_vmaf_blk_preprocessing(const AV1_COMP *cpi,
+                                YV12_BUFFER_CONFIG *source);
+
+void av1_vmaf_frame_preprocessing(const AV1_COMP *cpi,
+                                  YV12_BUFFER_CONFIG *source);
 
 void av1_set_mb_vmaf_rdmult_scaling(AV1_COMP *cpi);