Implement convolve_round in high bit-depth mode

Change-Id: I48a2148854e2abd0a3cc66aef58bb6a5d77c8b59
diff --git a/av1/common/convolve.c b/av1/common/convolve.c
index eab6fe7..c83c8d7 100644
--- a/av1/common/convolve.c
+++ b/av1/common/convolve.c
@@ -331,6 +331,138 @@
   }
 }
 
+#if CONFIG_HIGHBITDEPTH
+static INLINE void transpose_uint16(uint16_t *dst, int dst_stride,
+                                    const uint16_t *src, int src_stride, int w,
+                                    int h) {
+  int r, c;
+  for (r = 0; r < h; ++r)
+    for (c = 0; c < w; ++c) dst[c * dst_stride + r] = src[r * src_stride + c];
+}
+
+void av1_highbd_convolve_rounding(const int32_t *src, int src_stride,
+                                  uint8_t *dst8, int dst_stride, int w, int h,
+                                  int bits, int bd) {
+  uint16_t *dst = CONVERT_TO_SHORTPTR(dst8);
+  int r, c;
+  for (r = 0; r < h; ++r) {
+    for (c = 0; c < w; ++c) {
+      dst[r * dst_stride + c] = clip_pixel_highbd(
+          ROUND_POWER_OF_TWO_SIGNED(src[r * src_stride + c], bits), bd);
+    }
+  }
+}
+
+void av1_highbd_convolve_2d(const uint16_t *src, int src_stride,
+                            CONV_BUF_TYPE *dst, int dst_stride, int w, int h,
+                            InterpFilterParams *filter_params_x,
+                            InterpFilterParams *filter_params_y,
+                            const int subpel_x_q4, const int subpel_y_q4,
+                            ConvolveParams *conv_params, int bd) {
+  int x, y, k;
+  CONV_BUF_TYPE im_block[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) * MAX_SB_SIZE];
+  int im_h = h + filter_params_y->taps - 1;
+  int im_stride = w;
+  const int fo_vert = filter_params_y->taps / 2 - 1;
+  const int fo_horiz = filter_params_x->taps / 2 - 1;
+  (void)conv_params;
+  // horizontal filter
+  const uint16_t *src_horiz = src - fo_vert * src_stride;
+  const int16_t *x_filter = av1_get_interp_filter_subpel_kernel(
+      *filter_params_x, subpel_x_q4 & SUBPEL_MASK);
+  for (y = 0; y < im_h; ++y) {
+    for (x = 0; x < w; ++x) {
+      CONV_BUF_TYPE sum = 0;
+      for (k = 0; k < filter_params_x->taps; ++k) {
+        sum += x_filter[k] * src_horiz[y * src_stride + x - fo_horiz + k];
+      }
+#if CONFIG_COMPOUND_ROUND
+      im_block[y * im_stride + x] = clip_pixel_highbd(
+          ROUND_POWER_OF_TWO_SIGNED(sum, conv_params->round_0), bd);
+#else
+      (void)bd;
+      im_block[y * im_stride + x] =
+          ROUND_POWER_OF_TWO_SIGNED(sum, conv_params->round_0);
+#endif
+    }
+  }
+
+  // vertical filter
+  CONV_BUF_TYPE *src_vert = im_block + fo_vert * im_stride;
+  const int16_t *y_filter = av1_get_interp_filter_subpel_kernel(
+      *filter_params_y, subpel_y_q4 & SUBPEL_MASK);
+  for (y = 0; y < h; ++y) {
+    for (x = 0; x < w; ++x) {
+      CONV_BUF_TYPE sum = 0;
+      for (k = 0; k < filter_params_y->taps; ++k) {
+        sum += y_filter[k] * src_vert[(y - fo_vert + k) * im_stride + x];
+      }
+      dst[y * dst_stride + x] +=
+          ROUND_POWER_OF_TWO_SIGNED(sum, conv_params->round_1);
+    }
+  }
+}
+
+void av1_highbd_convolve_2d_facade(const uint8_t *src8, int src_stride,
+                                   uint8_t *dst, int dst_stride, int w, int h,
+                                   const InterpFilter *interp_filter,
+                                   const int subpel_x_q4, int x_step_q4,
+                                   const int subpel_y_q4, int y_step_q4,
+                                   ConvolveParams *conv_params, int bd) {
+  (void)x_step_q4;
+  (void)y_step_q4;
+  (void)dst;
+  (void)dst_stride;
+#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]);
+
+  if (filter_params_x.interp_filter == MULTITAP_SHARP &&
+      filter_params_y.interp_filter == 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);
+  }
+#else
+  InterpFilterParams filter_params_x =
+      av1_get_interp_filter_params(*interp_filter);
+  InterpFilterParams filter_params_y =
+      av1_get_interp_filter_params(*interp_filter);
+#endif
+  const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
+  if (filter_params_y.taps < filter_params_x.taps) {
+    uint16_t tr_src[(MAX_SB_SIZE + MAX_FILTER_TAP - 1) *
+                    (MAX_SB_SIZE + MAX_FILTER_TAP - 1)];
+    int tr_src_stride = MAX_SB_SIZE + MAX_FILTER_TAP - 1;
+    CONV_BUF_TYPE tr_dst[MAX_SB_SIZE * MAX_SB_SIZE];
+    int tr_dst_stride = MAX_SB_SIZE;
+    int fo_vert = filter_params_y.taps / 2 - 1;
+    int fo_horiz = filter_params_x.taps / 2 - 1;
+
+    transpose_uint16(
+        tr_src, tr_src_stride, src - fo_vert * src_stride - fo_horiz,
+        src_stride, w + filter_params_x.taps - 1, h + filter_params_y.taps - 1);
+    transpose_int32(tr_dst, tr_dst_stride, conv_params->dst,
+                    conv_params->dst_stride, w, h);
+
+    // horizontal and vertical parameters are swapped because of the transpose
+    av1_highbd_convolve_2d(tr_src + fo_horiz * tr_src_stride + fo_vert,
+                           tr_src_stride, tr_dst, tr_dst_stride, h, w,
+                           &filter_params_y, &filter_params_x, subpel_y_q4,
+                           subpel_x_q4, conv_params, bd);
+    transpose_int32(conv_params->dst, conv_params->dst_stride, tr_dst,
+                    tr_dst_stride, h, w);
+  } else {
+    av1_highbd_convolve_2d(src, src_stride, conv_params->dst,
+                           conv_params->dst_stride, w, h, &filter_params_x,
+                           &filter_params_y, subpel_x_q4, subpel_y_q4,
+                           conv_params, bd);
+  }
+}
+#endif  // CONFIG_HIGHBITDEPTH
+
 #endif  // CONFIG_CONVOLVE_ROUND
 
 typedef void (*ConvolveFunc)(const uint8_t *src, int src_stride, uint8_t *dst,
diff --git a/av1/common/convolve.h b/av1/common/convolve.h
index 4a4dd8c..15a3087 100644
--- a/av1/common/convolve.h
+++ b/av1/common/convolve.h
@@ -78,6 +78,26 @@
 
 void av1_convolve_rounding(const int32_t *src, int src_stride, uint8_t *dst,
                            int dst_stride, int w, int h, int bits);
+
+#if CONFIG_HIGHBITDEPTH
+void av1_highbd_convolve_rounding(const int32_t *src, int src_stride,
+                                  uint8_t *dst8, int dst_stride, int w, int h,
+                                  int bits, int bd);
+
+void av1_highbd_convolve_2d(const uint16_t *src, int src_stride,
+                            CONV_BUF_TYPE *dst, int dst_stride, int w, int h,
+                            InterpFilterParams *filter_params_x,
+                            InterpFilterParams *filter_params_y,
+                            const int subpel_x_q4, const int subpel_y_q4,
+                            ConvolveParams *conv_params, int bd);
+
+void av1_highbd_convolve_2d_facade(const uint8_t *src8, int src_stride,
+                                   uint8_t *dst, int dst_stride, int w, int h,
+                                   const InterpFilter *interp_filter,
+                                   const int subpel_x_q4, int x_step_q4,
+                                   const int subpel_y_q4, int y_step_q4,
+                                   ConvolveParams *conv_params, int bd);
+#endif
 #endif  // CONFIG_CONVOLVE_ROUND
 
 void av1_convolve(const uint8_t *src, int src_stride, uint8_t *dst,
diff --git a/av1/common/reconinter.c b/av1/common/reconinter.c
index 09f5ec1..7424502 100644
--- a/av1/common/reconinter.c
+++ b/av1/common/reconinter.c
@@ -1153,7 +1153,13 @@
 #if CONFIG_CONVOLVE_ROUND
 // TODO(angiebird): This part needs optimization
 #if CONFIG_HIGHBITDEPTH
-    if (!(xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH))
+    if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
+      av1_highbd_convolve_rounding(tmp_dst, MAX_SB_SIZE, dst, dst_buf->stride,
+                                   w, h, FILTER_BITS * 2 + is_compound -
+                                             conv_params.round_0 -
+                                             conv_params.round_1,
+                                   xd->bd);
+    else
 #endif  // CONFIG_HIGHBITDEPTH
       av1_convolve_rounding(tmp_dst, MAX_SB_SIZE, dst, dst_buf->stride, w, h,
                             FILTER_BITS * 2 + is_compound -
diff --git a/av1/common/reconinter.h b/av1/common/reconinter.h
index 7e4e982..1a6c7b8 100644
--- a/av1/common/reconinter.h
+++ b/av1/common/reconinter.h
@@ -101,13 +101,18 @@
                                           const int subpel_x,
                                           const int subpel_y,
                                           const struct scale_factors *sf, int w,
-                                          int h, int ref,
+                                          int h, ConvolveParams *conv_params,
 #if CONFIG_DUAL_FILTER
                                           const InterpFilter *interp_filter,
 #else
                                           const InterpFilter interp_filter,
 #endif
                                           int xs, int ys, int bd) {
+  const int ref = conv_params->ref;
+  // ref > 0 means this is the second reference frame
+  // first reference frame's prediction result is already in dst
+  // therefore we need to average the first and second results
+  const int avg = ref > 0;
 #if CONFIG_DUAL_FILTER
   const InterpFilterParams interp_filter_params_x =
       av1_get_interp_filter_params(interp_filter[1 + 2 * ref]);
@@ -119,21 +124,35 @@
   const InterpFilterParams interp_filter_params_y = interp_filter_params_x;
 #endif
 
-  if (interp_filter_params_x.taps == SUBPEL_TAPS &&
-      interp_filter_params_y.taps == SUBPEL_TAPS && w > 2 && h > 2) {
-    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);
-    sf->highbd_predict[subpel_x != 0][subpel_y != 0][ref](
-        src, src_stride, dst, dst_stride, kernel_x, xs, kernel_y, ys, w, h, bd);
-  } else {
-    // ref > 0 means this is the second reference frame
-    // first reference frame's prediction result is already in dst
-    // therefore we need to average the first and second results
-    const int avg = ref > 0;
+  if (has_scale(xs, ys)) {
     av1_highbd_convolve(src, src_stride, dst, dst_stride, w, h, interp_filter,
                         subpel_x, xs, subpel_y, ys, avg, bd);
+  } else if (conv_params->round == CONVOLVE_OPT_NO_ROUND) {
+#if CONFIG_CONVOLVE_ROUND
+    av1_highbd_convolve_2d_facade(src, src_stride, dst, dst_stride, w, h,
+#if CONFIG_DUAL_FILTER
+                                  interp_filter,
+#else   // CONFIG_DUAL_FILTER
+                                  &interp_filter,
+#endif  // CONFIG_DUAL_FILTER
+                                  subpel_x, xs, subpel_y, ys, conv_params, bd);
+#else
+    assert(0);
+#endif  // CONFIG_CONVOLVE_ROUND
+  } else {
+    if (interp_filter_params_x.taps == SUBPEL_TAPS &&
+        interp_filter_params_y.taps == SUBPEL_TAPS && w > 2 && h > 2) {
+      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);
+      sf->highbd_predict[subpel_x != 0][subpel_y != 0][ref](
+          src, src_stride, dst, dst_stride, kernel_x, xs, kernel_y, ys, w, h,
+          bd);
+    } else {
+      av1_highbd_convolve(src, src_stride, dst, dst_stride, w, h, interp_filter,
+                          subpel_x, xs, subpel_y, ys, avg, bd);
+    }
   }
 }
 #endif  // CONFIG_HIGHBITDEPTH
@@ -417,7 +436,7 @@
 #if CONFIG_HIGHBITDEPTH
   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
     highbd_inter_predictor(src, src_stride, dst, dst_stride, subpel_x, subpel_y,
-                           sf, w, h, conv_params->ref, interp_filter, xs, ys,
+                           sf, w, h, conv_params, interp_filter, xs, ys,
                            xd->bd);
     return;
   }