scaling: Avoid SSSE3 convolution instructions when scaling

The SSE3 convolve functions do not work when scaling is involved. If
either x_step_q4 or y_step_q4 is not 16 scaling will occur and an assert
fails.

This patch creates a C version of av1_convolve, av1_convolve_c. This
function in turn calls all C versions of what av1_convolve calls. New C
versions of these called functions have been created where needed and
the same for the functions they call. This means that when scaling is
enabled no asserts fail. av1_convolve_c is called instead of
av1_convolve when x_step_q4 or y_step_q4 is not 16.

Change-Id: I604d2716e034e23a0553fb7004133d3075514a7a
diff --git a/av1/common/convolve.c b/av1/common/convolve.c
index cf2acd0..1a8ef88 100644
--- a/av1/common/convolve.c
+++ b/av1/common/convolve.c
@@ -131,6 +131,27 @@
   }
 }
 
+void av1_convolve_horiz_facade_c(const uint8_t *src, int src_stride,
+                                 uint8_t *dst, int dst_stride, int w, int h,
+                                 const InterpFilterParams filter_params,
+                                 const int subpel_x_q4, int x_step_q4,
+                                 ConvolveParams *conv_params) {
+  assert(conv_params->round == CONVOLVE_OPT_ROUND);
+  if (filter_params.taps == SUBPEL_TAPS) {
+    const int16_t *filter_x =
+        av1_get_interp_filter_subpel_kernel(filter_params, subpel_x_q4);
+    if (conv_params->ref == 0)
+      aom_convolve8_horiz_c(src, src_stride, dst, dst_stride, filter_x,
+                            x_step_q4, NULL, -1, w, h);
+    else
+      aom_convolve8_avg_horiz_c(src, src_stride, dst, dst_stride, filter_x,
+                                x_step_q4, NULL, -1, w, h);
+  } else {
+    av1_convolve_horiz_c(src, src_stride, dst, dst_stride, w, h, filter_params,
+                         subpel_x_q4, x_step_q4, conv_params);
+  }
+}
+
 void av1_convolve_vert_facade(const uint8_t *src, int src_stride, uint8_t *dst,
                               int dst_stride, int w, int h,
                               const InterpFilterParams filter_params,
@@ -153,6 +174,28 @@
   }
 }
 
+void av1_convolve_vert_facade_c(const uint8_t *src, int src_stride,
+                                uint8_t *dst, int dst_stride, int w, int h,
+                                const InterpFilterParams filter_params,
+                                const int subpel_y_q4, int y_step_q4,
+                                ConvolveParams *conv_params) {
+  assert(conv_params->round == CONVOLVE_OPT_ROUND);
+  if (filter_params.taps == SUBPEL_TAPS) {
+    const int16_t *filter_y =
+        av1_get_interp_filter_subpel_kernel(filter_params, subpel_y_q4);
+    if (conv_params->ref == 0) {
+      aom_convolve8_vert_c(src, src_stride, dst, dst_stride, NULL, -1, filter_y,
+                           y_step_q4, w, h);
+    } else {
+      aom_convolve8_avg_vert_c(src, src_stride, dst, dst_stride, NULL, -1,
+                               filter_y, y_step_q4, w, h);
+    }
+  } else {
+    av1_convolve_vert_c(src, src_stride, dst, dst_stride, w, h, filter_params,
+                        subpel_y_q4, y_step_q4, conv_params);
+  }
+}
+
 #if CONFIG_CONVOLVE_ROUND
 void av1_convolve_rounding(const int32_t *src, int src_stride, uint8_t *dst,
                            int dst_stride, int w, int h, int bits) {
@@ -411,6 +454,133 @@
   }
 }
 
+void av1_convolve_c(const uint8_t *src, int src_stride, uint8_t *dst,
+                    int dst_stride, int w, int h,
+#if CONFIG_DUAL_FILTER
+                    const InterpFilter *interp_filter,
+#else
+                    const InterpFilter interp_filter,
+#endif
+                    const int subpel_x_q4, int x_step_q4, const int subpel_y_q4,
+                    int y_step_q4, ConvolveParams *conv_params) {
+  int ignore_horiz = x_step_q4 == 16 && subpel_x_q4 == 0;
+  int ignore_vert = y_step_q4 == 16 && subpel_y_q4 == 0;
+#if CONFIG_DUAL_FILTER
+  InterpFilterParams filter_params_x =
+      av1_get_interp_filter_params(interp_filter[1 + 2 * conv_params->ref]);
+  InterpFilterParams filter_params_y =
+      av1_get_interp_filter_params(interp_filter[0 + 2 * conv_params->ref]);
+  InterpFilterParams filter_params;
+#else
+  InterpFilterParams filter_params =
+      av1_get_interp_filter_params(interp_filter);
+#endif
+  assert(conv_params->round == CONVOLVE_OPT_ROUND);
+
+  assert(w <= MAX_BLOCK_WIDTH);
+  assert(h <= MAX_BLOCK_HEIGHT);
+  assert(y_step_q4 <= MAX_STEP);
+  assert(x_step_q4 <= MAX_STEP);
+
+  if (ignore_horiz && ignore_vert) {
+    convolve_copy(src, src_stride, dst, dst_stride, w, h, conv_params);
+  } else if (ignore_vert) {
+#if CONFIG_DUAL_FILTER
+    filter_params = filter_params_x;
+#endif
+    assert(filter_params.taps <= MAX_FILTER_TAP);
+    av1_convolve_horiz_facade_c(src, src_stride, dst, dst_stride, w, h,
+                                filter_params, subpel_x_q4, x_step_q4,
+                                conv_params);
+  } else if (ignore_horiz) {
+#if CONFIG_DUAL_FILTER
+    filter_params = filter_params_y;
+#endif
+    assert(filter_params.taps <= MAX_FILTER_TAP);
+    av1_convolve_vert_facade_c(src, src_stride, dst, dst_stride, w, h,
+                               filter_params, subpel_y_q4, y_step_q4,
+                               conv_params);
+  } else {
+    // temp's size is set to a 256 aligned value to facilitate SIMD
+    // implementation. The value is greater than (maximum possible intermediate
+    // height or width) * MAX_SB_SIZE
+    DECLARE_ALIGNED(16, uint8_t,
+                    temp[((MAX_SB_SIZE * 2 + 16) + 16) * MAX_SB_SIZE]);
+    int max_intermediate_size = ((MAX_SB_SIZE * 2 + 16) + 16);
+    int filter_size;
+#if CONFIG_DUAL_FILTER
+    if (interp_filter[0 + 2 * conv_params->ref] == MULTITAP_SHARP &&
+        interp_filter[1 + 2 * conv_params->ref] == MULTITAP_SHARP) {
+      // Avoid two directions both using 12-tap filter.
+      // This will reduce hardware implementation cost.
+      filter_params_y = av1_get_interp_filter_params(EIGHTTAP_SHARP);
+    }
+
+    // we do filter with fewer taps first to reduce hardware implementation
+    // complexity
+    if (filter_params_y.taps < filter_params_x.taps) {
+      int intermediate_width;
+      int temp_stride = max_intermediate_size;
+      ConvolveParams temp_conv_params;
+      temp_conv_params.ref = 0;
+      temp_conv_params.round = CONVOLVE_OPT_ROUND;
+      filter_params = filter_params_y;
+      filter_size = filter_params_x.taps;
+      intermediate_width =
+          (((w - 1) * x_step_q4 + subpel_x_q4) >> SUBPEL_BITS) + filter_size;
+      assert(intermediate_width <= max_intermediate_size);
+
+      assert(filter_params.taps <= MAX_FILTER_TAP);
+
+      av1_convolve_vert_facade(src - (filter_size / 2 - 1), src_stride, temp,
+                               temp_stride, intermediate_width, h,
+                               filter_params, subpel_y_q4, y_step_q4,
+                               &temp_conv_params);
+
+      filter_params = filter_params_x;
+      assert(filter_params.taps <= MAX_FILTER_TAP);
+      av1_convolve_horiz_facade(temp + (filter_size / 2 - 1), temp_stride, dst,
+                                dst_stride, w, h, filter_params, subpel_x_q4,
+                                x_step_q4, conv_params);
+    } else {
+#endif  // CONFIG_DUAL_FILTER
+      int intermediate_height;
+      int temp_stride = MAX_SB_SIZE;
+      ConvolveParams temp_conv_params;
+      temp_conv_params.ref = 0;
+      temp_conv_params.round = CONVOLVE_OPT_ROUND;
+#if CONFIG_DUAL_FILTER
+      filter_params = filter_params_x;
+      filter_size = filter_params_y.taps;
+#else
+    filter_size = filter_params.taps;
+#endif
+      intermediate_height =
+          (((h - 1) * y_step_q4 + subpel_y_q4) >> SUBPEL_BITS) + filter_size;
+      assert(intermediate_height <= max_intermediate_size);
+      (void)max_intermediate_size;
+
+      assert(filter_params.taps <= MAX_FILTER_TAP);
+
+      av1_convolve_horiz_facade_c(src - src_stride * (filter_size / 2 - 1),
+                                  src_stride, temp, temp_stride, w,
+                                  intermediate_height, filter_params,
+                                  subpel_x_q4, x_step_q4, &temp_conv_params);
+
+#if CONFIG_DUAL_FILTER
+      filter_params = filter_params_y;
+#endif
+      assert(filter_params.taps <= MAX_FILTER_TAP);
+
+      av1_convolve_vert_facade_c(
+          temp + temp_stride * (filter_size / 2 - 1), temp_stride, dst,
+          dst_stride, w, h, filter_params, subpel_y_q4, y_step_q4, conv_params);
+#if CONFIG_DUAL_FILTER
+    }
+#endif  // CONFIG_DUAL_FILTER
+  }
+}
+
 void av1_lowbd_convolve_init_c(void) {
   // A placeholder for SIMD initialization
   return;
diff --git a/av1/common/convolve.h b/av1/common/convolve.h
index 471152e..cb6569c 100644
--- a/av1/common/convolve.h
+++ b/av1/common/convolve.h
@@ -86,6 +86,16 @@
                   const int subpel_x, int xstep, const int subpel_y, int ystep,
                   ConvolveParams *conv_params);
 
+void av1_convolve_c(const uint8_t *src, int src_stride, uint8_t *dst,
+                    int dst_stride, int w, int h,
+#if CONFIG_DUAL_FILTER
+                    const InterpFilter *interp_filter,
+#else
+                    const InterpFilter interp_filter,
+#endif
+                    const int subpel_x, int xstep, const int subpel_y,
+                    int ystep, ConvolveParams *conv_params);
+
 #if CONFIG_AOM_HIGHBITDEPTH
 void av1_highbd_convolve(const uint8_t *src, int src_stride, uint8_t *dst,
                          int dst_stride, int w, int h,
diff --git a/av1/common/reconinter.h b/av1/common/reconinter.h
index 5408870..c33c936 100644
--- a/av1/common/reconinter.h
+++ b/av1/common/reconinter.h
@@ -56,14 +56,14 @@
 #if CONFIG_DUAL_FILTER
   if (interp_filter_params_x.taps == SUBPEL_TAPS &&
       interp_filter_params_y.taps == SUBPEL_TAPS && w > 2 && h > 2 &&
-      conv_params->round == CONVOLVE_OPT_ROUND) {
+      conv_params->round == CONVOLVE_OPT_ROUND && xs == 16 && ys == 16) {
     const int16_t *kernel_x =
         av1_get_interp_filter_subpel_kernel(interp_filter_params_x, subpel_x);
     const int16_t *kernel_y =
         av1_get_interp_filter_subpel_kernel(interp_filter_params_y, subpel_y);
 #else
   if (interp_filter_params.taps == SUBPEL_TAPS && w > 2 && h > 2 &&
-      conv_params->round == CONVOLVE_OPT_ROUND) {
+      conv_params->round == CONVOLVE_OPT_ROUND && xs == 16 && ys == 16) {
     const int16_t *kernel_x =
         av1_get_interp_filter_subpel_kernel(interp_filter_params, subpel_x);
     const int16_t *kernel_y =
@@ -76,7 +76,7 @@
 // first reference frame's prediction result is already in dst
 // therefore we need to average the first and second results
 #if CONFIG_CONVOLVE_ROUND
-    if (conv_params->round == CONVOLVE_OPT_NO_ROUND)
+    if (conv_params->round == CONVOLVE_OPT_NO_ROUND && xs == 16 && ys == 16)
       av1_convolve_2d_facade(src, src_stride, dst, dst_stride, w, h,
 #if CONFIG_DUAL_FILTER
                              interp_filter,
@@ -86,8 +86,17 @@
                              subpel_x, xs, subpel_y, ys, conv_params);
     else
 #endif
-      av1_convolve(src, src_stride, dst, dst_stride, w, h, interp_filter,
-                   subpel_x, xs, subpel_y, ys, conv_params);
+    {
+      if (xs == 16 && ys == 16) {
+        av1_convolve(src, src_stride, dst, dst_stride, w, h, interp_filter,
+                     subpel_x, xs, subpel_y, ys, conv_params);
+      } else {
+        // If xs == 16 || ys == 16 scaling is happening and the SSE2
+        // instructions don't support scaling; use the C versions to be safe.
+        av1_convolve_c(src, src_stride, dst, dst_stride, w, h, interp_filter,
+                       subpel_x, xs, subpel_y, ys, conv_params);
+      }
+    }
   }
 }