Improve readability for experiment UV_LVL

Add asserts for switch.
Add helper functions to make selection of plane functions readable.
Remove extra { such that brackets match.

Change-Id: I048f3e5e0822bf1669fdcbedb84a0255139df686
diff --git a/aom_dsp/psnr.c b/aom_dsp/psnr.c
index 461c137..17404e1 100644
--- a/aom_dsp/psnr.c
+++ b/aom_dsp/psnr.c
@@ -289,6 +289,27 @@
 }
 #endif  // CONFIG_HIGHBITDEPTH
 
+int64_t aom_get_sse_plane(const YV12_BUFFER_CONFIG *a,
+                          const YV12_BUFFER_CONFIG *b, int plane, int highbd) {
+#if CONFIG_HIGHBITDEPTH
+  if (highbd) {
+    switch (plane) {
+      case 0: return aom_highbd_get_y_sse(a, b);
+      case 1: return aom_highbd_get_u_sse(a, b);
+      case 2: return aom_highbd_get_v_sse(a, b);
+      default: assert(plane >= 0 && plane <= 2); return 0;
+    }
+  }
+#endif
+  (void)highbd;
+  switch (plane) {
+    case 0: return aom_get_y_sse(a, b);
+    case 1: return aom_get_u_sse(a, b);
+    case 2: return aom_get_v_sse(a, b);
+    default: assert(plane >= 0 && plane <= 2); return 0;
+  }
+}
+
 #if CONFIG_HIGHBITDEPTH
 void aom_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
                           const YV12_BUFFER_CONFIG *b, PSNR_STATS *psnr,
diff --git a/aom_dsp/psnr.h b/aom_dsp/psnr.h
index 480140e..df5f8f9 100644
--- a/aom_dsp/psnr.h
+++ b/aom_dsp/psnr.h
@@ -47,6 +47,8 @@
                            const YV12_BUFFER_CONFIG *b, int hstart, int width,
                            int vstart, int height);
 int64_t aom_get_v_sse(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b);
+int64_t aom_get_sse_plane(const YV12_BUFFER_CONFIG *a,
+                          const YV12_BUFFER_CONFIG *b, int plane, int highbd);
 #if CONFIG_HIGHBITDEPTH
 int64_t aom_highbd_get_y_sse_part(const YV12_BUFFER_CONFIG *a,
                                   const YV12_BUFFER_CONFIG *b, int hstart,
diff --git a/av1/common/av1_loopfilter.c b/av1/common/av1_loopfilter.c
index 2e28b39..4ca4cb1 100644
--- a/av1/common/av1_loopfilter.c
+++ b/av1/common/av1_loopfilter.c
@@ -1910,8 +1910,8 @@
         orig_pos[i] = -1;
       }
 
-      int direct = pick_min_grad_direct(src, left_filt_len, row, col, width,
-                                        height, pitch, 1, 0);
+      const int direct = pick_min_grad_direct(src, left_filt_len, row, col,
+                                              width, height, pitch, 1, 0);
 
       pick_filter_block_vert(src, block, orig_pos, left_filt_len, row, col,
                              width, height, pitch, pivot, line_length, 1,
@@ -1940,8 +1940,8 @@
         orig_pos[i] = -1;
       }
 
-      int direct = pick_min_grad_direct(src, 4, row, col + 4, width, height,
-                                        pitch, 1, 0);
+      const int direct = pick_min_grad_direct(src, 4, row, col + 4, width,
+                                              height, pitch, 1, 0);
 
       pick_filter_block_vert(src, block, orig_pos, 4, row, col + 4, width,
                              height, pitch, pivot, line_length, 1, direct);
@@ -2927,7 +2927,7 @@
 
       if (params.filter_length) {
         const int filt_len = params.filter_length == 16 ? 8 : 4;
-        int direct =
+        const int direct =
             pick_min_grad_direct(src, filt_len, curr_y, curr_x, width, height,
                                  dst_stride, unit, vert_or_horz);
 
@@ -2987,7 +2987,7 @@
           orig_pos[i] = -1;
         }
 
-        int direct =
+        const int direct =
             pick_min_grad_direct(src, 4, curr_y, curr_x + 4, width, height,
                                  dst_stride, unit, vert_or_horz);
 
@@ -3113,7 +3113,7 @@
 
       if (params.filter_length) {
         const int filt_len = params.filter_length == 16 ? 8 : 4;
-        int direct =
+        const int direct =
             pick_min_grad_direct(src, filt_len, curr_y, curr_x, width, height,
                                  dst_stride, unit, vert_or_horz);
 
@@ -3173,7 +3173,7 @@
           orig_pos[i] = -1;
         }
 
-        int direct =
+        const int direct =
             pick_min_grad_direct(src, 4, curr_y + 4, curr_x, width, height,
                                  dst_stride, unit, vert_or_horz);
 
@@ -3269,8 +3269,11 @@
   const int plane_end = plane_start + 1;
 #else
   const int num_planes = y_only ? 1 : MAX_MB_PLANE;
+  const int plane_start = 0;
+  const int plane_end = num_planes;
 #endif  // CONFIG_UV_LVL
   int mi_row, mi_col;
+  int plane;
 
 #if CONFIG_VAR_TX || CONFIG_EXT_PARTITION || CONFIG_EXT_PARTITION_TYPES || \
     CONFIG_CB4X4
@@ -3288,15 +3291,9 @@
                                                      << TX_UNIT_HIGH_LOG2);
 #endif  // CONFIG_VAR_TX
     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += cm->mib_size) {
-      int plane;
-
       av1_setup_dst_planes(planes, cm->sb_size, frame_buffer, mi_row, mi_col);
 
-#if CONFIG_UV_LVL
       for (plane = plane_start; plane < plane_end; ++plane) {
-#else
-      for (plane = 0; plane < num_planes; ++plane) {
-#endif  // CONFIG_UV_LVL
         av1_filter_block_plane_non420_ver(cm, &planes[plane], mi + mi_col,
                                           mi_row, mi_col, plane);
         av1_filter_block_plane_non420_hor(cm, &planes[plane], mi + mi_col,
@@ -3310,13 +3307,8 @@
   for (mi_row = start; mi_row < stop; mi_row += MAX_MIB_SIZE) {
     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MAX_MIB_SIZE) {
       av1_setup_dst_planes(planes, cm->sb_size, frame_buffer, mi_row, mi_col);
-#if CONFIG_UV_LVL
-      for (int plane_idx = plane_start; plane_idx < plane_end; ++plane_idx) {
-#else
-      for (int plane_idx = 0; plane_idx < num_planes; plane_idx += 1) {
-#endif  // CONFIG_UV_LVL
-        av1_filter_block_plane_vert(cm, plane_idx, &planes[plane_idx], mi_row,
-                                    mi_col);
+      for (plane = plane_start; plane < plane_end; ++plane) {
+        av1_filter_block_plane_vert(cm, plane, &planes[plane], mi_row, mi_col);
       }
     }
   }
@@ -3325,13 +3317,8 @@
   for (mi_row = start; mi_row < stop; mi_row += MAX_MIB_SIZE) {
     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MAX_MIB_SIZE) {
       av1_setup_dst_planes(planes, cm->sb_size, frame_buffer, mi_row, mi_col);
-#if CONFIG_UV_LVL
-      for (int plane_idx = plane_start; plane_idx < plane_end; ++plane_idx) {
-#else
-      for (int plane_idx = 0; plane_idx < num_planes; plane_idx += 1) {
-#endif  // CONFIG_UV_LVL
-        av1_filter_block_plane_horz(cm, plane_idx, &planes[plane_idx], mi_row,
-                                    mi_col);
+      for (plane = plane_start; plane < plane_end; ++plane) {
+        av1_filter_block_plane_horz(cm, plane, &planes[plane], mi_row, mi_col);
       }
     }
   }
@@ -3344,9 +3331,8 @@
     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MAX_MIB_SIZE) {
       av1_setup_dst_planes(planes, cm->sb_size, frame_buffer, mi_row, mi_col);
       // filter all vertical edges in every 64x64 super block
-      for (int plane_idx = 0; plane_idx < num_planes; plane_idx += 1) {
-        av1_filter_block_plane_vert(cm, plane_idx, planes + plane_idx, mi_row,
-                                    mi_col);
+      for (plane = plane_start; plane < plane_end; plane += 1) {
+        av1_filter_block_plane_vert(cm, plane, &planes[plane], mi_row, mi_col);
       }
     }
   }
@@ -3354,9 +3340,8 @@
     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MAX_MIB_SIZE) {
       av1_setup_dst_planes(planes, cm->sb_size, frame_buffer, mi_row, mi_col);
       // filter all horizontal edges in every 64x64 super block
-      for (int plane_idx = 0; plane_idx < num_planes; plane_idx += 1) {
-        av1_filter_block_plane_horz(cm, plane_idx, planes + plane_idx, mi_row,
-                                    mi_col);
+      for (plane = plane_start; plane < plane_end; plane += 1) {
+        av1_filter_block_plane_horz(cm, plane, &planes[plane], mi_row, mi_col);
       }
     }
   }
@@ -3376,8 +3361,6 @@
   for (mi_row = start; mi_row < stop; mi_row += MAX_MIB_SIZE) {
     MODE_INFO **mi = cm->mi_grid_visible + mi_row * cm->mi_stride;
     for (mi_col = 0; mi_col < cm->mi_cols; mi_col += MAX_MIB_SIZE) {
-      int plane;
-
       av1_setup_dst_planes(planes, cm->sb_size, frame_buffer, mi_row, mi_col);
 
       // TODO(JBB): Make setup_mask work for non 420.
diff --git a/av1/encoder/picklpf.c b/av1/encoder/picklpf.c
index 26fd55e..da15d6a 100644
--- a/av1/encoder/picklpf.c
+++ b/av1/encoder/picklpf.c
@@ -27,6 +27,16 @@
 #include "av1/encoder/encoder.h"
 #include "av1/encoder/picklpf.h"
 
+static void yv12_copy_plane(const YV12_BUFFER_CONFIG *src_bc,
+                            YV12_BUFFER_CONFIG *dst_bc, int plane) {
+  switch (plane) {
+    case 0: aom_yv12_copy_y(src_bc, dst_bc); break;
+    case 1: aom_yv12_copy_u(src_bc, dst_bc); break;
+    case 2: aom_yv12_copy_v(src_bc, dst_bc); break;
+    default: assert(plane >= 0 && plane <= 2); break;
+  }
+}
+
 int av1_get_max_filter_level(const AV1_COMP *cpi) {
   if (cpi->oxcf.pass == 2) {
     return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
@@ -49,6 +59,7 @@
 
 #if CONFIG_VAR_TX || CONFIG_EXT_PARTITION || CONFIG_CB4X4
 #if CONFIG_UV_LVL
+  assert(plane >= 0 && plane <= 2);
   av1_loop_filter_frame(cm->frame_to_show, cm, &cpi->td.mb.e_mbd, filt_level,
                         plane, partial_frame);
 #else
@@ -65,52 +76,21 @@
                           1, partial_frame);
 #endif
 
+  int highbd = 0;
+#if CONFIG_HIGHBITDEPTH
+  highbd = cm->use_highbitdepth;
+#endif  // CONFIG_HIGHBITDEPTH
+
 #if CONFIG_UV_LVL
-#if CONFIG_HIGHBITDEPTH
-  if (cm->use_highbitdepth) {
-    if (plane == 0)
-      filt_err = aom_highbd_get_y_sse(sd, cm->frame_to_show);
-    else if (plane == 1)
-      filt_err = aom_highbd_get_u_sse(sd, cm->frame_to_show);
-    else
-      filt_err = aom_highbd_get_v_sse(sd, cm->frame_to_show);
-  } else {
-    if (plane == 0)
-      filt_err = aom_get_y_sse(sd, cm->frame_to_show);
-    else if (plane == 1)
-      filt_err = aom_get_u_sse(sd, cm->frame_to_show);
-    else
-      filt_err = aom_get_v_sse(sd, cm->frame_to_show);
-  }
-#else
-  if (plane == 0)
-    filt_err = aom_get_y_sse(sd, cm->frame_to_show);
-  else if (plane == 1)
-    filt_err = aom_get_u_sse(sd, cm->frame_to_show);
-  else
-    filt_err = aom_get_v_sse(sd, cm->frame_to_show);
-#endif  // CONFIG_HIGHBITDEPTH
+  filt_err = aom_get_sse_plane(sd, cm->frame_to_show, plane, highbd);
 
   // Re-instate the unfiltered frame
-  if (plane == 0)
-    aom_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
-  else if (plane == 1)
-    aom_yv12_copy_u(&cpi->last_frame_uf, cm->frame_to_show);
-  else
-    aom_yv12_copy_v(&cpi->last_frame_uf, cm->frame_to_show);
+  yv12_copy_plane(&cpi->last_frame_uf, cm->frame_to_show, plane);
 #else
-#if CONFIG_HIGHBITDEPTH
-  if (cm->use_highbitdepth) {
-    filt_err = aom_highbd_get_y_sse(sd, cm->frame_to_show);
-  } else {
-    filt_err = aom_get_y_sse(sd, cm->frame_to_show);
-  }
-#else
-  filt_err = aom_get_y_sse(sd, cm->frame_to_show);
-#endif  // CONFIG_HIGHBITDEPTH
+  filt_err = aom_get_sse_plane(sd, cm->frame_to_show, 0, highbd);
 
   // Re-instate the unfiltered frame
-  aom_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
+  yv12_copy_plane(&cpi->last_frame_uf, cm->frame_to_show, 0);
 #endif  // CONFIG_UV_LVL
 
   return filt_err;
@@ -140,7 +120,7 @@
     case 0: lvl = lf->filter_level; break;
     case 1: lvl = lf->filter_level_u; break;
     case 2: lvl = lf->filter_level_v; break;
-    default: lvl = lf->filter_level; break;
+    default: assert(plane >= 0 && plane <= 2); return 0;
   }
   int filt_mid = clamp(lvl, min_filter_level, max_filter_level);
 #else
@@ -154,12 +134,7 @@
   memset(ss_err, 0xFF, sizeof(ss_err));
 
 #if CONFIG_UV_LVL
-  if (plane == 0)
-    aom_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
-  else if (plane == 1)
-    aom_yv12_copy_u(cm->frame_to_show, &cpi->last_frame_uf);
-  else if (plane == 2)
-    aom_yv12_copy_v(cm->frame_to_show, &cpi->last_frame_uf);
+  yv12_copy_plane(cm->frame_to_show, &cpi->last_frame_uf, plane);
 #else
   //  Make a copy of the unfiltered / processed recon buffer
   aom_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);