Refactoring/simplification of buffers used for sgr

Inlcudes miscellaneous cleanups, test fixes, and code reorganization
for loop-restoration components.

Change-Id: I5b2e6419234d945e6f4344b22636119b50df4054
diff --git a/av1/encoder/pickrst.c b/av1/encoder/pickrst.c
index 98fbfb5..5650a48 100644
--- a/av1/encoder/pickrst.c
+++ b/av1/encoder/pickrst.c
@@ -29,9 +29,9 @@
 
 #include "av1/encoder/av1_quantize.h"
 #include "av1/encoder/encoder.h"
+#include "av1/encoder/mathutils.h"
 #include "av1/encoder/picklpf.h"
 #include "av1/encoder/pickrst.h"
-#include "av1/encoder/mathutils.h"
 
 // When set to RESTORE_WIENER or RESTORE_SGRPROJ only those are allowed.
 // When set to RESTORE_TYPES we allow switchable.
@@ -354,7 +354,6 @@
                                           int32_t *rstbuf) {
   int32_t *flt1 = rstbuf;
   int32_t *flt2 = flt1 + RESTORATION_TILEPELS_MAX;
-  int32_t *tmpbuf2 = flt2 + RESTORATION_TILEPELS_MAX;
   int ep, bestep = 0;
   int64_t err, besterr = -1;
   int exqd[2], bestxqd[2] = { 0, 0 };
@@ -387,11 +386,11 @@
 #else
           av1_selfguided_restoration_highbd(
               dat_p, w, h, dat_stride, flt1_p, flt1_stride, bit_depth,
-              sgr_params[ep].r1, sgr_params[ep].e1, tmpbuf2);
+              sgr_params[ep].r1, sgr_params[ep].e1);
 #endif  // USE_HIGHPASS_IN_SGRPROJ
           av1_selfguided_restoration_highbd(
               dat_p, w, h, dat_stride, flt2_p, flt2_stride, bit_depth,
-              sgr_params[ep].r2, sgr_params[ep].e2, tmpbuf2);
+              sgr_params[ep].r2, sgr_params[ep].e2);
         }
     } else {
 #endif
@@ -407,12 +406,11 @@
                               sgr_params[ep].corner, sgr_params[ep].edge);
 #else
         av1_selfguided_restoration(dat_p, w, h, dat_stride, flt1_p, flt1_stride,
-                                   sgr_params[ep].r1, sgr_params[ep].e1,
-                                   tmpbuf2);
+                                   sgr_params[ep].r1, sgr_params[ep].e1);
 #endif  // USE_HIGHPASS_IN_SGRPROJ
           av1_selfguided_restoration(dat_p, w, h, dat_stride, flt2_p,
                                      flt2_stride, sgr_params[ep].r2,
-                                     sgr_params[ep].e2, tmpbuf2);
+                                     sgr_params[ep].e2);
         }
 #if CONFIG_HIGHBITDEPTH
     }
@@ -640,11 +638,12 @@
 #if CONFIG_HIGHBITDEPTH
   if (cm->use_highbitdepth)
     extend_frame_highbd(CONVERT_TO_SHORTPTR(ctxt.dgd_buffer), ctxt.plane_width,
-                        ctxt.plane_height, ctxt.dgd_stride);
+                        ctxt.plane_height, ctxt.dgd_stride, SGRPROJ_BORDER_HORZ,
+                        SGRPROJ_BORDER_VERT);
   else
 #endif
     extend_frame(ctxt.dgd_buffer, ctxt.plane_width, ctxt.plane_height,
-                 ctxt.dgd_stride);
+                 ctxt.dgd_stride, SGRPROJ_BORDER_HORZ, SGRPROJ_BORDER_VERT);
 
   for (int tile_row = 0; tile_row < cm->tile_rows; ++tile_row) {
     for (int tile_col = 0; tile_col < cm->tile_cols; ++tile_col) {
@@ -1242,14 +1241,17 @@
 
   AV1_COMMON *const cm = &cpi->common;
 // Construct a (WIENER_HALFWIN)-pixel border around the frame
+// Note use this border to gather stats even though the actual filter
+// may use less border on the top/bottom of a processing unit.
 #if CONFIG_HIGHBITDEPTH
   if (cm->use_highbitdepth)
     extend_frame_highbd(CONVERT_TO_SHORTPTR(ctxt.dgd_buffer), ctxt.plane_width,
-                        ctxt.plane_height, ctxt.dgd_stride);
+                        ctxt.plane_height, ctxt.dgd_stride, WIENER_HALFWIN,
+                        WIENER_HALFWIN);
   else
 #endif
     extend_frame(ctxt.dgd_buffer, ctxt.plane_width, ctxt.plane_height,
-                 ctxt.dgd_stride);
+                 ctxt.dgd_stride, WIENER_HALFWIN, WIENER_HALFWIN);
 
   // Compute best Wiener filters for each rtile, one (encoder/decoder)
   // tile at a time.