Add flag to indicate if intrabc is used

Change-Id: If8a4f3d256e619c9efdb66b1d8b8da5b8fc980ab
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 5687de2..69b86ab 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -278,6 +278,9 @@
 #endif
 
   int allow_screen_content_tools;
+#if CONFIG_INTRABC
+  int allow_intrabc;
+#endif  // CONFIG_INTRABC
   int allow_interintra_compound;
   int allow_masked_compound;
 
diff --git a/av1/common/reconintra.h b/av1/common/reconintra.h
index b821752..6fbc135 100644
--- a/av1/common/reconintra.h
+++ b/av1/common/reconintra.h
@@ -75,7 +75,7 @@
   // TODO(huisu@google.com): intrabc is disabled for BLOCK_4X16 and
   // BLOCK_16X4 because of onflict between cfl.
   return bsize != BLOCK_4X16 && bsize != BLOCK_16X4 &&
-         cm->allow_screen_content_tools;
+         cm->allow_screen_content_tools && cm->allow_intrabc;
 }
 #endif  // CONFIG_INTRABC
 
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index bc11ea2..305bb32 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -2843,6 +2843,10 @@
   int frame_size_override_flag = aom_rb_read_literal(rb, 1);
 #endif
 
+#if CONFIG_INTRABC
+  cm->allow_intrabc = 0;
+#endif  // CONFIG_INTRABC
+
   if (cm->frame_type == KEY_FRAME) {
     cm->current_video_frame = 0;
 #if !CONFIG_OBU
@@ -2870,6 +2874,9 @@
     cm->ans_window_size_log2 = aom_rb_read_literal(rb, 4) + 8;
 #endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
     cm->allow_screen_content_tools = aom_rb_read_bit(rb);
+#if CONFIG_INTRABC
+    if (cm->allow_screen_content_tools) cm->allow_intrabc = aom_rb_read_bit(rb);
+#endif  // CONFIG_INTRABC
 #if CONFIG_AMVR
     if (cm->allow_screen_content_tools) {
       if (aom_rb_read_bit(rb)) {
@@ -2885,7 +2892,13 @@
     cm->use_prev_frame_mvs = 0;
 #endif
   } else {
-    if (cm->intra_only) cm->allow_screen_content_tools = aom_rb_read_bit(rb);
+    if (cm->intra_only) {
+      cm->allow_screen_content_tools = aom_rb_read_bit(rb);
+#if CONFIG_INTRABC
+      if (cm->allow_screen_content_tools)
+        cm->allow_intrabc = aom_rb_read_bit(rb);
+#endif  // CONFIG_INTRABC
+    }
 #if CONFIG_TEMPMV_SIGNALING
     if (cm->intra_only || cm->error_resilient_mode) cm->use_prev_frame_mvs = 0;
 #endif
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 1340cef..d8d8e7b 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -3870,6 +3870,9 @@
     aom_wb_write_literal(wb, cpi->common.ans_window_size_log2 - 8, 4);
 #endif  // CONFIG_ANS && ANS_MAX_SYMBOLS
     aom_wb_write_bit(wb, cm->allow_screen_content_tools);
+#if CONFIG_INTRABC
+    if (cm->allow_screen_content_tools) aom_wb_write_bit(wb, cm->allow_intrabc);
+#endif  // CONFIG_INTRABC
 #if CONFIG_AMVR
     if (cm->allow_screen_content_tools) {
       if (cm->seq_force_integer_mv == 2) {
@@ -3881,7 +3884,13 @@
     }
 #endif
   } else {
-    if (cm->intra_only) aom_wb_write_bit(wb, cm->allow_screen_content_tools);
+    if (cm->intra_only) {
+      aom_wb_write_bit(wb, cm->allow_screen_content_tools);
+#if CONFIG_INTRABC
+      if (cm->allow_screen_content_tools)
+        aom_wb_write_bit(wb, cm->allow_intrabc);
+#endif  // CONFIG_INTRABC
+    }
 #if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
     if (!cm->error_resilient_mode) {
       if (cm->intra_only) {
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index b646d29..0a653f4 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -3628,11 +3628,19 @@
 
   av1_crc_calculator_init(&td->mb.tx_rd_record.crc_calculator, 24, 0x5D6DCB);
 
+#if CONFIG_INTRABC
+  td->intrabc_used_this_tile = 0;
+#endif  // CONFIG_INTRABC
+
   for (mi_row = tile_info->mi_row_start; mi_row < tile_info->mi_row_end;
        mi_row += cm->mib_size) {
     encode_rd_sb_row(cpi, td, this_tile, mi_row, &tok);
   }
 
+#if CONFIG_INTRABC
+  cpi->intrabc_used |= td->intrabc_used_this_tile;
+#endif  // CONFIG_INTRABC
+
   cpi->tok_count[tile_row][tile_col] =
       (unsigned int)(tok - cpi->tile_tok[tile_row][tile_col]);
   assert(cpi->tok_count[tile_row][tile_col] <=
@@ -3919,6 +3927,13 @@
                           cpi->source->y_height);
   }
 
+#if CONFIG_INTRABC
+  // Allow intrabc when screen content tools are enabled.
+  cm->allow_intrabc = cm->allow_screen_content_tools;
+  // Reset the flag.
+  cpi->intrabc_used = 0;
+#endif  // CONFIG_INTRABC
+
 #if CONFIG_HASH_ME
   if (cpi->oxcf.pass != 1 && cpi->common.allow_screen_content_tools) {
     // add to hash table
@@ -4224,6 +4239,11 @@
     cpi->time_encode_sb_row += aom_usec_timer_elapsed(&emr_timer);
   }
 
+#if CONFIG_INTRABC
+  // If intrabc is allowed but never selected, reset the allow_intrabc flag.
+  if (cm->allow_intrabc && !cpi->intrabc_used) cm->allow_intrabc = 0;
+#endif  // CONFIG_INTRABC
+
 #if 0
   // Keep record of the total distortion this time around for future use
   cpi->last_frame_distortion = cpi->frame_distortion;
@@ -4850,6 +4870,10 @@
 #endif  // CONFIG_DIST_8X8
 
   if (!dry_run) {
+#if CONFIG_INTRABC
+    if (av1_allow_intrabc(bsize, cm))
+      if (is_intrabc_block(mbmi)) td->intrabc_used_this_tile = 1;
+#endif  // CONFIG_INTRABC
     TX_SIZE tx_size =
         is_inter && !mbmi->skip ? mbmi->min_tx_size : mbmi->tx_size;
     if (cm->tx_mode == TX_MODE_SELECT && !xd->lossless[mbmi->segment_id] &&
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 453be1b..32cb2f1 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -341,8 +341,10 @@
   int32_t *mask_buf;
   uint8_t *above_pred_buf;
   uint8_t *left_pred_buf;
-
   PALETTE_BUFFER *palette_buffer;
+#if CONFIG_INTRABC
+  int intrabc_used_this_tile;
+#endif  // CONFIG_INTRABC
 } ThreadData;
 
 struct EncWorkerData;
@@ -606,6 +608,10 @@
 #if CONFIG_BGSPRITE
   int bgsprite_allowed;
 #endif  // CONFIG_BGSPRITE
+#if CONFIG_INTRABC
+  // A flag to indicate if intrabc is ever used in current frame.
+  int intrabc_used;
+#endif  // CONFIG_INTRABC
 } AV1_COMP;
 
 void av1_initialize_enc(void);