CNN Restoration: separate use cnn flags per plane.

Instead of two frame level flags use_cnn_y and use_cnn_uv, we now have 3
flags for Y, U and V for more flexibility.

Change-Id: I312351ee23e0e729f846010ceaa1387a5476f010
diff --git a/av1/common/av1_common_int.h b/av1/common/av1_common_int.h
index a26a462..11006e2 100644
--- a/av1/common/av1_common_int.h
+++ b/av1/common/av1_common_int.h
@@ -1064,14 +1064,9 @@
 
 #if CONFIG_CNN_RESTORATION
   /*!
-   * True if luma channel is using CNN restoration.
+   * `use_cnn[p]` is true if plane `p` is using CNN restoration.
    */
-  int use_cnn_y;
-  /*!
-   * True if chroma channels are using CNN restoration.
-   */
-  // TODO(urvang): Separate out U and V?
-  int use_cnn_uv;
+  int use_cnn[MAX_MB_PLANE];
 #endif  // CONFIG_CNN_RESTORATION
 } AV1_COMMON;
 
diff --git a/av1/common/cnn_tflite.cc b/av1/common/cnn_tflite.cc
index d0c7594..5b8018b 100644
--- a/av1/common/cnn_tflite.cc
+++ b/av1/common/cnn_tflite.cc
@@ -268,10 +268,11 @@
 }
 
 extern "C" void av1_restore_cnn_tflite(const AV1_COMMON *cm, int num_threads,
-                                       int plane_from, int plane_to) {
+                                       const int apply_cnn[MAX_MB_PLANE]) {
   YV12_BUFFER_CONFIG *buf = &cm->cur_frame->buf;
   const int is_intra_only = frame_is_intra_only(cm);
-  for (int plane = plane_from; plane <= plane_to; ++plane) {
+  for (int plane = 0; plane < av1_num_planes(cm); ++plane) {
+    if (!apply_cnn[plane]) continue;
     const int is_luma = (plane == AOM_PLANE_Y);
     if (cm->seq_params.use_highbitdepth) {
       switch (plane) {
diff --git a/av1/common/cnn_tflite.h b/av1/common/cnn_tflite.h
index cd75402..bcff5f9 100644
--- a/av1/common/cnn_tflite.h
+++ b/av1/common/cnn_tflite.h
@@ -55,8 +55,9 @@
 struct AV1Common;
 
 // Restore current frame buffer in 'cm' in-place with a CNN model using TFlite.
+// Apply CNN to plane 'p' if and only if apply_cnn[p] is true.
 void av1_restore_cnn_tflite(const struct AV1Common *cm, int num_threads,
-                            int plane_from, int plane_to);
+                            const int apply_cnn[MAX_MB_PLANE]);
 
 #ifdef __cplusplus
 }
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 9908a1b..9cd6888 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1327,9 +1327,7 @@
     const int num_planes = av1_num_planes(cm);
     for (int plane = 0; plane < num_planes; ++plane) {
 #if CONFIG_CNN_RESTORATION
-      if ((plane == 0 && cm->use_cnn_y) || (plane > 0 && cm->use_cnn_uv)) {
-        continue;
-      }
+      if (cm->use_cnn[plane]) continue;
 #endif  // CONFIG_CNN_RESTORATION
       int rcol0, rcol1, rrow0, rrow1;
       if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
@@ -1533,11 +1531,11 @@
 #if CONFIG_CNN_RESTORATION
 static void decode_cnn(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
   if (av1_use_cnn(cm)) {
-    cm->use_cnn_y = aom_rb_read_bit(rb);
-    cm->use_cnn_uv = aom_rb_read_bit(rb);
+    for (int plane = 0; plane < av1_num_planes(cm); ++plane) {
+      cm->use_cnn[plane] = aom_rb_read_bit(rb);
+    }
   } else {
-    cm->use_cnn_y = 0;
-    cm->use_cnn_uv = 0;
+    memset(cm->use_cnn, 0, sizeof(cm->use_cnn));
   }
 }
 #endif  // CONFIG_CNN_RESTORATION
@@ -1551,7 +1549,7 @@
   for (int p = 0; p < num_planes; ++p) {
     RestorationInfo *rsi = &cm->rst_info[p];
 #if CONFIG_CNN_RESTORATION
-    if ((p == 0 && cm->use_cnn_y) || (p > 0 && cm->use_cnn_uv)) {
+    if (cm->use_cnn[p]) {
       rsi->frame_restoration_type = RESTORE_NONE;
       continue;
     }
@@ -5147,8 +5145,7 @@
   }
   setup_loopfilter(cm, rb);
 #if CONFIG_CNN_RESTORATION
-  cm->use_cnn_y = 0;
-  cm->use_cnn_uv = 0;
+  memset(cm->use_cnn, 0, sizeof(cm->use_cnn));
   if (!features->coded_lossless) decode_cnn(cm, rb);
 #endif  // CONFIG_CNN_RESTORATION
   if (!features->coded_lossless && seq_params->enable_cdef) {
@@ -5370,11 +5367,12 @@
     }
 
 #if CONFIG_CNN_RESTORATION
-    assert(IMPLIES(cm->use_cnn_y,
+    assert(IMPLIES(cm->use_cnn[0],
                    cm->rst_info[0].frame_restoration_type == RESTORE_NONE));
-    assert(IMPLIES(cm->use_cnn_uv,
-                   cm->rst_info[1].frame_restoration_type == RESTORE_NONE &&
-                       cm->rst_info[2].frame_restoration_type == RESTORE_NONE));
+    assert(IMPLIES(cm->use_cnn[1],
+                   cm->rst_info[1].frame_restoration_type == RESTORE_NONE));
+    assert(IMPLIES(cm->use_cnn[2],
+                   cm->rst_info[2].frame_restoration_type == RESTORE_NONE));
 #endif  // CONFIG_CNN_RESTORATION
 
     const int do_loop_restoration =
@@ -5399,14 +5397,8 @@
 
       superres_post_decode(pbi);
 
-      // TODO(urvang): Refactor.
 #if CONFIG_CNN_RESTORATION
-      if (cm->use_cnn_y || cm->use_cnn_uv) {
-        const int plane_from = cm->use_cnn_y ? AOM_PLANE_Y : AOM_PLANE_U;
-        const int plane_to =
-            cm->use_cnn_uv ? av1_num_planes(cm) - 1 : AOM_PLANE_Y;
-        av1_restore_cnn_tflite(cm, pbi->num_workers, plane_from, plane_to);
-      }
+      av1_restore_cnn_tflite(cm, pbi->num_workers, cm->use_cnn);
 #endif  // CONFIG_CNN_RESTORATION
 
       if (do_loop_restoration) {
@@ -5427,12 +5419,7 @@
       // In no cdef and no superres case. Provide an optimized version of
       // loop_restoration_filter.
 #if CONFIG_CNN_RESTORATION
-      if (cm->use_cnn_y || cm->use_cnn_uv) {
-        const int plane_from = cm->use_cnn_y ? AOM_PLANE_Y : AOM_PLANE_U;
-        const int plane_to =
-            cm->use_cnn_uv ? av1_num_planes(cm) - 1 : AOM_PLANE_Y;
-        av1_restore_cnn_tflite(cm, pbi->num_workers, plane_from, plane_to);
-      }
+      av1_restore_cnn_tflite(cm, pbi->num_workers, cm->use_cnn);
 #endif  // CONFIG_CNN_RESTORATION
 
       if (do_loop_restoration) {
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 55aa458..f484e58 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -1636,9 +1636,7 @@
   const int num_planes = av1_num_planes(cm);
   for (int plane = 0; plane < num_planes; ++plane) {
 #if CONFIG_CNN_RESTORATION
-    if ((plane == 0 && cm->use_cnn_y) || (plane > 0 && cm->use_cnn_uv)) {
-      continue;
-    }
+    if (cm->use_cnn[plane]) continue;
 #endif  // CONFIG_CNN_RESTORATION
     int rcol0, rcol1, rrow0, rrow1;
     if (av1_loop_restoration_corners_in_sb(cm, plane, mi_row, mi_col, bsize,
@@ -1774,7 +1772,7 @@
   for (int p = 0; p < num_planes; ++p) {
     RestorationInfo *rsi = &cm->rst_info[p];
 #if CONFIG_CNN_RESTORATION
-    if ((p == 0 && cm->use_cnn_y) || (p > 0 && cm->use_cnn_uv)) {
+    if (cm->use_cnn[p]) {
       assert(rsi->frame_restoration_type == RESTORE_NONE);
       continue;
     }
@@ -2089,11 +2087,13 @@
 #if CONFIG_CNN_RESTORATION
 static void encode_cnn(AV1_COMMON *cm, struct aom_write_bit_buffer *wb) {
   if (av1_use_cnn(cm)) {
-    aom_wb_write_bit(wb, cm->use_cnn_y);
-    aom_wb_write_bit(wb, cm->use_cnn_uv);
+    for (int plane = 0; plane < av1_num_planes(cm); ++plane) {
+      aom_wb_write_bit(wb, cm->use_cnn[plane]);
+    }
   } else {
-    assert(!cm->use_cnn_y);
-    assert(!cm->use_cnn_uv);
+    for (int plane = 0; plane < av1_num_planes(cm); ++plane) {
+      assert(!cm->use_cnn[plane]);
+    }
   }
 }
 #endif  // CONFIG_CNN_RESTORATION
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index ef7b339..addec09 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -2000,17 +2000,16 @@
   yv12_copy_chroma_planes(src, dst, num_planes);
 }
 
-/*!\brief Returns SSE between 'a' and 'b' in luma chanel (Y plane) and chroma
- * channels (total for U and V planes).
+/*!\brief Returns SSE between 'a' and 'b' for each plane.
  */
-static void get_sse_luma_chroma(const YV12_BUFFER_CONFIG *a,
-                                const YV12_BUFFER_CONFIG *b, int highbd,
-                                int64_t *sse_y, int64_t *sse_uv,
-                                int num_planes) {
-  *sse_y = aom_get_sse_plane(a, b, AOM_PLANE_Y, highbd);
-  *sse_uv = (num_planes == 3) ? aom_get_sse_plane(a, b, AOM_PLANE_U, highbd) +
-                                    aom_get_sse_plane(a, b, AOM_PLANE_V, highbd)
-                              : INT64_MAX;
+static void get_sse_planes(const YV12_BUFFER_CONFIG *a,
+                           const YV12_BUFFER_CONFIG *b, int highbd,
+                           int64_t *sse, int num_planes) {
+  sse[0] = aom_get_sse_plane(a, b, AOM_PLANE_Y, highbd);
+  sse[1] = (num_planes > 1) ? aom_get_sse_plane(a, b, AOM_PLANE_U, highbd)
+                            : INT64_MAX;
+  sse[2] = (num_planes > 2) ? aom_get_sse_plane(a, b, AOM_PLANE_V, highbd)
+                            : INT64_MAX;
 }
 #endif  // CONFIG_CNN_RESTORATION
 
@@ -2051,12 +2050,15 @@
 #if CONFIG_COLLECT_COMPONENT_TIMING
   start_timing(cpi, loop_restoration_time);
 #endif
+
+#if CONFIG_CNN_RESTORATION
+  memset(cm->use_cnn, 0, sizeof(cm->use_cnn));
+#endif  // CONFIG_CNN_RESTORATION
+
   if (use_restoration) {
     av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
 
 #if CONFIG_CNN_RESTORATION
-    cm->use_cnn_y = 0;
-    cm->use_cnn_uv = 0;
     if (av1_use_cnn_encode(cm,
                            cpi->gf_group.update_type[cpi->gf_group.index])) {
       const int num_planes = av1_num_planes(cm);
@@ -2065,28 +2067,28 @@
                            num_planes);
 
       // Calculate errors for unfiltered planes from source.
-      int64_t dgd_error_y, dgd_error_uv;
-      get_sse_luma_chroma(cpi->source, &cm->cur_frame->buf,
-                          cm->seq_params.use_highbitdepth, &dgd_error_y,
-                          &dgd_error_uv, num_planes);
+      int64_t dgd_errors[MAX_MB_PLANE];
+      get_sse_planes(cpi->source, &cm->cur_frame->buf,
+                     cm->seq_params.use_highbitdepth, dgd_errors, num_planes);
 
       // Try CNN restoration on all planes.
-      av1_restore_cnn_tflite(cm, cpi->mt_info.num_workers, AOM_PLANE_Y,
-                             num_planes - 1);
+      const int apply_cnn[MAX_MB_PLANE] = { 1, 1, 1 };
+      av1_restore_cnn_tflite(cm, cpi->mt_info.num_workers, apply_cnn);
 
       // Calculate errors after applying cnn from source.
-      int64_t cnn_error_y, cnn_error_uv;
-      get_sse_luma_chroma(cpi->source, &cm->cur_frame->buf,
-                          cm->seq_params.use_highbitdepth, &cnn_error_y,
-                          &cnn_error_uv, num_planes);
+      int64_t cnn_errors[MAX_MB_PLANE];
+      get_sse_planes(cpi->source, &cm->cur_frame->buf,
+                     cm->seq_params.use_highbitdepth, cnn_errors, num_planes);
 
       // Save CNN restored frame.
-      if (cnn_error_y <= dgd_error_y) {
+      if (cnn_errors[0] <= dgd_errors[0]) {
         aom_yv12_copy_y(&cm->cur_frame->buf, &cpi->cnn_buffer);
       }
-      if (cnn_error_uv <= dgd_error_uv) {
-        yv12_copy_chroma_planes(&cm->cur_frame->buf, &cpi->cnn_buffer,
-                                num_planes);
+      if (cnn_errors[1] <= dgd_errors[1] && num_planes > 1) {
+        aom_yv12_copy_u(&cm->cur_frame->buf, &cpi->cnn_buffer);
+      }
+      if (cnn_errors[2] <= dgd_errors[2] && num_planes > 2) {
+        aom_yv12_copy_v(&cm->cur_frame->buf, &cpi->cnn_buffer);
       }
 
       // Restore unfiltered frame.
@@ -2109,38 +2111,41 @@
       }
 
       // Calculate errors after LR from source.
-      int64_t res_error_y, res_error_uv;
-      get_sse_luma_chroma(cpi->source, &cm->cur_frame->buf,
-                          cm->seq_params.use_highbitdepth, &res_error_y,
-                          &res_error_uv, num_planes);
+      int64_t res_errors[MAX_MB_PLANE];
+      get_sse_planes(cpi->source, &cm->cur_frame->buf,
+                     cm->seq_params.use_highbitdepth, res_errors, num_planes);
 
-      // For each of luma and chroma, pick either CNN or LR (mutually
-      // exclusive).
-      if (cnn_error_y <= res_error_y && cnn_error_y <= dgd_error_y) {
-        // Enable CNN for luma and copy CNN restored luma plane.
-        cm->use_cnn_y = 1;
+      // For each plane, pick either CNN or LR (mutually exclusive).
+      if (cnn_errors[0] <= res_errors[0] && cnn_errors[0] <= dgd_errors[0]) {
+        // Enable CNN for Y and copy CNN restored Y plane.
+        cm->use_cnn[0] = 1;
         aom_yv12_copy_y(&cpi->cnn_buffer, &cm->cur_frame->buf);
       } else {
         aom_yv12_copy_y(&cpi->last_frame_uf, &cm->cur_frame->buf);
       }
-      if (cnn_error_uv <= res_error_uv && cnn_error_uv <= dgd_error_uv) {
-        // Enable CNN for chroma and copy CNN restored chroma planes.
-        cm->use_cnn_uv = 1;
-        yv12_copy_chroma_planes(&cpi->cnn_buffer, &cm->cur_frame->buf,
-                                num_planes);
+      if (cnn_errors[1] <= res_errors[1] && cnn_errors[1] <= dgd_errors[1] &&
+          num_planes > 1) {
+        // Enable CNN for U and copy CNN restored U plane.
+        cm->use_cnn[1] = 1;
+        aom_yv12_copy_u(&cpi->cnn_buffer, &cm->cur_frame->buf);
       } else {
-        yv12_copy_chroma_planes(&cpi->last_frame_uf, &cm->cur_frame->buf,
-                                num_planes);
+        aom_yv12_copy_u(&cpi->last_frame_uf, &cm->cur_frame->buf);
+      }
+      if (cnn_errors[2] <= res_errors[2] && cnn_errors[2] <= dgd_errors[2] &&
+          num_planes > 2) {
+        // Enable CNN for V and copy CNN restored V plane.
+        cm->use_cnn[2] = 1;
+        aom_yv12_copy_v(&cpi->cnn_buffer, &cm->cur_frame->buf);
+      } else {
+        aom_yv12_copy_v(&cpi->last_frame_uf, &cm->cur_frame->buf);
       }
 
       // Disable LR for planes where CNN is used, and reapply LR for other
       // planes.
-      if (cm->use_cnn_y) {
-        cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
-      }
-      if (cm->use_cnn_uv) {
-        cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
-        cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
+      for (int plane = 0; plane < av1_num_planes(cm); ++plane) {
+        if (cm->use_cnn[plane]) {
+          cm->rst_info[plane].frame_restoration_type = RESTORE_NONE;
+        }
       }
       if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
           cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
@@ -2154,8 +2159,11 @@
                                             &cpi->lr_ctxt);
       }
       /*
-      fprintf(stderr, "\ngf_index = %d, use_cnn_y = %d, use_cnn_uv = %d\n",
-              cpi->gf_group.index, cm->use_cnn_y, cm->use_cnn_uv);
+      fprintf(stderr,
+              "\ngf_index = %d, use_cnn[0] = %d, use_cnn[1] = %d, "
+              "use_cnn[2] = %d\n",
+              cpi->gf_group.index, cm->use_cnn[0], cm->use_cnn[1],
+              cm->use_cnn[2]);
       */
     } else {
 #endif  // CONFIG_CNN_RESTORATION
@@ -2760,8 +2768,7 @@
     cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
     cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
 #if CONFIG_CNN_RESTORATION
-    cm->use_cnn_y = 0;
-    cm->use_cnn_uv = 0;
+    memset(cm->use_cnn, 0, sizeof(cm->use_cnn));
 #endif  // CONFIG_CNN_RESTORATION
   }