Make loop-restoration compatible w/ frame_superres

When frame_superres is on, loop-restoration should work
on the size of the upscaled frame and not on the internal
width and height in the common structure. This patch
makes the necessary changes on the encoder and decoder
side to enable that.

Change-Id: I1d1c024ac6f95944169d90647b4c5a61354a5cc6
diff --git a/av1/common/restoration.c b/av1/common/restoration.c
index 4413093..84c06fe 100644
--- a/av1/common/restoration.c
+++ b/av1/common/restoration.c
@@ -1237,8 +1237,10 @@
                                   int components_pattern, RestorationInfo *rsi,
                                   YV12_BUFFER_CONFIG *dst) {
   const int ywidth = frame->y_crop_width;
-  const int ystride = frame->y_stride;
+  const int yheight = frame->y_crop_height;
   const int uvwidth = frame->uv_crop_width;
+  const int uvheight = frame->uv_crop_height;
+  const int ystride = frame->y_stride;
   const int uvstride = frame->uv_stride;
   const int ystart = start_mi_row << MI_SIZE_LOG2;
   const int uvstart = ystart >> cm->subsampling_y;
@@ -1295,7 +1297,7 @@
     dst = &dst_;
     memset(dst, 0, sizeof(YV12_BUFFER_CONFIG));
     if (aom_realloc_frame_buffer(
-            dst, cm->width, cm->height, cm->subsampling_x, cm->subsampling_y,
+            dst, ywidth, yheight, cm->subsampling_x, cm->subsampling_y,
 #if CONFIG_HIGHBITDEPTH
             cm->use_highbitdepth,
 #endif
@@ -1307,7 +1309,7 @@
   if ((components_pattern >> AOM_PLANE_Y) & 1) {
     if (rsi[0].frame_restoration_type != RESTORE_NONE) {
       cm->rst_internal.ntiles = av1_get_rest_ntiles(
-          cm->width, cm->height, cm->rst_info[AOM_PLANE_Y].restoration_tilesize,
+          ywidth, yheight, cm->rst_info[AOM_PLANE_Y].restoration_tilesize,
           &cm->rst_internal.tile_width, &cm->rst_internal.tile_height,
           &cm->rst_internal.nhtiles, &cm->rst_internal.nvtiles);
       cm->rst_internal.rsi = &rsi[0];
@@ -1334,9 +1336,7 @@
   if ((components_pattern >> AOM_PLANE_U) & 1) {
     if (rsi[AOM_PLANE_U].frame_restoration_type != RESTORE_NONE) {
       cm->rst_internal.ntiles = av1_get_rest_ntiles(
-          ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
-          ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
-          cm->rst_info[AOM_PLANE_U].restoration_tilesize,
+          uvwidth, uvheight, cm->rst_info[AOM_PLANE_U].restoration_tilesize,
           &cm->rst_internal.tile_width, &cm->rst_internal.tile_height,
           &cm->rst_internal.nhtiles, &cm->rst_internal.nvtiles);
       cm->rst_internal.rsi = &rsi[AOM_PLANE_U];
@@ -1363,9 +1363,7 @@
   if ((components_pattern >> AOM_PLANE_V) & 1) {
     if (rsi[AOM_PLANE_V].frame_restoration_type != RESTORE_NONE) {
       cm->rst_internal.ntiles = av1_get_rest_ntiles(
-          ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
-          ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
-          cm->rst_info[AOM_PLANE_V].restoration_tilesize,
+          uvwidth, uvheight, cm->rst_info[AOM_PLANE_V].restoration_tilesize,
           &cm->rst_internal.tile_width, &cm->rst_internal.tile_height,
           &cm->rst_internal.nhtiles, &cm->rst_internal.nvtiles);
       cm->rst_internal.rsi = &rsi[AOM_PLANE_V];
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 6105199..8585c6c 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -2682,16 +2682,23 @@
 
 static void decode_restoration(AV1_COMMON *cm, aom_reader *rb) {
   int i, p;
+#if CONFIG_FRAME_SUPERRES
+  const int width = cm->superres_upscaled_width;
+  const int height = cm->superres_upscaled_height;
+#else
+  const int width = cm->width;
+  const int height = cm->height;
+#endif  // CONFIG_FRAME_SUPERRES
   SgrprojInfo ref_sgrproj_info;
   WienerInfo ref_wiener_info;
   set_default_wiener(&ref_wiener_info);
   set_default_sgrproj(&ref_sgrproj_info);
-  const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
-                                         cm->rst_info[0].restoration_tilesize,
-                                         NULL, NULL, NULL, NULL);
+  const int ntiles =
+      av1_get_rest_ntiles(width, height, cm->rst_info[0].restoration_tilesize,
+                          NULL, NULL, NULL, NULL);
   const int ntiles_uv = av1_get_rest_ntiles(
-      ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
-      ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
+      ROUND_POWER_OF_TWO(width, cm->subsampling_x),
+      ROUND_POWER_OF_TWO(height, cm->subsampling_y),
       cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
   RestorationInfo *rsi = &cm->rst_info[0];
   if (rsi->frame_restoration_type != RESTORE_NONE) {
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index f8378b1..3184ad3 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -3311,16 +3311,23 @@
 
 static void encode_restoration(AV1_COMMON *cm, aom_writer *wb) {
   int i, p;
-  const int ntiles = av1_get_rest_ntiles(cm->width, cm->height,
-                                         cm->rst_info[0].restoration_tilesize,
-                                         NULL, NULL, NULL, NULL);
+#if CONFIG_FRAME_SUPERRES
+  const int width = cm->superres_upscaled_width;
+  const int height = cm->superres_upscaled_height;
+#else
+  const int width = cm->width;
+  const int height = cm->height;
+#endif  // CONFIG_FRAME_SUPERRES
+  const int ntiles =
+      av1_get_rest_ntiles(width, height, cm->rst_info[0].restoration_tilesize,
+                          NULL, NULL, NULL, NULL);
   WienerInfo ref_wiener_info;
   SgrprojInfo ref_sgrproj_info;
   set_default_wiener(&ref_wiener_info);
   set_default_sgrproj(&ref_sgrproj_info);
   const int ntiles_uv = av1_get_rest_ntiles(
-      ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
-      ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
+      ROUND_POWER_OF_TWO(width, cm->subsampling_x),
+      ROUND_POWER_OF_TWO(height, cm->subsampling_y),
       cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
   RestorationInfo *rsi = &cm->rst_info[0];
   if (rsi->frame_restoration_type != RESTORE_NONE) {
diff --git a/av1/encoder/pickrst.c b/av1/encoder/pickrst.c
index 4a446d2..532f6e5 100644
--- a/av1/encoder/pickrst.c
+++ b/av1/encoder/pickrst.c
@@ -437,8 +437,8 @@
   int width, height, src_stride, dgd_stride;
   uint8_t *dgd_buffer, *src_buffer;
   if (plane == AOM_PLANE_Y) {
-    width = cm->width;
-    height = cm->height;
+    width = src->y_crop_width;
+    height = src->y_crop_height;
     src_buffer = src->y_buffer;
     src_stride = src->y_stride;
     dgd_buffer = dgd->y_buffer;
@@ -985,8 +985,8 @@
   int width, height, src_stride, dgd_stride;
   uint8_t *dgd_buffer, *src_buffer;
   if (plane == AOM_PLANE_Y) {
-    width = cm->width;
-    height = cm->height;
+    width = src->y_crop_width;
+    height = src->y_crop_height;
     src_buffer = src->y_buffer;
     src_stride = src->y_stride;
     dgd_buffer = dgd->y_buffer;
@@ -1133,8 +1133,8 @@
   int h_start, h_end, v_start, v_end;
   int width, height;
   if (plane == AOM_PLANE_Y) {
-    width = cm->width;
-    height = cm->height;
+    width = src->y_crop_width;
+    height = src->y_crop_height;
   } else {
     width = src->uv_crop_width;
     height = src->uv_crop_height;
@@ -1165,8 +1165,8 @@
 }
 
 static double search_switchable_restoration(
-    AV1_COMP *cpi, int partial_frame, int plane, RestorationInfo *rsi,
-    double *tile_cost[RESTORE_SWITCHABLE_TYPES]) {
+    const YV12_BUFFER_CONFIG *src, AV1_COMP *cpi, int partial_frame, int plane,
+    RestorationInfo *rsi, double *tile_cost[RESTORE_SWITCHABLE_TYPES]) {
   AV1_COMMON *const cm = &cpi->common;
   MACROBLOCK *x = &cpi->td.mb;
   double cost_switchable = 0;
@@ -1174,11 +1174,11 @@
   RestorationType r;
   int width, height;
   if (plane == AOM_PLANE_Y) {
-    width = cm->width;
-    height = cm->height;
+    width = src->y_crop_width;
+    height = src->y_crop_height;
   } else {
-    width = ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x);
-    height = ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y);
+    width = src->uv_crop_width;
+    height = src->uv_crop_height;
   }
   const int ntiles = av1_get_rest_ntiles(
       width, height, cm->rst_info[plane].restoration_tilesize, NULL, NULL, NULL,
@@ -1243,14 +1243,17 @@
   RestorationType *restore_types[RESTORE_SWITCHABLE_TYPES];
   double best_cost_restore;
   RestorationType r, best_restore;
+  const int ywidth = src->y_crop_width;
+  const int yheight = src->y_crop_height;
+  const int uvwidth = src->uv_crop_width;
+  const int uvheight = src->uv_crop_height;
 
-  const int ntiles_y = av1_get_rest_ntiles(cm->width, cm->height,
-                                           cm->rst_info[0].restoration_tilesize,
-                                           NULL, NULL, NULL, NULL);
+  const int ntiles_y =
+      av1_get_rest_ntiles(ywidth, yheight, cm->rst_info[0].restoration_tilesize,
+                          NULL, NULL, NULL, NULL);
   const int ntiles_uv = av1_get_rest_ntiles(
-      ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
-      ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y),
-      cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL, NULL);
+      uvwidth, uvheight, cm->rst_info[1].restoration_tilesize, NULL, NULL, NULL,
+      NULL);
 
   // Assume ntiles_uv is never larger that ntiles_y and so the same arrays work.
   for (r = 0; r < RESTORE_SWITCHABLE_TYPES; r++) {
@@ -1270,9 +1273,9 @@
                                 tile_cost[r], &cpi->trial_frame_rst);
     }
     if (plane == AOM_PLANE_Y)
-      cost_restore[RESTORE_SWITCHABLE] =
-          search_switchable_restoration(cpi, method == LPF_PICK_FROM_SUBIMAGE,
-                                        plane, &cm->rst_info[plane], tile_cost);
+      cost_restore[RESTORE_SWITCHABLE] = search_switchable_restoration(
+          src, cpi, method == LPF_PICK_FROM_SUBIMAGE, plane,
+          &cm->rst_info[plane], tile_cost);
     else
       cost_restore[RESTORE_SWITCHABLE] = DBL_MAX;
     best_cost_restore = DBL_MAX;