Disable in-loop filters for frames with intrabc enabled

Change-Id: Ia47101de22091a60d7931890f00a4a3a527f5bd4
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 013172f..0cfbece 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -405,6 +405,8 @@
 } MODE_INFO;
 
 #if CONFIG_INTRABC
+#define NO_FILTER_FOR_IBC 1  // Disable in-loop filters for frame with intrabc
+
 static INLINE int is_intrabc_block(const MB_MODE_INFO *mbmi) {
   return mbmi->use_intrabc;
 }
diff --git a/av1/common/restoration.c b/av1/common/restoration.c
index 921e6c2..7f15a82 100644
--- a/av1/common/restoration.c
+++ b/av1/common/restoration.c
@@ -1683,6 +1683,7 @@
   assert(rcol0 && rcol1 && rrow0 && rrow1);
 
   if (bsize != cm->sb_size) return 0;
+  if (cm->rst_info[plane].frame_restoration_type == RESTORE_NONE) return 0;
 
   const int is_uv = plane > 0;
 
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index ecc0baf..e8734c1 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1142,6 +1142,9 @@
 #if CONFIG_LOOP_RESTORATION
 static void decode_restoration_mode(AV1_COMMON *cm,
                                     struct aom_read_bit_buffer *rb) {
+#if CONFIG_INTRABC
+  if (cm->allow_intrabc && NO_FILTER_FOR_IBC) return;
+#endif  // CONFIG_INTRABC
   for (int p = 0; p < MAX_MB_PLANE; ++p) {
     RestorationInfo *rsi = &cm->rst_info[p];
     if (aom_rb_read_bit(rb)) {
@@ -1309,6 +1312,9 @@
 #endif  // CONFIG_LOOP_RESTORATION
 
 static void setup_loopfilter(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
+#if CONFIG_INTRABC
+  if (cm->allow_intrabc && NO_FILTER_FOR_IBC) return;
+#endif  // CONFIG_INTRABC
   struct loopfilter *lf = &cm->lf;
 #if !CONFIG_LPF_SB
 #if CONFIG_LOOPFILTER_LEVEL
@@ -1347,6 +1353,9 @@
 
 #if CONFIG_CDEF
 static void setup_cdef(AV1_COMMON *cm, struct aom_read_bit_buffer *rb) {
+#if CONFIG_INTRABC
+  if (cm->allow_intrabc && NO_FILTER_FOR_IBC) return;
+#endif  // CONFIG_INTRABC
   int i;
 #if CONFIG_CDEF_SINGLEPASS
   cm->cdef_pri_damping = cm->cdef_sec_damping = aom_rb_read_literal(rb, 2) + 3;
@@ -2423,34 +2432,35 @@
   }
 
 #if CONFIG_INTRABC
-// When intraBC is on, do loop filtering per superblock,
-// instead of do it after the whole frame has been encoded,
-// as is in the else branch
-#else
+  if (!(cm->allow_intrabc && NO_FILTER_FOR_IBC))
+#endif  // CONFIG_INTRABC
+  {
 // Loopfilter the whole frame.
 #if CONFIG_LPF_SB
-  av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
-                        cm->lf.filter_level, 0, 0, 0, 0);
+    av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
+                          cm->lf.filter_level, 0, 0, 0, 0);
 #else
 #if CONFIG_OBU
-  if (endTile == cm->tile_rows * cm->tile_cols - 1)
+    if (endTile == cm->tile_rows * cm->tile_cols - 1)
 #endif
 #if CONFIG_LOOPFILTER_LEVEL
-    if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
-      av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
-                            cm->lf.filter_level[0], cm->lf.filter_level[1], 0,
-                            0);
-      av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
-                            cm->lf.filter_level_u, cm->lf.filter_level_u, 1, 0);
-      av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
-                            cm->lf.filter_level_v, cm->lf.filter_level_v, 2, 0);
-    }
+      if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
+        av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
+                              cm->lf.filter_level[0], cm->lf.filter_level[1], 0,
+                              0);
+        av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
+                              cm->lf.filter_level_u, cm->lf.filter_level_u, 1,
+                              0);
+        av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
+                              cm->lf.filter_level_v, cm->lf.filter_level_v, 2,
+                              0);
+      }
 #else
-  av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
-                        cm->lf.filter_level, 0, 0);
+    av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
+                          cm->lf.filter_level, 0, 0);
 #endif  // CONFIG_LOOPFILTER_LEVEL
 #endif  // CONFIG_LPF_SB
-#endif  // CONFIG_INTRABC
+  }
   if (cm->frame_parallel_decode)
     av1_frameworker_broadcast(pbi->cur_buf, INT_MAX);
 
@@ -3182,6 +3192,29 @@
   if (frame_is_intra_only(cm) || cm->error_resilient_mode)
     av1_setup_past_independence(cm);
 
+#if CONFIG_INTRABC
+  if (cm->allow_intrabc && NO_FILTER_FOR_IBC) {
+    // Set parameters corresponding to no filtering.
+    struct loopfilter *lf = &cm->lf;
+#if CONFIG_LOOPFILTER_LEVEL
+    lf->filter_level[0] = 0;
+    lf->filter_level[1] = 0;
+#else
+    lf->filter_level = 0;
+#endif
+#if CONFIG_CDEF
+    cm->cdef_bits = 0;
+    cm->cdef_strengths[0] = 0;
+    cm->nb_cdef_strengths = 1;
+#endif  // CONFIG_CDEF
+#if CONFIG_LOOP_RESTORATION
+    cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
+    cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
+    cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
+#endif  // CONFIG_LOOP_RESTORATION
+  }
+#endif  // CONFIG_INTRABC
+
   setup_loopfilter(cm, rb);
   setup_quantization(cm, rb);
   xd->bd = (int)cm->bit_depth;
@@ -3810,7 +3843,11 @@
 #endif
 
 #if CONFIG_CDEF
-  if (!cm->skip_loop_filter && !cm->all_lossless) {
+  if (!cm->skip_loop_filter &&
+#if CONFIG_INTRABC
+      !(cm->allow_intrabc && NO_FILTER_FOR_IBC) &&
+#endif  // CONFIG_INTRABC
+      !cm->all_lossless) {
     av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
   }
 #endif  // CONFIG_CDEF
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 0798cfb..8d8a664 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -2532,6 +2532,9 @@
 #if CONFIG_LOOP_RESTORATION
 static void encode_restoration_mode(AV1_COMMON *cm,
                                     struct aom_write_bit_buffer *wb) {
+#if CONFIG_INTRABC
+  if (cm->allow_intrabc && NO_FILTER_FOR_IBC) return;
+#endif  // CONFIG_INTRABC
   for (int p = 0; p < MAX_MB_PLANE; ++p) {
     RestorationInfo *rsi = &cm->rst_info[p];
     switch (rsi->frame_restoration_type) {
@@ -2693,6 +2696,9 @@
 #endif  // CONFIG_LOOP_RESTORATION
 
 static void encode_loopfilter(AV1_COMMON *cm, struct aom_write_bit_buffer *wb) {
+#if CONFIG_INTRABC
+  if (cm->allow_intrabc && NO_FILTER_FOR_IBC) return;
+#endif  // CONFIG_INTRABC
   int i;
   struct loopfilter *lf = &cm->lf;
 
@@ -2743,6 +2749,9 @@
 
 #if CONFIG_CDEF
 static void encode_cdef(const AV1_COMMON *cm, struct aom_write_bit_buffer *wb) {
+#if CONFIG_INTRABC
+  if (cm->allow_intrabc && NO_FILTER_FOR_IBC) return;
+#endif  // CONFIG_INTRABC
   int i;
 #if CONFIG_CDEF_SINGLEPASS
   aom_wb_write_literal(wb, cm->cdef_pri_damping - 3, 2);
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 7a33add..807b706 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -4616,9 +4616,12 @@
 #if CONFIG_LOOP_RESTORATION
   int no_restoration = 0;
   if (is_lossless_requested(&cpi->oxcf)
+#if CONFIG_INTRABC
+      || (cm->allow_intrabc && NO_FILTER_FOR_IBC)
+#endif  // CONFIG_INTRABC
 #if CONFIG_EXT_TILE
       || cm->single_tile_decoding
-#endif
+#endif  // CONFIG_EXT_TILE
       ) {
     no_loopfilter = 1;
     no_restoration = 1;
@@ -4655,11 +4658,6 @@
     cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
   }
 
-#if CONFIG_INTRABC
-// When intraBC is on, do loop filtering per superblock,
-// instead of do it after the whole frame has been encoded,
-// as is in the else branch
-#else
 #if !CONFIG_LPF_SB
 #if CONFIG_LOOPFILTER_LEVEL
   if (lf->filter_level[0] || lf->filter_level[1])
@@ -4685,7 +4683,6 @@
 #endif  // CONFIG_LOOPFILTER_LEVEL
 #endif  // CONFIG_LPF_SB
   }
-#endif  // CONFIG_INTRABC
 
 #if CONFIG_STRIPED_LOOP_RESTORATION
 #if CONFIG_FRAME_SUPERRES && CONFIG_HORZONLY_FRAME_SUPERRES