Select filter level for U, V planes

Previously, U, V planes share the same filter level with Y.
Here, we search and pick the best filter level for U, V planes.
Selected filter levels are transmitted per frame.
This works with parallel_deblocking.

Coding gain on Google test set:
		Avg_psnr	ovr_psnr	ssim
lowres: 	-0.116		-0.120		-0.339
midres:		-0.218		-0.228		-0.338
hdres:		-0.260		-0.264		-0.365

Change-Id: I03d2ac47539f3eea9f3c4b08007bd6d3f4b73572
diff --git a/av1/encoder/picklpf.c b/av1/encoder/picklpf.c
index 287a6a6..26fd55e 100644
--- a/av1/encoder/picklpf.c
+++ b/av1/encoder/picklpf.c
@@ -38,13 +38,23 @@
 
 static int64_t try_filter_frame(const YV12_BUFFER_CONFIG *sd,
                                 AV1_COMP *const cpi, int filt_level,
-                                int partial_frame) {
+                                int partial_frame
+#if CONFIG_UV_LVL
+                                ,
+                                int plane
+#endif
+                                ) {
   AV1_COMMON *const cm = &cpi->common;
   int64_t filt_err;
 
 #if CONFIG_VAR_TX || CONFIG_EXT_PARTITION || CONFIG_CB4X4
+#if CONFIG_UV_LVL
+  av1_loop_filter_frame(cm->frame_to_show, cm, &cpi->td.mb.e_mbd, filt_level,
+                        plane, partial_frame);
+#else
   av1_loop_filter_frame(cm->frame_to_show, cm, &cpi->td.mb.e_mbd, filt_level, 1,
                         partial_frame);
+#endif  // CONFIG_UV_LVL
 #else
   if (cpi->num_workers > 1)
     av1_loop_filter_frame_mt(cm->frame_to_show, cm, cpi->td.mb.e_mbd.plane,
@@ -55,6 +65,40 @@
                           1, partial_frame);
 #endif
 
+#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
+
+  // 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);
+#else
 #if CONFIG_HIGHBITDEPTH
   if (cm->use_highbitdepth) {
     filt_err = aom_highbd_get_y_sse(sd, cm->frame_to_show);
@@ -67,12 +111,18 @@
 
   // Re-instate the unfiltered frame
   aom_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
+#endif  // CONFIG_UV_LVL
 
   return filt_err;
 }
 
 int av1_search_filter_level(const YV12_BUFFER_CONFIG *sd, AV1_COMP *cpi,
-                            int partial_frame, double *best_cost_ret) {
+                            int partial_frame, double *best_cost_ret
+#if CONFIG_UV_LVL
+                            ,
+                            int plane
+#endif
+                            ) {
   const AV1_COMMON *const cm = &cpi->common;
   const struct loopfilter *const lf = &cm->lf;
   const int min_filter_level = 0;
@@ -82,9 +132,20 @@
   int filt_best;
   MACROBLOCK *x = &cpi->td.mb;
 
-  // Start the search at the previous frame filter level unless it is now out of
-  // range.
+// Start the search at the previous frame filter level unless it is now out of
+// range.
+#if CONFIG_UV_LVL
+  int lvl;
+  switch (plane) {
+    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;
+  }
+  int filt_mid = clamp(lvl, min_filter_level, max_filter_level);
+#else
   int filt_mid = clamp(lf->filter_level, min_filter_level, max_filter_level);
+#endif  // CONFIG_UV_LVL
   int filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
   // Sum squared error at each filter level
   int64_t ss_err[MAX_LOOP_FILTER + 1];
@@ -92,10 +153,23 @@
   // Set each entry to -1
   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);
+#else
   //  Make a copy of the unfiltered / processed recon buffer
   aom_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
+#endif  // CONFIG_UV_LVL
 
+#if CONFIG_UV_LVL
+  best_err = try_filter_frame(sd, cpi, filt_mid, partial_frame, plane);
+#else
   best_err = try_filter_frame(sd, cpi, filt_mid, partial_frame);
+#endif  // CONFIG_UV_LVL
   filt_best = filt_mid;
   ss_err[filt_mid] = best_err;
 
@@ -115,7 +189,12 @@
     if (filt_direction <= 0 && filt_low != filt_mid) {
       // Get Low filter error score
       if (ss_err[filt_low] < 0) {
+#if CONFIG_UV_LVL
+        ss_err[filt_low] =
+            try_filter_frame(sd, cpi, filt_low, partial_frame, plane);
+#else
         ss_err[filt_low] = try_filter_frame(sd, cpi, filt_low, partial_frame);
+#endif  // CONFIG_UV_LVL
       }
       // If value is close to the best so far then bias towards a lower loop
       // filter value.
@@ -131,7 +210,12 @@
     // Now look at filt_high
     if (filt_direction >= 0 && filt_high != filt_mid) {
       if (ss_err[filt_high] < 0) {
+#if CONFIG_UV_LVL
+        ss_err[filt_high] =
+            try_filter_frame(sd, cpi, filt_high, partial_frame, plane);
+#else
         ss_err[filt_high] = try_filter_frame(sd, cpi, filt_high, partial_frame);
+#endif  // CONFIG_UV_LVL
       }
       // If value is significantly better than previous best, bias added against
       // raising filter value
@@ -197,7 +281,16 @@
     if (cm->frame_type == KEY_FRAME) filt_guess -= 4;
     lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
   } else {
+#if CONFIG_UV_LVL
+    lf->filter_level = av1_search_filter_level(
+        sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL, 0);
+    lf->filter_level_u = av1_search_filter_level(
+        sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL, 1);
+    lf->filter_level_v = av1_search_filter_level(
+        sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL, 2);
+#else
     lf->filter_level = av1_search_filter_level(
         sd, cpi, method == LPF_PICK_FROM_SUBIMAGE, NULL);
+#endif  // CONFIG_UV_LVL
   }
 }