Make largest_tile_id explicit output of bitstream

cm->largest_tile_id is effectively an output from
write_tiles_in_tg_obus() via av1_pack_bitstream() to
encode_frame_to_data_rate().  This patch makes this flow explicit and
removes the common variable cpi->largest_tile_id

This forms part of my efforts to remove implicit outputs from and
side-effects of av1_pack_bitstream.  This is part of wider restructuring
and refactoring in order to achieve a clean API separation at the entry
to the low-level encoder.

BUG=aomedia:2244

Change-Id: I60d8547756d4add89dd451744076478212a72c62
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index 61d7fcb..44b7c4a 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -3402,7 +3402,8 @@
 static uint32_t write_tiles_in_tg_obus(AV1_COMP *const cpi, uint8_t *const dst,
                                        struct aom_write_bit_buffer *saved_wb,
                                        uint8_t obu_extension_header,
-                                       const FrameHeaderInfo *fh_info) {
+                                       const FrameHeaderInfo *fh_info,
+                                       int *const largest_tile_id) {
   AV1_COMMON *const cm = &cpi->common;
   aom_writer mode_bc;
   int tile_row, tile_col;
@@ -3428,7 +3429,7 @@
   const int have_tiles = tile_cols * tile_rows > 1;
   int first_tg = 1;
 
-  cpi->largest_tile_id = 0;
+  *largest_tile_id = 0;
 
   if (cm->large_scale_tile) {
     // For large_scale_tile case, we always have only one tile group, so it can
@@ -3494,7 +3495,7 @@
         // Record the maximum tile size we see, so we can compact headers later.
         if (tile_size > max_tile_size) {
           max_tile_size = tile_size;
-          cpi->largest_tile_id = tile_cols * tile_row + tile_col;
+          *largest_tile_id = tile_cols * tile_row + tile_col;
         }
 
         if (have_tiles) {
@@ -3636,7 +3637,7 @@
       curr_tg_data_size += (tile_size + (is_last_tile_in_tg ? 0 : 4));
       buf->size = tile_size;
       if (tile_size > max_tile_size) {
-        cpi->largest_tile_id = tile_cols * tile_row + tile_col;
+        *largest_tile_id = tile_cols * tile_row + tile_col;
         max_tile_size = tile_size;
       }
 
@@ -3671,7 +3672,7 @@
           // Force context update tile to be the first tile in error
           // resiliant mode as the duplicate frame headers will have
           // context_update_tile_id set to 0
-          cpi->largest_tile_id = 0;
+          *largest_tile_id = 0;
 
           // Rewrite the OBU header to change the OBU type to Redundant Frame
           // Header.
@@ -3694,7 +3695,7 @@
     // Fill in context_update_tile_id indicating the tile to use for the
     // cdf update. The encoder currently sets it to the largest tile
     // (but is up to the encoder)
-    aom_wb_overwrite_literal(saved_wb, cpi->largest_tile_id,
+    aom_wb_overwrite_literal(saved_wb, *largest_tile_id,
                              cm->log2_tile_cols + cm->log2_tile_rows);
     // If more than one tile group. tile_size_bytes takes the default value 4
     // and does not need to be set. For a single tile group it is set in the
@@ -3740,7 +3741,8 @@
   return total_size;
 }
 
-int av1_pack_bitstream(AV1_COMP *const cpi, uint8_t *dst, size_t *size) {
+int av1_pack_bitstream(AV1_COMP *const cpi, uint8_t *dst, size_t *size,
+                       int *const largest_tile_id) {
   uint8_t *data = dst;
   uint32_t data_size;
   AV1_COMMON *const cm = &cpi->common;
@@ -3804,8 +3806,8 @@
   } else {
     //  Each tile group obu will be preceded by 4-byte size of the tile group
     //  obu
-    data_size = write_tiles_in_tg_obus(cpi, data, &saved_wb,
-                                       obu_extension_header, &fh_info);
+    data_size = write_tiles_in_tg_obus(
+        cpi, data, &saved_wb, obu_extension_header, &fh_info, largest_tile_id);
   }
   data += data_size;
   *size = data - dst;
diff --git a/av1/encoder/bitstream.h b/av1/encoder/bitstream.h
index 9f9166a..a567086 100644
--- a/av1/encoder/bitstream.h
+++ b/av1/encoder/bitstream.h
@@ -33,7 +33,8 @@
 int write_uleb_obu_size(uint32_t obu_header_size, uint32_t obu_payload_size,
                         uint8_t *dest);
 
-int av1_pack_bitstream(AV1_COMP *const cpi, uint8_t *dest, size_t *size);
+int av1_pack_bitstream(AV1_COMP *const cpi, uint8_t *dst, size_t *size,
+                       int *const largest_tile_id);
 
 static INLINE int av1_preserve_existing_gf(const AV1_COMP *const cpi) {
   // Do not swap gf and arf indices for internal overlay frames
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index c15a111..9b3ab84 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -4839,7 +4839,8 @@
       restore_coding_context(cpi);
 
       finalize_encoded_frame(cpi);
-      if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
+      int largest_tile_id = 0;  // Output from bitstream: unused here
+      if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
         return AOM_CODEC_ERROR;
 
       rc->projected_frame_size = (int)(*size) << 3;
@@ -5141,7 +5142,8 @@
 
     finalize_encoded_frame(cpi);
     // Build the bitstream
-    if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
+    int largest_tile_id = 0;  // Output from bitstream: unused here
+    if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
       return AOM_CODEC_ERROR;
 
     if (seq_params->frame_id_numbers_present_flag &&
@@ -5350,7 +5352,8 @@
 
   finalize_encoded_frame(cpi);
   // Build the bitstream
-  if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
+  int largest_tile_id = 0;  // Output from pack_bitstream
+  if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
     return AOM_CODEC_ERROR;
 
   cpi->seq_params_locked = 1;
@@ -5391,7 +5394,7 @@
 #endif  // CONFIG_ENTROPY_STATS
 
   if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
-    *cm->fc = cpi->tile_data[cpi->largest_tile_id].tctx;
+    *cm->fc = cpi->tile_data[largest_tile_id].tctx;
     av1_reset_cdf_symbol_counters(cm->fc);
   }
 
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 5030635..d84eec5 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -793,7 +793,6 @@
 
   TOKENEXTRA *tile_tok[MAX_TILE_ROWS][MAX_TILE_COLS];
   TOKENLIST *tplist[MAX_TILE_ROWS][MAX_TILE_COLS];
-  int largest_tile_id;
 
   int resize_state;
   int resize_avg_qp;