[NORMATIVE] Signal which tile is used for cdf update

This patch adds signalling of the tile id to use for cdf
update as suggested by Peter de Rivaz and agreed at the
working group meeting of 3rd April 2018. The encoder can
select any tile it likes for the cdf update but at the
moment chooses the largest tile to signal. The decoder
uses which ever tile was signalled.

The additional bits are only signalled if there is more
than one tile and the signalling uses
(log2_tile_cols + log2_tile_rows) bits.

BUG=aomedia:1678

Change-Id: I1db514b8078ccf2a0ba5f3a249ee2eaf59917250
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index e114848..3c31ab2 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -252,6 +252,7 @@
 
   int largest_tile_id;
   size_t largest_tile_size;
+  int context_update_tile_id;
 
   // Scale of the current frame with respect to itself.
   struct scale_factors sf_identity;
diff --git a/av1/common/tile_common.c b/av1/common/tile_common.c
index 01766b0..bf9562c 100644
--- a/av1/common/tile_common.c
+++ b/av1/common/tile_common.c
@@ -42,8 +42,6 @@
   cm->max_log2_tile_rows = tile_log2(1, AOMMIN(sb_rows, MAX_TILE_ROWS));
   cm->min_log2_tiles = tile_log2(max_tile_area_sb, sb_cols * sb_rows);
   cm->min_log2_tiles = AOMMAX(cm->min_log2_tiles, cm->min_log2_tile_cols);
-  // TODO(dominic.symes@arm.com):
-  // Add in levelMinLog2Tiles as a lower limit when levels are defined
 }
 
 void av1_calculate_tile_cols(AV1_COMMON *const cm) {
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 3a9749f..a8652d3 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1580,7 +1580,11 @@
 
   read_tile_info_max_tile(cm, rb);
 
+  cm->context_update_tile_id = 0;
   if (cm->tile_rows * cm->tile_cols > 1) {
+    // tile to use for cdf update
+    cm->context_update_tile_id =
+        aom_rb_read_literal(rb, cm->log2_tile_rows + cm->log2_tile_cols);
     // tile size magnitude
     pbi->tile_size_bytes = aom_rb_read_literal(rb, 2) + 1;
   }
@@ -1771,11 +1775,6 @@
   int tc = 0;
   int first_tile_in_tg = 0;
 
-  if (startTile == 0) {
-    cm->largest_tile_size = 0;
-    cm->largest_tile_id = 0;
-  }
-
   for (int r = 0; r < tile_rows; ++r) {
     for (int c = 0; c < tile_cols; ++c, ++tc) {
       TileBufferDec *const buf = &tile_buffers[r][c];
@@ -1793,10 +1792,6 @@
       data += hdr_offset;
       get_tile_buffer(data_end, pbi->tile_size_bytes, is_last,
                       &pbi->common.error, &data, buf);
-      if (buf->size > cm->largest_tile_size) {
-        cm->largest_tile_size = buf->size;
-        cm->largest_tile_id = r * tile_cols + c;
-      }
     }
   }
 }
@@ -3270,7 +3265,7 @@
 
   if (!xd->corrupted) {
     if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
-      *cm->fc = pbi->tile_data[cm->largest_tile_id].tctx;
+      *cm->fc = pbi->tile_data[cm->context_update_tile_id].tctx;
       av1_reset_cdf_symbol_counters(cm->fc);
     } else {
       debug_check_frame_counts(cm);
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 126a5c3..7c59568 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -2215,6 +2215,8 @@
     return;
   }
   if (cm->tile_rows * cm->tile_cols > 1) {
+    // tile id used for cdf update
+    aom_wb_write_literal(wb, 0, cm->log2_tile_cols + cm->log2_tile_rows);
     // Number of bytes in tile size - 1
     aom_wb_write_literal(wb, 3, 2);
   }
@@ -3681,6 +3683,11 @@
                              max_tile_col_size, &tile_size_bytes, &unused);
     total_size += tile_data_offset;
     assert(tile_size_bytes >= 1 && tile_size_bytes <= 4);
+
+    // fill in id of tile to use for the cdf update which encoder currently
+    // sets to the largest tile (but is up to the encoder)
+    aom_wb_overwrite_literal(saved_wb, cm->largest_tile_id,
+                             cm->log2_tile_cols + cm->log2_tile_rows);
     aom_wb_overwrite_literal(saved_wb, tile_size_bytes - 1, 2);
 
     // Update the OBU length if remux_tiles() reduced the size.